Help - Search - Members - Calendar
Full Version: Different type of calculation for the same model?
Modelica Forum > Modelica > Modelica
jerome-gmc
Hi,


I would like to be able to run different type of calculation with the same model. i.e: according to the type of calculation some variables will "fixed = true" or "fixed=false".


So I have done that:

QUELLTEXT

parameter Integer Calculation_Type(
    min=1,
    max=3) = 1 "Choice of Calculation Type" annotation (Evaluate=true,
    choices(
     choice=1 "Type1",
     choice=2 "Type2",
     choice=3 "Type3"),
    Dialog,
    editText=false);




And, this is the pb: I would like to write a code like that:

QUELLTEXT

initial equation
if input_data.Calculation_Type== 1 then
  V_measured=V1;
Mavariable1( fixed=false,start=  10);
end if;

if input_data.Calculation_Type== 2 then
  V_measured=V2;
Mavariable2( fixed=false,start=  10);
end if;

if input_data.Calculation_Type== 33 then
  V_measured=V3;
Mavariable3( fixed=false,start=  10);
end if;


However it doesn't run, because it is not possible to "fixed=false" in initial equation?


Is anybody have a solution?



Thank you in advance wink.gif


See you...
Carsten
Hi Jerome,

put the your modifiers in the declaration part, e.g.:

QUELLTEXT
Real Mavariable2(fixed=(Calculation_Type==2), start=  10);


Onething else, the initial equation part means there are equations, which are only valid during initialization. So the code:
QUELLTEXT
Real TestVariable(start=10);


means the same as:

QUELLTEXT
Real TestVariable;

initial equation
TestVariable = 10;


Hope that helps.

Regards
Carsten
jerome-gmc
Hi Carsten,


Your response solve 80% of my problem, thanks. smile.gif

Then, do you know the syntax to declare a variable according to the value of Calculation_type?

In fact, according to the value of Calculation_type some variables exist for ==1 but do not exist for ==2.

In my case the parameter MyDiameter does not exist if Calculation_type==2, then I have 2 solutions:
---> I can declare MyDiameter if Calculation_type==2
----> Or I can make "editText=false" if Calculation_type==2

However I have not succed to do both, could you help me?


For example, a thing like that
CODE

if Calculation_type==1 then
parameter Modelica.SIunits.Diameter MyDiameter = 6e-3
Modelica.SIunits.Velocity V1 = Q/(rho*(pi/4*MyDiameter^2);
else
Modelica.SIunits.Velocity V1  = 0;
end if;



Thanks for your help.



See you.
jce
ZITAT(jerome-gmc @ Aug 24 2006, 05:01 PM) *

In fact, according to the value of Calculation_type some variables exist for ==1 but do not exist for ==2.

Hi,

one question: Does it matter (besides that it isn't really "clean" style) that there is an unused parameter in the model? The translator will remove it anyway before solving when it is used nowhere.

If it really does matter, what about a little OOP? What about making a base class, then generating the specific sub classes with different parameter sets and then making a template model which allows to choose the used sub class instead of just the calculation_type? (redeclare, etc.) Just a suggestion... smile.gif

Best regards,
Jonathan
jerome-gmc
Hi jce,

Thank you for your response.

In fact, I am going to "customize" the interface to simplify the use to the inexperienced user, that is why I would like some parameters does not exist = does not appear on the interface.

I think your response can help me, however I do not understand it. Could you give me more details. What is OOP, and you speak about sub classes: could you explain me the advantages to use them. Sorry but I am novice in modelica langage.



Regards.
jce
Hi Jerome,

no problem, I try to explain in short terms. First, OOP means just Object Oriented Programming, that means the use of classes, subclasses, inheritance, etc.

For your problem I would suggest something like this:

First, you need a base class for the calculation, e.g.:

QUELLTEXT

partial model CalcBase
  Modelica.SIunits.Velocity V1;
  // add all variables and parameters that are identical for all calculations here
end CalcBase;

Then you derive the different calculation subclasses through inheritance:

QUELLTEXT

model Calc1
  extends CalcBase;
  
  parameter Modelica.SIunits.Diameter MyDiameter = 6e-3;
  parameter Modelica.SIunits.Density rho;
  Modelica.SIunits.Heat Q;
  // add calculation specific parameters and variables here
equation
  V1 = Q/(rho*(pi/4*MyDiameter^2));
end Calc1;

and:

QUELLTEXT

model Calc2
  extends CalcBase;
equation
  V1 = 0;
end Calc2;

and finally, you write a "wrapper" class that just consists of an replaceable CalcBase object:

QUELLTEXT

model CalcChooser
  replaceable Calc1 calculation extends CalcBase annotation(choicesAllMatching);
  
  Modelica.SIunits.Velocity V;
equation
  V = calculation.V1;
end CalcChooser;


In your models you just include the wrapper class "CalcChooser" which let's you choose the applicable calculation class in the parameter dialog. The drop down list is automatically populated with all models derived from the "CalcBase" class by the annotation "choicesAllMatching"). After selecting the desired calculation, just click on the little arrow button besides the drop down list and choose "Edit". Then you are presented with the specific paramter dialog for the class you chose before. That's it! :-)

Good Luck and best regards,

Jonathan
jerome-gmc
Hi Jonathan,


I like your solution, it is a very "elegant" manner to program. wink.gif


Thank you.



See you soon


Regards.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.