Skip to content
This repository has been archived by the owner on Dec 14, 2017. It is now read-only.

Modelica wrapper: index reduction #61

Closed
thorade opened this issue Oct 26, 2013 · 8 comments
Closed

Modelica wrapper: index reduction #61

thorade opened this issue Oct 26, 2013 · 8 comments

Comments

@thorade
Copy link

thorade commented Oct 26, 2013

If you set Hidden.PrintFailureToDifferentiate=true then you will see that simple test models like this one

model R600_TestModel "Test Butane"
  extends Modelica.Media.Examples.Tests.Components.PartialTestModel(
    redeclare package Medium = CoolProp2Modelica.Media.R600_CP);
end R600_TestModel;

have numerical Jacobians, but they still simulate. Heavier models like this one

model Branching_Dynamic_Pipes
  extends Modelica.Fluid.Examples.BranchingDynamicPipes(
    redeclare package Medium = CoolProp2Modelica.Media.R600_CP);
end Branching_Dynamic_Pipes;

will fail with error message Failed to reduce the DAE index.. This will only happen when using BaseProperties, but most models in Modelica.Fluid use them.

@thorade
Copy link
Author

thorade commented Oct 26, 2013

To fix this, this is how to use BaseProperties (using density as example):

  • BaseProperties calls density_ph
  • density_ph calls a function named e.g. density_ph_state(p=p, h=h, state=setState_ph(p=p, h=h, phase=phase)) which has the same input as density_ph plus the input ThermodynamicState state.
  • density_ph has an annotation(Inline=true); (the inlining will collect all calls to setState_ph with identical input).
  • density_ph_state finally has an annotation(LateInline=true); and annotation(derivative(noDerivative=state)=density_ph_der);

While introducing the intermediate function density_ph_state might look strange, this is the key to a) properly define the derivatives and b) avoid multiple calls to setState_ph.

@thorade
Copy link
Author

thorade commented Oct 26, 2013

PS: Should issues regarding the Modelica wrapper be filed here at coolprop?
Or at https://github.com/thermocycle/CoolProp2Modelica-library ?
Or even in the ExternalMedia issue tracker: https://trac.modelica.org/Modelica/query?component=_ExternalMedia ??

If I wanted to fix this myself, I guess I have to fork the CoolProp2Modelica-library, right? And then send a Pull request to @jowr ?

@jowr
Copy link
Collaborator

jowr commented Oct 27, 2013

Hi Matthis, @ibell tries to merge CoolProp and ExternalMedia on a C++ level. I am not sure, but he might be finished already. For now, I think it is fine to report things here. As soon as we have better integration with ExternalMedia (mid November?), the best way is to follow your second suggestion and directly contact the Modelica people via Trac: https://trac.modelica.org/Modelica/query?component=_ExternalMedia .

Sorry for the confusion, but I am not completely sure how to handle future bug reports myself.

@ibell
Copy link
Owner

ibell commented Oct 27, 2013

It's a work in progress. The limitation is that I don't know the Modelica
side of things, but I have ported our C++ code back into ExternalMedia. We
made a lot of improvements to the Modelica code, but these changes have not
been pushed back into ExternalMedia yet. I'm still waiting for news from
the other developers on the ExternalMedia side of things.

On Sun, Oct 27, 2013 at 9:52 AM, jowr notifications@github.com wrote:

Hi Matthis, @ibell https://github.com/ibell tries to merge CoolProp and
ExternalMedia on a C++ level. I am not sure, but he might be finished
already. For now, I think it is fine to report things here. As soon as we
have better integration with ExternalMedia (mid November?), the best way is
to follow your second suggestion and directly contact the Modelica people
via Trac:
https://trac.modelica.org/Modelica/query?component=_ExternalMedia .

Sorry for the confusion, but I am not completely sure how to handle future
bug reports myself.


Reply to this email directly or view it on GitHubhttps://github.com//issues/61#issuecomment-27165300
.

@ghost ghost assigned squoilin Nov 6, 2013
@jowr
Copy link
Collaborator

jowr commented Nov 27, 2013

Hi, I just tried to implement @thorade's suggestions and failed... Branching_Dynamic_Pipes still fails, but I might have missed some of the annotations. If you have the time, you can have a look at https://github.com/thermocycle/CoolProp2Modelica-library/tree/indexReduction

@thorade
Copy link
Author

thorade commented Nov 27, 2013

I forked the CoolProp2Modelica-library and did some changes, index reduction does then work as it should. You had all the tricky stuff correct, all I had to do was revert the BaseProperties back to how they were implemented in ExternalMedia: BaseProperties needs to call density_ph and so on, otherwise Dymola won't find all the annotations.
I sent you a Pull request, but you can as well just copy the code back manually.

You only added the derivative annotation for density_ph and specificEntropy_ph, that should be enough if ph is used as input. I also added the annotation to temperature_ph, but I don't remember wether it was really necessary.

It might happen that pT is used as input, then you would have to do the same for density_pT and specificEnthalpy_pT.

There was a comment about avoiding unnecessary computations, this should be handled by the Inlining annotations. So if it works as it should, you get index reduction without any disadvantages.

@jowr
Copy link
Collaborator

jowr commented Nov 28, 2013

Of course, the baseproperties. I forgot to change them, thanks.

Regarding the rest, we have to be a little careful because CoolProp does not support as many derivatives and inverse functions as HelmholtzMedia does.

Thanks again for the comments, I really appreciate that you can explain all the advanced Modelica concepts in a a nutshell.

Jorrit

Jorrit Wronski
DTU Mechanical Engineering

Technical University of Denmark
Department of Mechanical Engineering
Nils Koppels Allé
Building 403 Room 111
2800 Kgs. Lyngby
Denmark

phone: +45 45 25 41 83
fax: +45 45 88 43 25
email: jowr@mek.dtu.dk
http://www.tes.mek.dtu.dk/English

Matthis Thorade notifications@github.com wrote:

I forked the CoolProp2Modelica-library and did some changes, index reduction does then work as it should. You had all the tricky stuff correct, all I had to do was revert the BaseProperties back to how they were implemented in ExternalMedia: BaseProperties needs to call density_ph and so on, otherwise Dymola won't find all the annotations.
I sent you a Pull request, but you can as well just copy the code back manually.

You only added the derivative annotation for density_ph and specificEntropy_ph, that should be enough if ph is used as input. I also added the annotation to temperature_ph, but I don't remember wether it was really necessary.

It might happen that pT is used as input, then you would have to do the same for density_pT and specificEnthalpy_pT.

There was a comment about avoiding unnecessary computations, this should be handled by the Inlining annotations. So if it works as it should, you get index reduction without any disadvantages.


Reply to this email directly or view it on GitHubhttps://github.com//issues/61#issuecomment-29427074.

jowr pushed a commit to thermocycle/CoolProp2Modelica-library that referenced this issue Nov 28, 2013
@jowr
Copy link
Collaborator

jowr commented Nov 28, 2013

I think it's done. We only have to keep our eyes open when adding new functions...

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

No branches or pull requests

4 participants