]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.tutorial/scl/Tutorial/3.04 (Solution) Model fitting.md
Import org.simantics.scl.tutorial from incubator SVN repo
[simantics/platform.git] / bundles / org.simantics.scl.tutorial / scl / Tutorial / 3.04 (Solution) Model fitting.md
diff --git a/bundles/org.simantics.scl.tutorial/scl/Tutorial/3.04 (Solution) Model fitting.md b/bundles/org.simantics.scl.tutorial/scl/Tutorial/3.04 (Solution) Model fitting.md
new file mode 100644 (file)
index 0000000..01a762b
--- /dev/null
@@ -0,0 +1,46 @@
+## Model fitting solution\r
+\r
+The original loss coefficints are (although the optimizer will not find exact values)\r
+\r
+    amodi "PI1" "PI12_LOSS_COEFF" 1.2\r
+    amodi "PI2" "PI12_LOSS_COEFF" 4.6\r
+    amodi "PI3" "PI12_LOSS_COEFF" 2.2\r
+    \r
+Here is one possible solution:\r
+\r
+~~~\r
+import "Apros/Sequences"\r
+import "Apros/InitialCondition"\r
+import "Simantics/DB"\r
+import "Simantics/Model"\r
+\r
+objFun (p :: [Double]) = do\r
+    print p\r
+    loadInitialCondition (syncRead $ \_ -> fromResource $ relativeResource currentModel "/Initial%20Condition")\r
+    iRef = ref 0\r
+    sqSum = ref (0 :: Double)\r
+    runSequence mdo\r
+        execute do\r
+            setVar "PO1#PO11_PRESSURE" 1.1\r
+            setVar "PI1#PI12_LOSS_COEFF" (p!0)\r
+            setVar "PI2#PI12_LOSS_COEFF" (p!1)\r
+            setVar "PI3#PI12_LOSS_COEFF" (p!2)\r
+        fork (wait 1 >> stop)\r
+        fork $ repeatForever mdo\r
+          wait 0.05\r
+          execute do\r
+              i = getRef iRef\r
+              iRef := i+1\r
+              row = measurements!i\r
+              a = getVar "PI1#PI12_MIX_MASS_FLOW" - row!1 :: Double\r
+              b = getVar "PI2#PI12_MIX_MASS_FLOW" - row!2 :: Double\r
+              c = getVar "PI3#PI12_MIX_MASS_FLOW" - row!3 :: Double\r
+              sqSum := getRef sqSum + a*a + b*b + c*c\r
+    getRef sqSum\r
+~~~\r
+\r
+with this definition call\r
+\r
+    newuoa 1.0 1e-4 100 objFun [1,1,1]\r
+    \r
+from console.
\ No newline at end of file