| 1 | echo(false) ;
|
|---|
| 2 | if (loadFile("plant.mo"))then
|
|---|
| 3 | print("Plant Model Loaded\n");
|
|---|
| 4 | else
|
|---|
| 5 | print("Plant Model failed\n");
|
|---|
| 6 | exit(0) ;
|
|---|
| 7 | end if;
|
|---|
| 8 | /************ Cleanup Work **********/
|
|---|
| 9 | print("Cleaning Up Work Area\n") ;
|
|---|
| 10 | system("rm work/*");
|
|---|
| 11 | cd("./work"); // Do all the dirty work here
|
|---|
| 12 | print(getSettings());
|
|---|
| 13 |
|
|---|
| 14 | /********* Flatten and Run Plant ******/
|
|---|
| 15 | print("Simulation has begun\n");
|
|---|
| 16 |
|
|---|
| 17 | /* if you don't set timeStep it is calculated as (stopTime-startTime)/count
|
|---|
| 18 | which can be bad news fi you set too large a stopTime this really
|
|---|
| 19 | caught me off gaurd, lost few weeks of effor */
|
|---|
| 20 |
|
|---|
| 21 | tEnd := 10;
|
|---|
| 22 | iniStepSize := 0.05 ;
|
|---|
| 23 | count := 100 ; // floor(tEnd / iniStepSize) ;
|
|---|
| 24 | setDebugFlags("infoXmlOperations,failtrace");
|
|---|
| 25 | flags := "" ; // "-lv=LOG_NLS" ;
|
|---|
| 26 | echo(true);
|
|---|
| 27 | status := simulate(plant, startTime=0, outputFormat="mat",
|
|---|
| 28 | stopTime=tEnd, tolerance=1e-6, stepSize=iniStepSize,
|
|---|
| 29 | numberOfIntervals=count, simflags=flags) ;
|
|---|
| 30 | echo(false);
|
|---|
| 31 |
|
|---|
| 32 | /* Error Handling stuff */
|
|---|
| 33 | getErrorMessage();
|
|---|
| 34 | writeFile("tmpfile",status.messages);
|
|---|
| 35 | system("grep \"Simulation execution failed\" tmpfile | wc -l > tmpfile");
|
|---|
| 36 | getErrorMessage();
|
|---|
| 37 | success := Modelica.Utilities.Strings.isEqual(readFile("tmpfile"),"0",false);
|
|---|
| 38 | getErrorMessage();
|
|---|
| 39 |
|
|---|
| 40 | if (false) then
|
|---|
| 41 | print("simulation failed ... bailing out\n");
|
|---|
| 42 | exit(0) ;
|
|---|
| 43 | else
|
|---|
| 44 | print("simulation done\n");
|
|---|
| 45 | sVars := readSimulationResultVars("plant_res.mat");
|
|---|
| 46 | print("No. of Plant Variables = " + String(size(sVars,1)) + "\n");
|
|---|
| 47 | end if;
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | print("Plotting\n");
|
|---|
| 51 |
|
|---|
| 52 | plot({mv, sp, pv},
|
|---|
| 53 | curveWidth=2, externalWindow=true, title = "PI controller)");
|
|---|
| 54 | system("sleep 1s");
|
|---|
| 55 |
|
|---|
| 56 | /***********************************************************/
|
|---|
| 57 | /* Useful String Manipulation Functions */
|
|---|
| 58 | /* These exist in Modelica.Utilities.Strings ..
|
|---|
| 59 | but just an illustration of using external calls */
|
|---|
| 60 | /***********************************************************/
|
|---|
| 61 | loadString("
|
|---|
| 62 | function strlen
|
|---|
| 63 | input String str;
|
|---|
| 64 | output Integer len;
|
|---|
| 65 | external \"C\" len=ModelicaStrings_length(str);
|
|---|
| 66 | end strlen;
|
|---|
| 67 | function substr
|
|---|
| 68 | input String str;
|
|---|
| 69 | input Integer i1,i2;
|
|---|
| 70 | output String out;
|
|---|
| 71 | external \"C\" out=ModelicaStrings_substring(str,i1,i2);
|
|---|
| 72 | end substr;
|
|---|
| 73 | ");
|
|---|
| 74 | /***********************************************************/
|
|---|
| 75 |
|
|---|