Sphinx SD Tools
Welcome, Guest
Please Login or Register.    Lost Password?
executing the simulation from java (1 viewing) (1) Guest
Development
Go to bottom Post Reply Favoured: 0
TOPIC: executing the simulation from java
#34
cboehme (Visitor)
Posts: 0
graphgraph
User Offline Click here to see the profile of this user
Birthdate:
executing the simulation from java 6 Months, 1 Week ago Karma: 0  
Hello,

I want to open and execute the simulation models from my java code.

I imported the sources of Sphinx and loaded the example model (osc.scp) successfully.

Now i want to start the simulation. Because i didn't found any documentation about this, I tried it with the ModelExecutor. Here is my entire code:

Code:



SDModel model;

String projectFileName = "model/osc.sdp";

ZipFile zipFile = new ZipFile(projectFileName);

ZipEntry modelEntry = zipFile.getEntry("model.sdmodel");

InputStream modelXml = zipFile.getInputStream(modelEntry);

try {

       model = SDModelSerializator.modelFromXML(modelXml);

} finally {

        modelXml.close();

}



//executions Settings saved in the model

ModellingSettings executionSettings = new ModellingSettings();

executionSettings.setStartTime(0.0);

executionSettings.setEndTime(50.0);

executionSettings.setDT(100);

executionSettings.setReportingPeriod(1);

                       



ModelExecutor executor = new ModelExecutor(model, executionSettings);

               

executor.start();



//repeat, doing all the ticks

executor.doTick();



executor.stop();                       

 



I didn't modified the model, but there comes an exception:

Code:



org.sphinxes.exception.InconcistentModelException: There are errors in the model:

Fehler in Formel: k = 25*exp(-0.05*TIME )



        at org.sphinxes.sdmodel.execution.ModelExecutor.checkModel(ModelExecutor.java:134)

        at org.sphinxes.sdmodel.execution.ModelExecutor.<init>(ModelExecutor.java:116)

        at test.SphinxTester.main(SphinxTester.java:45)

 



1. Is this the right way to execute the model from java? Are there any other things I have to setup before executing?
2. How do I get to the simulation results? By accessing the model and iterate through all elements?
3. Is there a xml-schema for the xml-Model File in the zip archive?

Thanks!

Christian
 
Report to moderator   Logged Logged  
 
Last Edit: 2011/11/11 16:13 By .
  The administrator has disabled public write access.
#35
cboehme (Visitor)
Posts: 0
graphgraph
User Offline Click here to see the profile of this user
Birthdate:
Re:executing the simulation from java 6 Months ago Karma: 0  
Yesterday I tried again and now it works.

Here the solution for everyone, who wants to do similar things.

To let the ModelExecutor understand the formulas, you need to setup a FunctionsLoader, which extends the AbstractFormulaFunctionsLoader:

Code:



public class FormulaFunctionsLoader extends AbstractFormulaFunctionsLoader {

        @Override

        public void loadFunctions() {

                try {

                        clearFunctions();

                        InputStream stream = new FileInputStream("functions/functions_standard.xml");

                        loadFunctionsFromStream(stream);

                } catch (FileNotFoundException e) {

                }

        }

}

 



The xml funcsions file is located in the sources.

Then you need to load it, before executing the model:

Code:



FormulaFunctionsLoader loader = new FormulaFunctionsLoader();

loader.loadFunctions();

FormulaFunctions.setFunctionsLoader(loader);

 



Now Sphinx knows how to handle with special functions.

Executing the model with following code works very well:

Code:



ModelExecutor executor = new ModelExecutor(model, executionSettings);

executor.init();

executor.start();

while (executor.getCurrentTime() < endTime) {

        executor.doTick();

        String elementName = model.getStocks().get(0).getName();

        System.out.println(elementName + ": " + executor.getEntityExecutor(elementName).getValue());

}                     

executor.stop();

 



Again the question:
1. Is there any documentation about running Sphinx directly from java?
2. Are there xml-Schema definitions for the xml-files?

Christian
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
Go to top Post Reply

get the latest posts directly to your desktop