Search code examples
javaxmlmodelicaopenmodelica

How can I change Modelica MultiBody component parameters by working on the XML file?


I have created a trivial model of a pendulum in Modelica. After that, I have built the model so that the .xml file and the .exe file were generated.

 model TestJAVA_v2
 inner Modelica.Mechanics.MultiBody.World world annotation(
Placement(visible = true, transformation(origin = {-86, 28}, extent = {{-10, 
-10}, {10, 10}}, rotation = 0)));
 Modelica.Mechanics.MultiBody.Joints.Revolute revolute1(n = {1, 0, 0}, 
 phi(fixed = true, start = 0.785398))  annotation(
 Placement(visible = true, transformation(origin = {-46, 28}, extent = 
 {{-10, 
-10}, {10, 10}}, rotation = 0)));
 Modelica.Mechanics.MultiBody.Parts.PointMass pointMass1 annotation(
 Placement(visible = true, transformation(origin = {16, 28}, extent = {{-10, 
-10}, {10, 10}}, rotation = 0)));
 Modelica.Mechanics.MultiBody.Parts.BodyShape bodyShape1(r = {0, 1, 1}, r_CM 
 = bodyShape1.r / 2)  annotation(
 Placement(visible = true, transformation(origin = {-14, 26}, extent = 
 {{-10, 
-10}, {10, 10}}, rotation = 0)));
equation
connect(bodyShape1.frame_b, pointMass1.frame_a) annotation(
Line(points = {{-4, 26}, {16, 26}, {16, 28}, {16, 28}}, color = {95, 95, 
95}));
connect(revolute1.frame_b, bodyShape1.frame_a) annotation(
Line(points = {{-36, 28}, {-24, 28}, {-24, 26}, {-24, 26}}, color = {95, 95, 
95}));
connect(world.frame_b, revolute1.frame_a) annotation(
Line(points = {{-76, 28}, {-58, 28}, {-58, 28}, {-56, 28}}, color = {95, 95, 
95}));
annotation(
uses(Modelica(version = "3.2.2")),
experiment(StartTime = 0, StopTime = 15, Tolerance = 0.001, Interval = 
0.0010002),
__OpenModelica_simulationFlags(iim = "none", lv = "LOG_STATS", s = 
 "dassl"));
end TestJAVA_v2;

Using an xml parser I am able to change the mass of the BodyShape and the vector r (Vector from frame_a to frame_b resolved in frame_a). But after simulating, I found out that only the mass has changed, while the length of the component did not. Is it possible to modify this vector through the xml file ?


Solution

  • Some parameters cannot be changed without recompiling the model.

    Search in the ScalarVariable for isValueChangeable attribute. If isValueChangeable = "false" then you cannot change that value via the xml file.

    This can happen because:

    1. the parameter is structural (array dimensions depend on it)
    2. the parameter has annotation(Evaluate=true) which basically makes it a constant

    More information on edit, after some more analysis:

    • the current front-end forces evaluation of the bodyShape1.r so it makes it a constant that cannot be changed
    • you can use the nightly builds (v1.13) and run omc -d=newInst or set in OMEdit the OMC flags to -d=newInst to use the new front-end implementation that allows bodyShape1.r to be changeable.

    With -d=newInst you get:

      <ScalarVariable
        name = "bodyShape1.r[1]"
        valueReference = "1711"
        description = "Vector from frame_a to frame_b resolved in frame_a"
        variability = "parameter" isDiscrete = "true"
        causality = "internal" isValueChangeable = "true"
        alias = "noAlias"
        classIndex = "86" classType = "rPar"
        isProtected = "false" hideResult = "false"
        fileName = "c:/home/adrpo33/dev/OpenModelica/build/lib/omlibrary/Modelica 3.2.2/Mechanics/MultiBody/Parts.mo" startLine = "1038" startColumn = "5" endLine = "1039" endColumn = "59" fileWritable = "true">
        <Real start="0.0" fixed="true" useNominal="false" unit="m" />
      </ScalarVariable>
    

    which has isValueChangeable = "true".

    adrpo33@computer MINGW64 /c/home/adrpo33/dev/OMTesting/mb
    $ ./TestJAVA_v2.exe -override=bodyShape1.r[1]=2 -lv=LOG_ALL | grep 'bodyShape1.r\['
    LOG_SOLVER        | info    | read override values: bodyShape1.r[1]=2
    LOG_SOLVER        | info    | override bodyShape1.r[1] = 2
    |                 | |       | | Real bodyShape1.r[1](start=2, fixed=true, {nominal=1}, min=-1.79769e+308, max=1.79769e+308)
    |                 | |       | | Real bodyShape1.r[2](start=1, fixed=true, {nominal=1}, min=-1.79769e+308, max=1.79769e+308)
    |                 | |       | | Real bodyShape1.r[3](start=1, fixed=true, {nominal=1}, min=-1.79769e+308, max=1.79769e+308)
    |                 | |       | | | [87] parameter Real bodyShape1.r[1](start=2, fixed=true) = 2
    |                 | |       | | | [88] parameter Real bodyShape1.r[2](start=1, fixed=true) = 1
    |                 | |       | | | [89] parameter Real bodyShape1.r[3](start=1, fixed=true) = 1
    

    Seems to be working fine with omc -d=newInst and Model -override