Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

From OpenModelica.jl to MTK.jl #32

Open
finmod opened this issue Jun 27, 2022 · 4 comments
Open

From OpenModelica.jl to MTK.jl #32

finmod opened this issue Jun 27, 2022 · 4 comments
Assignees

Comments

@finmod
Copy link

finmod commented Jun 27, 2022

I saw your video about OpenModelica.jl, now OM.jl, and the conversion to MTK using OMBackend.jl. When I look at OMBackend here, I no longer see any mention of MTK.

I would like to convert economic models like this ModelA1.mo prototype
ModelA1.zip into MTK to continue investigations into the SciML ecosystem in Julia.

Please indicate the way to proceed.

@JKRT JKRT self-assigned this Jun 27, 2022
@JKRT
Copy link
Owner

JKRT commented Jun 27, 2022

Hi!

ModelingToolkit is imported in the backend.

(See MTK_Codegeneration.jl)

Yes, the documentation is currently lacking, some basic options are provided in the readme.
The provided model should work in principle.

I have provided a script that shows how to do it, see:
FinsModel.zip

However, I discovered two bugs with this model (In the compiler).

The first is that it seems that OMBackend does not handle start bindings for parameter variables.
That could easily be resolved by rewriting the model slightly.

The second is that there seems to be an issue with reordering the state variables.
See also SciML/ModelingToolkit.jl#860

I implemented my own solution for this, but for some reason, it is flaky in this example.

I will make two separate issues trying to solve this

Kind Regards,
John

@JKRT
Copy link
Owner

JKRT commented Jun 27, 2022

The first one I think is rather simple to solve, it is just a slight oversight on my part.
The second issue is a bit deeper, I will look into it a bit more

See issues:
#34
#33

@finmod Are you OK with your model being used as a test model?

@finmod
Copy link
Author

finmod commented Jun 27, 2022

You are welcome to use this model as a test. It will open new horizons toward DAE economic systems and there would be many interesting examples/applications in Julia to follow.

@JKRT
Copy link
Owner

JKRT commented Jul 2, 2022

ModelA1.zip
@finmod you could try this model

Based on a slightly modified variant of your previous model:

model ModelA1
  parameter Real alpha = 0.5; //(start = 0.5);
  parameter Real beta = 1.0; //(start = 1.0);
  parameter Real dpdach = 0.1; //(start = 0.1);
  parameter Real gamma = 0.5; //(start = 0.5);
  parameter Real k0 = 1.5; //(start = 1.5);
  parameter Real l0 = 1.0; //(start = 1.0);
  parameter Real ldach = 1.0; //(start = 1.0);
  parameter Real mF0 = 1.0; //(start = 1.0);
  parameter Real mH0= 1.0;
  parameter Real mHdach = 1.0;
  parameter Real muFk = 1.0;
  parameter Real muFl = 1.0;
  parameter Real muFp = 0.0; //(start = 0.0);
  parameter Real muFs = 1.0;
  parameter Real muFw = 0.0; //(start = 0.0);
  parameter Real muHcH = 1.0;
  parameter Real muHl = 1.0;
  parameter Real muHmH = 1.0;
  parameter Real p0 = 1.0;
  parameter Real s0 = 0.0; //(start = 0.0);
  parameter Real sdach = 1.0;
  parameter Real w0 = 2.0;
  Real cH;
  Real dp;
//  Real inv;
  Real k;
  Real l;
  Real lambda_1;
  Real lambda_2;
  Real lambda_3;
  Real mF;
  Real mH;
  Real p;
  Real s;
  Real uF;
  Real uH;
  Real w;
  Real y;
initial equation
  cH = k0 ^ (1 + (-1) * alpha) * l0 ^ alpha * beta;
  k = k0;
  l = l0;
  mF = mF0;
  mH = mH0;
  p = p0;
  s = s0;
  w = w0;
equation
  uF = (-1) * (sdach + (-1) * s) ^ 2 + (-1) * l * w + p * y;
  uH = cH ^ gamma + (-1) * (ldach + (-1) * l) ^ 2 + (-1) * (mHdach + (-1) * mH) ^ 2;
  dp = dpdach * k;
//  inv = der(k);
  y = beta * k ^ (1 + (-1) * alpha) * l ^ alpha;
  der(cH) = gamma * muHcH * cH ^ ((-1) + gamma) + (-1) * p * lambda_1 + p * lambda_2 + (-1) * lambda_3;
  der(k) = (1 + (-1) * alpha) * beta * muFk * k ^ ((-1) * alpha) * l ^ alpha * p + (-1) * lambda_3;
  der(l) = 2 * muHl * (ldach + (-1) * l) + muFl * (alpha * beta * k ^ (1 + (-1) * alpha) * l ^ ((-1) + alpha) * p + (-1) * w) + w * lambda_1 + (-1) * w * lambda_2 + alpha * beta * k ^ (1 + (-1) * alpha) * l ^ ((-1) + alpha) * lambda_3;
  der(mF) = (-1) * lambda_2;
  der(mH) = 2 * muHmH * (mHdach + (-1) * mH) + (-1) * lambda_1;
  der(p) = beta * muFp * k ^ (1 + (-1) * alpha) * l ^ alpha + (-1) * cH * lambda_1 + cH * lambda_2;
  der(s) = 2 * muFs * (sdach + (-1) * s) + (-1) * lambda_3;
  der(w) = (-1) * muFw * l + l * lambda_1 + (-1) * l * lambda_2;
  
  0 = (-1) * cH * p + l * w + (-1) * (-1) * lambda_2;
  0 = cH * p + (-1) * l * w + (-1) * 2 * muHmH * (mHdach + (-1) * mH) + (-1) * lambda_1;
  0 = (-1) * cH + (-1) * dpdach * k + beta * k ^ (1 + (-1) * alpha) * l ^ alpha + (-1) * ((1 + (-1) * alpha) * beta * muFk * k ^ ((-1) * alpha) * l ^ alpha * p + (-1) * lambda_3) + (-1) * (2 * muFs * (sdach + (-1) * s) + (-1) * lambda_3);
end ModelA1;

There seem to be an issue in MTK on how to represent the system where the result of a state variable is an algebraic variable. I will make an issue over there and check it out further.

A new version of the backend supporting this model here will soon be added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants