Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register
  • Index
  • » Users
  • » naibaf.omsare
  • » Profile

Posts

Posts

I'm trying to make the output variable of an object declared within a class, available to the class to use it an output also. I'm using the outer and inner declarations.  What I did works with the old frontend but fails with the new frontend. I don't know what the frontend is, so I don't know whether this is a bug or just a change in the usage specification.

In the outermost class, whose name  is SpringPendulum, the variable readout  is declared as inner, and within the instance sensors1, it is declared as outer.   

With the old frontend, the variable readout appears as an output of SpringPendulum with the correct values. However, the new frontend gives the error below.

Thank you in advance for any advice.

Fabian

Code:


[1] 10:47:19 Symbolic Error
Too few equations, under-determined system. The model has 1437 equation(s) and 1443 variable(s).

[2] 10:47:19 Symbolic Warning
[Sensors: 3:3-4:198]: Variable sensors1.readout[6] does not have any remaining equation to be solved in.
  The original equations were:

[3] 10:47:19 Symbolic Warning
[Sensors: 3:3-4:198]: Variable sensors1.readout[5] does not have any remaining equation to be solved in.
  The original equations were:

[4] 10:47:19 Symbolic Warning
[Sensors: 3:3-4:198]: Variable sensors1.readout[4] does not have any remaining equation to be solved in.
  The original equations were:

[5] 10:47:19 Symbolic Warning
[Sensors: 3:3-4:198]: Variable sensors1.readout[3] does not have any remaining equation to be solved in.
  The original equations were:

[6] 10:47:19 Symbolic Warning
[Sensors: 3:3-4:198]: Variable sensors1.readout[2] does not have any remaining equation to be solved in.
  The original equations were:

[7] 10:47:19 Symbolic Warning
[Sensors: 3:3-4:198]: Variable sensors1.readout[1] does not have any remaining equation to be solved in.
  The original equations were:

Code:


model SpringPendulum
 
  inner Modelica.Blocks.Interfaces.RealOutput readout[6] ;
   
  inner Modelica.Mechanics.MultiBody.World world(label2 = "z", n = {0, 0, -1}) annotation(
    Placement(visible = true, transformation(origin = {-66, -20}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Mechanics.MultiBody.Forces.Spring spring1(c = 10) annotation(
    Placement(visible = true, transformation(origin = {20, -10}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Mechanics.MultiBody.Parts.Body body1(m = 0.3, useQuaternions = true) annotation(
    Placement(visible = true, transformation(origin = {72, -12}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Mechanics.Translational.Sources.Position position1 annotation(
    Placement(visible = true, transformation(origin = {-38, 34}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Mechanics.MultiBody.Joints.Prismatic prismatic1(useAxisFlange = true) annotation(
    Placement(visible = true, transformation(origin = {-26, -10}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Blocks.Interfaces.RealInput gnd_in annotation(
    Placement(visible = true, transformation(origin = {-110, 46}, extent = {{-20, -20}, {20, 20}}, rotation = 0), iconTransformation(origin = {-110, 46}, extent = {{-20, -20}, {20, 20}}, rotation = 0)));
  Modelica.Mechanics.MultiBody.Joints.FreeMotionScalarInit freeMotionScalarInit1(r_rel_a_3(fixed = true, start = -0.2943), use_r = true) annotation(
    Placement(visible = true, transformation(origin = {12, -72}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Sensors sensors1 annotation(
    Placement(visible = true, transformation(origin = {83, 79}, extent = {{-11, -11}, {11, 11}}, rotation = 0))); 
  equation
  connect(spring1.frame_b, body1.frame_a) annotation(
    Line(points = {{30, -10}, {62, -10}, {62, -12}}));
  connect(prismatic1.frame_b, spring1.frame_a) annotation(
    Line(points = {{-16, -10}, {10, -10}}));
  connect(position1.flange, prismatic1.axis) annotation(
    Line(points = {{-28, 34}, {-18, 34}, {-18, -4}}, color = {0, 127, 0}));
  connect(world.frame_b, prismatic1.frame_a) annotation(
    Line(points = {{-56, -20}, {-52, -20}, {-52, -10}, {-36, -10}}));
  connect(gnd_in, position1.s_ref) annotation(
    Line(points = {{-110, 46}, {-70, 46}, {-70, 34}, {-50, 34}}, color = {0, 0, 127}));
  connect(freeMotionScalarInit1.frame_a, world.frame_b) annotation(
    Line(points = {{2, -72}, {-28, -72}, {-28, -20}, {-56, -20}}));
  connect(freeMotionScalarInit1.frame_b, body1.frame_a) annotation(
    Line(points = {{22, -72}, {54, -72}, {54, -12}, {62, -12}}, color = {95, 95, 95}));
  connect(body1.frame_a, sensors1.frame_a) annotation(
    Line(points = {{62, -12}, {44, -12}, {44, 79}, {73, 79}}, color = {95, 95, 95}));  protected
  annotation(
    uses(Modelica(version = "4.0.0")));
end SpringPendulum;

Code:


model Sensors 

  outer Modelica.Blocks.Interfaces.RealOutput readout[6] annotation(
    Placement(visible = true, transformation(origin = {-2, 0}, extent = {{10, -10}, {-10, 10}}, rotation = 0), iconTransformation(origin = {108, 6}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));


  Modelica.Mechanics.MultiBody.Interfaces.Frame_a frame_a annotation(
    Placement(visible = true, transformation(origin = {-98, 0}, extent = {{-16, -16}, {16, 16}}, rotation = 0), iconTransformation(origin = {-94, 0}, extent = {{-16, -16}, {16, 16}}, rotation = 0)));
  Modelica.Mechanics.MultiBody.Sensors.AbsolutePosition absolutePosition annotation(
    Placement(visible = true, transformation(origin = {-56, 18}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Mechanics.MultiBody.Sensors.AbsoluteAngles absoluteAngles annotation(
    Placement(visible = true, transformation(origin = {-55, -17}, extent = {{-11, -11}, {11, 11}}, rotation = 0)));


equation
  connect(frame_a, absolutePosition.frame_a) annotation(
    Line(points = {{-98, 0}, {-80, 0}, {-80, 18}, {-66, 18}}));
  connect(absoluteAngles.frame_a, frame_a) annotation(
    Line(points = {{-66, -17}, {-80, -17}, {-80, 0}, {-98, 0}}));
  connect(absolutePosition.r, readout[1:3]) annotation(
    Line(points = {{-44, 18}, {-2, 18}, {-2, 0}}, color = {0, 0, 127}, thickness = 0.5));
  connect(absoluteAngles.angles, readout[4:6]) annotation(
    Line(points = {{-42, -16}, {-2, -16}, {-2, 0}}, color = {0, 0, 127}, thickness = 0.5));
  annotation(
    uses(Modelica(version = "4.0.0")));
end Sensors;

Feb-01-22 01:10:45
Symbolic and numerical linearization yield different values for the same model.
Category: Developer

Dear all,

Symbolic linearization is not working. Numerical linearization works as expected, but the symbolic one yields an incorrect result.

The system I'm using for testing is a simple harmonic oscillator. The equation of motion is

x'' + w0^2 * ( x - gnd ) = 0, with resonant frequency w0 = 2 rad/s.

The state space representation is easily calculated by defining the variables q1 =x and q2=x'. The state space vector and matrices are

q = [ q1 ]
       [ q2 ] ,

A = [ 0 ,  1 ]
       [-4 ,  0 ] ,

B = [ 0 ]
       [ 4 ] ,

C = [ 1 , 0 ] ,

D = 0.

with

Input: gnd, position of the ground
Output: q1 = x, position of mass.

The symbolic method yield the correct values for A and B, but not for C. Namely, it yields

C = [ 0 , 1 ].

In more elaborate examples, it can be seen that the value for D is also wrong, but not in this simple one.

I'm using Windows 10, OpenModelica v1.18.1 (64-bit) and OMShell.

I guess there is still a chance I'm misunderstanding the usage of the symbolic linearization, but this seems to be a bug. Is the correct forum to post this, is GitHub  more appropriate?

Any help in solving this will be appreciated.

Regards,

Fabian

Numeric linearization:

Code:


loadFile("HarmonicOscillator.mo")
clearCommandLineOptions()
linearize(HarmonicOscillator)
readFile("linearized_model.mo")

Symbolic linearization

Code:


loadFile("HarmonicOscillator.mo")
setCommandLineOptions({"+generateSymbolicLinearization"})
linearize(HarmonicOscillator)
readFile("linearized_model.mo")

Model

Code:


model HarmonicOscillator
    input Real gnd = 0.03 "Position of the ground" ;
    Real q2 "Velocity of the test mass" ;
    constant Real w0 = 2 "Resonance frequency" ;
    output Real q1 "Position of the test mass" ;
initial equation
    q1 = 0.03 "Initial position of test mass" ;
    q2 = 0.0 "Initial velocity of test mass" ;
equation
    der( q1 ) = q2 ;
    der( q2 ) = - w0 * w0 * (q1 - gnd) ;   
end HarmonicOscillator ;

Jan-27-22 00:01:53
The linearize() function seems to provide a wrong value for one of the state space matrices.

Hello all,

I want to use OMPython to calculate the space state matrices of the differential equation of a simple harmonic oscillator, system which I use only as a test. The problem is that the command mod.linearize() yields the wrong expression for one of the matrices.

The equation of motion is

x'' + w0^2 * ( x - gnd ) = 0, with resonant frequency w0 = 2 rad/s.

The state space representation is easily calculated by defining the variables q1 =x and q2=x'. The state space vector and matrices are

q = [ q1 ]
    [ q2 ] ,

A = [ 0 ,  1 ]
    [-4 ,  0 ] ,

B = [ 0 ]
    [ 4 ] ,

C = [ 1 , 0 ] ,

D = 0.

with

Input: gnd, position of the ground
Output: q1 = x, position of mass.

The command mod.linearize() yields the incorrect value for C

C = [ 0 , 1 ] ,

but for A, B and D the values are correct.

This seems to be a bug, but I could have misunderstood anything. As an additional check, I calculated the transfer function, whose expression confirms there seems to be an error in the values produced by mod.linearize().

The Modelica model and Jupyter notebook commands are below. I'm using Windows 10, OpenModelica v1.18.1 (64-bit) and OMPython 3.3.0

Any comment will be appreciated. Thank you very much in advance.

Regards,

Fabian

Code:



model HarmonicOscillator
    output Real q1 "Position of the test mass" ;
    input Real gnd = 0.03 "Position of the ground" ;
    Real q2 "Velocity of the test mass" ;
    constant Real w0 = 2 "Resonance frequency" ;
initial equation
    q1 = 0 "Initial position of test mass" ;
    q2 = 0.05 "Initial velocity of test mass" ;
equation
    der( q1 ) = q2 ;
    der( q2 ) = - w0 * w0 * (q1 - gnd) ;   
end HarmonicOscillator ;


Code:



from OMPython import OMCSessionZMQ
from OMPython import ModelicaSystem
import matplotlib.pyplot as plt
import control

omc = OMCSessionZMQ()
# This value of C yields the correct transfer function

model_path=omc.sendExpression("cd()")
mod=ModelicaSystem(model_path + "/HarmonicOscillator.mo","HarmonicOscillator")

mod.setSimulationOptions(["stopTime=5.0","tolerance=1e-08"])
mod.setInputs(["gnd=0.03"])
mod.simulate()

print( mod.getSolutions() )
[ time , q1 , gnd ] = mod.getSolutions(["time","q1","gnd"])
plt.plot( time , q1 )
plt.plot( time , gnd )

ss_matrices = mod.linearize()
print( "Input: " , mod.getLinearInputs() )
print( "Output: " , mod.getLinearOutputs() )
print( "State Space variables: " , mod.getLinearStates() )

AA = ss_matrices[0]
BB = ss_matrices[1]
CC = ss_matrices[2]
DD = ss_matrices[3]
print( "C = " ,  CC )
ss_matrices

# CC = [ [ 1 , 0 ] ] # This value of C yields the correct transfer function.
TF = control.ss2tf( AA , BB , CC , DD )
TF

  • Index
  • » Users
  • » naibaf.omsare
  • » Profile
You are here: