]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.scl.tutorial/scl/Tutorial/3.02 (Exercise) Model fitting.md b/bundles/org.simantics.scl.tutorial/scl/Tutorial/3.02 (Exercise) Model fitting.md
new file mode 100644 (file)
index 0000000..d7602fd
--- /dev/null
@@ -0,0 +1,125 @@
+## Model fitting exercise\r
+\r
+### Step 1\r
+\r
+Create a diagram named X and run the following commands to create a simple model configuration:\r
+\r
+~~~\r
+aadd "X" "POINT" "PO1" (100,110)\r
+aadd "X" "POINT" "PO2" (140,110)\r
+aadd "X" "POINT" "PO3" (180,100)\r
+aadd "X" "POINT" "PO4" (180,120)\r
+aadd "X" "PIPE" "PI1" (120,110)\r
+aadd "X" "PIPE" "PI2" (160,100)\r
+aadd "X" "PIPE" "PI3" (160,120)\r
+amodi "PI1" "PI12_CONNECT_POINT_1" "PO1"\r
+amodi "PI1" "PI12_CONNECT_POINT_2" "PO2"\r
+amodi "PI2" "PI12_CONNECT_POINT_1" "PO2"\r
+amodi "PI2" "PI12_CONNECT_POINT_2" "PO3"\r
+amodi "PI3" "PI12_CONNECT_POINT_1" "PO2"\r
+amodi "PI3" "PI12_CONNECT_POINT_2" "PO4"\r
+aexclude "PO1"\r
+aexclude "PO3"\r
+aexclude "PO4"\r
+amodi "PI1" "PI12_AREA" 0.06\r
+amodi "PI2" "PI12_AREA" 0.03\r
+amodi "PI3" "PI12_AREA" 0.03\r
+amodi "ECCO" "MAXIMUM_TIME_STEP" 0.05\r
+amodi "ECCO" "CURRENT_TIME_STEP" 0.05\r
+amodi "SPEED" "SC_SPEED" 1000.0\r
+~~~\r
+\r
+Save the initial condition and set the simulation time to zero.\r
+\r
+### Step 2\r
+\r
+Run the following commands to print how mass flow of the\r
+pipe PI1 behaves in a simple transient.\r
+\r
+~~~\r
+loadInitialCondition (syncRead $ \_ -> fromResource $ relativeResource currentModel "/Initial%20Condition")\r
+runSequence mdo\r
+      execute (setVar "PO1#PO11_PRESSURE" 1.1)\r
+      fork (wait 1 >> stop)\r
+      fork $ repeatForever mdo\r
+          wait 0.05\r
+          execute do\r
+              massFlow = getVar "PI1#PI12_MIX_MASS_FLOW" :: Double\r
+              print massFlow\r
+~~~\r
+\r
+### Step 3\r
+\r
+Modify the above commands so that they print the index of the time step\r
+and variables PI1#PI12_MIX_MASS_FLOW, PI2#PI12_MIX_MASS_FLOW and PI3#PI12_MIX_MASS_FLOW.\r
+The commands should produce something like:\r
+\r
+~~~\r
+0 3.4978646673069216 1.728445205173922 1.728445205173922\r
+1 143.55517382976987 71.78198788751189 71.78198788751189\r
+2 261.95050765748715 130.97625819564252 130.97625819564252\r
+3 354.3858379588669 177.19356818048865 177.19356818048865\r
+...\r
+~~~\r
+\r
+Use the following functions to maintain the counter\r
+(although it is possible to compute it also from the current simulation time).\r
+\r
+::value[Prelude/ref, Prelude/getRef, Prelude/:=]\r
+\r
+### Step 4\r
+\r
+Modify the commands so that they compute the squared sum of errors from the "measurements" below:\r
+\r
+~~~\r
+measurements = [\r
+    [0.05, 3.4977860762417605, 1.7283740169550754, 1.7284367287300313],\r
+    [0.1, 134.88381983045093, 64.08353845423913, 70.80714606160574],\r
+    [0.15, 231.9062548984293, 105.65792385470164, 126.24691709056889],\r
+    [0.2, 296.4661748497294, 130.5064306373064, 165.95821168905044],\r
+    [0.25, 336.98201115920403, 144.68635329210002, 192.29464808262938],\r
+    [0.3, 361.56600347367186, 152.64673170010516, 208.9187479324867],\r
+    [0.35, 376.0973084183755, 157.07188101607773, 219.0252113124207],\r
+    [0.4, 384.690502383305, 159.57004084578205, 225.12040809039868],\r
+    [0.45, 389.742925383871, 160.98719663407357, 228.75573737254442],\r
+    [0.5, 392.7040735842458, 161.79511170873698, 230.90898615966157],\r
+    [0.55, 394.4365833534546, 162.2577698637119, 232.17883313521224],\r
+    [0.6, 395.36535200557034, 162.50160633728285, 232.86375430699712],\r
+    [0.65, 395.93996807514526, 162.65081127152007, 233.28915819466746],\r
+    [0.7, 396.2954901434386, 162.74229076331505, 233.55318938336018],\r
+    [0.75, 396.51546801100426, 162.79847242004152, 233.71697886953123],\r
+    [0.8, 396.65158867682203, 162.83302418223676, 233.81854447407886],\r
+    [0.85, 396.7358254901475, 162.85429827014457, 233.881506261103],\r
+    [0.9, 396.7879581657287, 162.8674097925786, 233.92052799957008],\r
+    [0.95, 396.8202241096274, 162.87549715428165, 233.94470806962704],\r
+    [1.0, 396.8401951361238, 162.88048891915273, 233.95968928259308]]\r
+~~~\r
+\r
+The columns of the data are, the simulation time\r
+PI1#PI12_MIX_MASS_FLOW, PI2#PI12_MIX_MASS_FLOW and PI3#PI12_MIX_MASS_FLOW.\r
+\r
+### Step 5\r
+\r
+Now create a function `objFun :: [Double] -> <Proc> Double` that is called as\r
+\r
+    objFun [1.0,1.1,1.2]\r
+    \r
+It should run the same commands as in previous steps and return the\r
+squared sum of errors, but additionally set\r
+PI1#PI12_LOSS_COEFF, PI2#PI12_LOSS_COEFF and PI3#PI12_LOSS_COEFF\r
+to the given values.\r
+\r
+It is good idea to also print the parameter values\r
+at the beginning of the function (and remove all other printing).\r
+\r
+### Step 6\r
+\r
+Use the function\r
+\r
+::value[Simantics/Newuoa/newuoa]\r
+\r
+to fit the loss coefficients to the measurements.\r
+Try first with small number of function evaluations,\r
+because the current implementation does not allow interrupting\r
+the optimization. You may try `rhobeg=1` and `rhoend=1e-4`\r
+and initial guess `[1,1,1]`.\r