Kaynağa Gözat

cosim materials

Claudio Gomes 6 yıl önce
ebeveyn
işleme
38f93d7a03

+ 5 - 1
cosimlibrary/cosimlibrary/loader.py

@@ -36,7 +36,11 @@ class FMULoader:
     @staticmethod
     def unload(loaded_fmu: LoadedFMU):
         loaded_fmu.fmu.freeInstance()
-        shutil.rmtree(loaded_fmu.dir)
+        try:
+            shutil.rmtree(loaded_fmu.dir)
+        except PermissionError:
+            print("Warning: failed to clear directory ", loaded_fmu.dir)
+
 
 
 

+ 7 - 3
cosimlibrary/cosimlibrary/runner.py

@@ -85,8 +85,9 @@ class CosimRunner:
         end_connection = scenario.stop_condition
         if end_connection is None:
             assert scenario.stop_time > 0.0
-            return (time < scenario.stop_time) or \
-                   np.isclose(time, scenario.stop_time, rtol=scenario.step_size * 1e-03, atol=scenario.step_size * 1e-03)
+            return (time + scenario.step_size <= scenario.stop_time) or \
+                   np.isclose(time + scenario.step_size, scenario.stop_time, rtol=scenario.step_size * 1e-03, atol=scenario.step_size * 1e-03)
+
         last_value = end_connection.source_fmu.getReal(end_connection.source_vr)[0]
         return last_value > 0.0 and not \
             np.isclose(last_value, 0.0, rtol=scenario.step_size*1e-3, atol=scenario.step_size*1e-3)
@@ -106,9 +107,12 @@ class CosimRunner:
         results = self.init_results(scenario)
 
         for f in scenario.fmus:
-            f.setupExperiment()
+            f.setupExperiment(None, 0.0, scenario.stop_time if scenario.stop_time>0.0 else 0.0)
             f.enterInitializationMode()
 
+        for f, (vrs, vals) in scenario.real_parameters.items():
+            f.setReal(vrs, vals)
+
         # TODO Support fixed point iteration initialization
         self.propagate_initial_outputs(scenario)
 

+ 2 - 2
cosimlibrary/cosimlibrary/scenario.py

@@ -1,5 +1,5 @@
 from enum import Enum,auto
-from typing import List, Dict, Callable
+from typing import List, Dict, Callable, Tuple
 from fmpy.fmi2 import FMU2Slave
 from cosimlibrary.autoinit import AutoInit
 
@@ -35,7 +35,7 @@ class CosimScenario(AutoInit):
     stop_condition: Connection = None
     print_interval: int = 1e-2
     outputs: List[OutputConnection] = None
-    # TODO: FMU parameters
+    real_parameters: Dict[FMU2Slave, Tuple[List[int], List[float]]] = {}
     fmu_connections: Dict[str, Dict[int, Connection]]
 
     def __init__(self, **args):

Dosya farkı çok büyük olduğundan ihmal edildi
+ 25 - 26
examples/Example2_MassSpringDamper/.ipynb_checkpoints/Example2_MassSpringDamper-checkpoint.ipynb


+ 18 - 17
examples/Example2_MassSpringDamper/Example2_MassSpringDamper.ipynb

@@ -19,7 +19,6 @@
     "import matplotlib.pyplot as plt\n",
     "from scipy import linalg\n",
     "import numpy as np\n",
-    "from numpy import linalg as LA\n",
     "from matplotlib.backends.backend_pdf import PdfPages\n",
     "from collections import deque\n",
     "\n",
@@ -35,7 +34,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
+   "execution_count": 2,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -46,7 +45,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 8,
+   "execution_count": 3,
    "metadata": {},
    "outputs": [
     {
@@ -59,7 +58,7 @@
        "v₁"
       ]
      },
-     "execution_count": 8,
+     "execution_count": 3,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -70,7 +69,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 9,
+   "execution_count": 4,
    "metadata": {},
    "outputs": [
     {
@@ -85,7 +84,7 @@
        "        m₁        "
       ]
      },
-     "execution_count": 9,
+     "execution_count": 4,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -103,7 +102,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 19,
+   "execution_count": 5,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -113,7 +112,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 20,
+   "execution_count": 6,
    "metadata": {},
    "outputs": [
     {
@@ -126,7 +125,7 @@
        "v₁"
       ]
      },
-     "execution_count": 20,
+     "execution_count": 6,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -137,7 +136,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 21,
+   "execution_count": 7,
    "metadata": {},
    "outputs": [
     {
@@ -150,7 +149,7 @@
        "-1.0⋅v₁ - 1.0⋅x₁"
       ]
      },
-     "execution_count": 21,
+     "execution_count": 7,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -168,7 +167,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 22,
+   "execution_count": 8,
    "metadata": {},
    "outputs": [
     {
@@ -178,7 +177,7 @@
        "        [-1., -1.]])"
       ]
      },
-     "execution_count": 22,
+     "execution_count": 8,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -201,8 +200,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 25,
-   "metadata": {},
+   "execution_count": 9,
+   "metadata": {
+    "scrolled": true
+   },
    "outputs": [
     {
      "data": {
@@ -265,7 +266,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 26,
+   "execution_count": 10,
    "metadata": {},
    "outputs": [],
    "source": [
@@ -288,7 +289,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 27,
+   "execution_count": 11,
    "metadata": {},
    "outputs": [
     {

+ 1 - 1
examples/Loading_FunctionalMockupUnits/.ipynb_checkpoints/Loading_FunctionalMockupUnits-checkpoint.ipynb

@@ -4,7 +4,7 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "# Double Mass Spring Damper Co-simulation Example"
+    "# Loading Functional Mockup Units"
    ]
   },
   {

+ 1 - 1
examples/Loading_FunctionalMockupUnits/Loading_FunctionalMockupUnits.ipynb

@@ -4,7 +4,7 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "# Double Mass Spring Damper Co-simulation Example"
+    "# Loading Functional Mockup Units"
    ]
   },
   {

Dosya farkı çok büyük olduğundan ihmal edildi
+ 0 - 349
examples/Running_Co-simulations/backup.ipynb


Dosya farkı çok büyük olduğundan ihmal edildi
+ 31042 - 0
examples/Running_Cosimulations/.ipynb_checkpoints/Running_Cosimulations-checkpoint.ipynb


Dosya farkı çok büyük olduğundan ihmal edildi
+ 31042 - 0
examples/Running_Cosimulations/Running_Cosimulations.ipynb


BIN
fmus/MassSpringDamper1.fmu


BIN
fmus/MassSpringDamper2.fmu


+ 11 - 0
fmus/models/MassSpringDamper1.mo

@@ -0,0 +1,11 @@
+model MassSpringDamper1
+  parameter Real c1 = 5.0;
+  parameter Real d1 = 1.0;
+  parameter Real m1 = 1.0;
+  output Real x1(start = 1);
+  output Real v1(start = 0);
+  input Real fk;
+equation
+  der(x1) = v1;
+  der(v1) = 1 / m1 * ((-c1 * x1) - d1 * v1 + fk);
+end MassSpringDamper1;

+ 16 - 0
fmus/models/MassSpringDamper2.mo

@@ -0,0 +1,16 @@
+model MassSpringDamper2
+  parameter Real c2 = 1;
+  parameter Real d2 = 2;
+  parameter Real cc = 1;
+  parameter Real dc = 100;
+  parameter Real m2 = 1;
+  input Real x1(start = 1);
+  input Real v1(start = 0);
+  output Real x2(start = 0);
+  output Real v2(start = 0);
+  output Real fk;
+equation
+  fk = cc * (x2 - x1) + dc * (x2 - x1);
+  der(x2) = v2;
+  der(v2) = 1 / m2 * ((-c2 * x2) - d2 * v2 - fk);
+end MassSpringDamper2;

+ 19 - 0
fmus/models/MassSpringDampersScenario.mo

@@ -0,0 +1,19 @@
+model MassSpringDampersScenario
+  MassSpringDamper1 msd1;
+  MassSpringDamper2 msd2;
+  output Real x1;
+  output Real v1;
+  output Real x2;
+  output Real v2;
+  output Real fk;
+equation
+  msd1.fk = msd2.fk;
+  msd2.x1 = msd1.x1;
+  msd2.v1 = msd1.v1;
+  x1 = msd1.x1;
+  v1 = msd1.v1;
+  x2 = msd2.x2;
+  v2 = msd2.v2;
+  fk = msd2.fk;
+  annotation(experiment(StartTime = 0, StopTime = 10, Tolerance = 0.001, Interval = 0.02));
+end MassSpringDampersScenario;