]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.tutorial/scl/Tutorial/3.02 (Exercise) Model fitting.md
Import org.simantics.scl.tutorial from incubator SVN repo
[simantics/platform.git] / bundles / org.simantics.scl.tutorial / scl / Tutorial / 3.02 (Exercise) Model fitting.md
1 ## Model fitting exercise\r
2 \r
3 ### Step 1\r
4 \r
5 Create a diagram named X and run the following commands to create a simple model configuration:\r
6 \r
7 ~~~\r
8 aadd "X" "POINT" "PO1" (100,110)\r
9 aadd "X" "POINT" "PO2" (140,110)\r
10 aadd "X" "POINT" "PO3" (180,100)\r
11 aadd "X" "POINT" "PO4" (180,120)\r
12 aadd "X" "PIPE" "PI1" (120,110)\r
13 aadd "X" "PIPE" "PI2" (160,100)\r
14 aadd "X" "PIPE" "PI3" (160,120)\r
15 amodi "PI1" "PI12_CONNECT_POINT_1" "PO1"\r
16 amodi "PI1" "PI12_CONNECT_POINT_2" "PO2"\r
17 amodi "PI2" "PI12_CONNECT_POINT_1" "PO2"\r
18 amodi "PI2" "PI12_CONNECT_POINT_2" "PO3"\r
19 amodi "PI3" "PI12_CONNECT_POINT_1" "PO2"\r
20 amodi "PI3" "PI12_CONNECT_POINT_2" "PO4"\r
21 aexclude "PO1"\r
22 aexclude "PO3"\r
23 aexclude "PO4"\r
24 amodi "PI1" "PI12_AREA" 0.06\r
25 amodi "PI2" "PI12_AREA" 0.03\r
26 amodi "PI3" "PI12_AREA" 0.03\r
27 amodi "ECCO" "MAXIMUM_TIME_STEP" 0.05\r
28 amodi "ECCO" "CURRENT_TIME_STEP" 0.05\r
29 amodi "SPEED" "SC_SPEED" 1000.0\r
30 ~~~\r
31 \r
32 Save the initial condition and set the simulation time to zero.\r
33 \r
34 ### Step 2\r
35 \r
36 Run the following commands to print how mass flow of the\r
37 pipe PI1 behaves in a simple transient.\r
38 \r
39 ~~~\r
40 loadInitialCondition (syncRead $ \_ -> fromResource $ relativeResource currentModel "/Initial%20Condition")\r
41 runSequence mdo\r
42       execute (setVar "PO1#PO11_PRESSURE" 1.1)\r
43       fork (wait 1 >> stop)\r
44       fork $ repeatForever mdo\r
45           wait 0.05\r
46           execute do\r
47               massFlow = getVar "PI1#PI12_MIX_MASS_FLOW" :: Double\r
48               print massFlow\r
49 ~~~\r
50 \r
51 ### Step 3\r
52 \r
53 Modify the above commands so that they print the index of the time step\r
54 and variables PI1#PI12_MIX_MASS_FLOW, PI2#PI12_MIX_MASS_FLOW and PI3#PI12_MIX_MASS_FLOW.\r
55 The commands should produce something like:\r
56 \r
57 ~~~\r
58 0 3.4978646673069216 1.728445205173922 1.728445205173922\r
59 1 143.55517382976987 71.78198788751189 71.78198788751189\r
60 2 261.95050765748715 130.97625819564252 130.97625819564252\r
61 3 354.3858379588669 177.19356818048865 177.19356818048865\r
62 ...\r
63 ~~~\r
64 \r
65 Use the following functions to maintain the counter\r
66 (although it is possible to compute it also from the current simulation time).\r
67 \r
68 ::value[Prelude/ref, Prelude/getRef, Prelude/:=]\r
69 \r
70 ### Step 4\r
71 \r
72 Modify the commands so that they compute the squared sum of errors from the "measurements" below:\r
73 \r
74 ~~~\r
75 measurements = [\r
76     [0.05, 3.4977860762417605, 1.7283740169550754, 1.7284367287300313],\r
77     [0.1, 134.88381983045093, 64.08353845423913, 70.80714606160574],\r
78     [0.15, 231.9062548984293, 105.65792385470164, 126.24691709056889],\r
79     [0.2, 296.4661748497294, 130.5064306373064, 165.95821168905044],\r
80     [0.25, 336.98201115920403, 144.68635329210002, 192.29464808262938],\r
81     [0.3, 361.56600347367186, 152.64673170010516, 208.9187479324867],\r
82     [0.35, 376.0973084183755, 157.07188101607773, 219.0252113124207],\r
83     [0.4, 384.690502383305, 159.57004084578205, 225.12040809039868],\r
84     [0.45, 389.742925383871, 160.98719663407357, 228.75573737254442],\r
85     [0.5, 392.7040735842458, 161.79511170873698, 230.90898615966157],\r
86     [0.55, 394.4365833534546, 162.2577698637119, 232.17883313521224],\r
87     [0.6, 395.36535200557034, 162.50160633728285, 232.86375430699712],\r
88     [0.65, 395.93996807514526, 162.65081127152007, 233.28915819466746],\r
89     [0.7, 396.2954901434386, 162.74229076331505, 233.55318938336018],\r
90     [0.75, 396.51546801100426, 162.79847242004152, 233.71697886953123],\r
91     [0.8, 396.65158867682203, 162.83302418223676, 233.81854447407886],\r
92     [0.85, 396.7358254901475, 162.85429827014457, 233.881506261103],\r
93     [0.9, 396.7879581657287, 162.8674097925786, 233.92052799957008],\r
94     [0.95, 396.8202241096274, 162.87549715428165, 233.94470806962704],\r
95     [1.0, 396.8401951361238, 162.88048891915273, 233.95968928259308]]\r
96 ~~~\r
97 \r
98 The columns of the data are, the simulation time\r
99 PI1#PI12_MIX_MASS_FLOW, PI2#PI12_MIX_MASS_FLOW and PI3#PI12_MIX_MASS_FLOW.\r
100 \r
101 ### Step 5\r
102 \r
103 Now create a function `objFun :: [Double] -> <Proc> Double` that is called as\r
104 \r
105     objFun [1.0,1.1,1.2]\r
106     \r
107 It should run the same commands as in previous steps and return the\r
108 squared sum of errors, but additionally set\r
109 PI1#PI12_LOSS_COEFF, PI2#PI12_LOSS_COEFF and PI3#PI12_LOSS_COEFF\r
110 to the given values.\r
111 \r
112 It is good idea to also print the parameter values\r
113 at the beginning of the function (and remove all other printing).\r
114 \r
115 ### Step 6\r
116 \r
117 Use the function\r
118 \r
119 ::value[Simantics/Newuoa/newuoa]\r
120 \r
121 to fit the loss coefficients to the measurements.\r
122 Try first with small number of function evaluations,\r
123 because the current implementation does not allow interrupting\r
124 the optimization. You may try `rhobeg=1` and `rhoend=1e-4`\r
125 and initial guess `[1,1,1]`.\r