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

Restricted class for parameter record with initial equation #2311

Closed
HansOlsson opened this issue Jan 24, 2019 · 6 comments
Closed

Restricted class for parameter record with initial equation #2311

HansOlsson opened this issue Jan 24, 2019 · 6 comments
Milestone

Comments

@HansOlsson
Copy link
Collaborator

Based on modelica/ModelicaStandardLibrary#1860
And related to #183

The issue is that we need the ability to create a class containing parameters and implicit equations for them, e.g.:

  class Data
    parameter Real z=1;
    parameter Real x(start=0,fixed=false);
  initial equation 
    x=cos(z*x);
  end Data;
  parameter Data d(z=4);

This is currently not possible, since parameter can only be combined with type, record, connector, and none of those specialized class may contain (initial) equations. In particular record is unsuitable since you cannot pass around Data as a record-value.

However, it is not clear that parameter as part of parameter Data is needed, and without it model Data would be perfectly ok. Will clarify.

There is also the general discussion whether class should be used. It is sort discouraged in practice, but allowed - and this could be one use and another would be documentation-only classes. There is (currently) at least one case where class itself is used: classes derived from ExternalObject (the specification also has this in the example).

An existing proposal #1345 is to specify that model is the normal modeling class (and not the same as class), and since documentation-only isn't a modeling class it wouldn't be odd that it still uses class.

@beutlich
Copy link
Member

Cross-reference: https://stackoverflow.com/q/54717649/8520615

@HansOlsson HansOlsson modified the milestone: Design99 May 15, 2019
@HansOlsson
Copy link
Collaborator Author

HansOlsson commented May 15, 2019

Language group:

  • Writing "x=(x-cos(z*x))+x" might work, but creates cyclic parameter binding
  • Algebraic equations in records (without parameter):
record Data
    Real z=1;
    Real x;
equation  
/* Or perhaps new keyword ("algebraic equation", "record equation") 
to indicate that not dependent on time*/
   x=cos(z*x);
 end Data;
parameter Data data(z=...);

Could set either z or x.
parameter Data data2=data; // Does this work? Will need more advanced type identity (nominal typing)

  • Currently legal option:
model Data
    parameter Real z=1;
    parameter Real x(start=0,fixed=false);
  initial equation 
    x=cos(z*x);
  end Data;
  Data d(z=4);
  • Related to overdetermined connectors. E.g. quaternions have one equations. Might need a more advanced class than "record".

Will need more, but should not proceed with work-arounds like "initial equation" in records.

@beutlich beutlich added this to the ModelicaSpec3.5 milestone Aug 9, 2019
@HansOlsson
Copy link
Collaborator Author

Thinking more I believe that to get this to work as expected we will need several changes. The basic issue is that even if we could create a parameter-record with equations we normally propagate parameter-records in various ways, and we need to handle that as well - and with structural type system that becomes messy. I don't see that the examples justify this.

I also realized that there is a straightforward workaround (hinted at 10 years ago).
Create a model and a record as follows:

record Data
  parameter Real z;
  parameter Real x;
end Data;

model DataInitializer
  parameter Data data(z=1, x(start=0,final fixed=false));
initial equation
 data.x=cos(data.z*data.x);
end DataInitializer;

model M
  DataInitializer init(data(z=4));
  M2 m2(data=init.data);
end M;

This is standard Modelica, and to me this solves the issue in a good way (especially if the classes have better names), and allows freely modifying m2.data afterwards. Possibly not only x.fixed, but all of x should be final to prevent misuse.

I even see a benefit: we can have multiple initializer-models that compute the parameter-record based on different approximations.

Thus I would propose that we close this without any changes.

@beutlich
Copy link
Member

  • This example seems valid Modelica, but fails to initialize in SimulationX though.
  • M2 should likely be replaced by DataInitializer.

@gkurzbach
Copy link
Collaborator

* This example seems valid Modelica, but fails to initialize in SimulationX though.

The cause for not initializing is that M.init.data.x does not become an interaton variable in the initial system for some reason. So it seems to be a tool issue.

@HansOlsson
Copy link
Collaborator Author

Seems that we can close it for now.

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

3 participants