- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Possible bug in OMPython's...

## Possible bug in OMPython's linearize() function.

### Possible bug in OMPython's linearize() function.

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
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Possible bug in OMPython's...