Christoph Höger
christoph.hoeger@tu-berlin.de
Technische Universität Berlin
19.04.2013
What is this guy talking about?
Difficulties & solutions.
Finally getting a plot.
A few facts and figures.
Room for your questions.
Contrary to popular belief, most current implementations do not compile Modelica.
[Given language semantics ⟦⟧L,S,T] Formally, c is an S-to-T compiler written in L, if for all sources, d ∈ D:
⟦⟦c⟧L source⟧T d = ⟦source⟧S d
N. Jones, Partial Evaluation
Depending on the model, the user might provide e.g. structural parameters:
model A parameter Real p; Real x if p > 0; end A;
When compiling, the target language usually is in some sense simple than the target language:
In case of Modelica (or any other equation based modeling language), we want to translate equations and models into something more "computational".
This talk will focus largely on model instantiation.
Compilation to Java is a novelty for Modelica. This was a (successful) experiment to determine, whether the JVM can actually host Modelica.
Reasons for Java:
In fact, also LLVM or even x86 assembler would be suitable, but require some additional work.
Modelica's objects can be used in contexts that expect a smaller record:
record A Real x; end A; record B Real x,y; end B; function f input A a; output Real y = a.x; end f; Real z = f(B(1.0, 0.0)); //legal!
class JB extends MObject { Object x, y; ... Object get(final String name) { switch(name) { case "x": return x; case "y": return y; case default: throw new IllegalArgException(name); } } }
The success of get can be guaranteed by static typechecking.
Modelica's classes can inherit from multiple sources:
record A Real x; end A; record B Real y; end B; record C extends A; extends B; end C; C c(x=1.0, y=0.0); Real z = c.y;
class JC extends MObject { JA superclass1; JB superclass2; ... Object get(final String name) { switch(name) { case "x": return superclass1.x; case "y": return superclass2.y; case default: throw new IllegalArgException(name); } } }
Fortunately, super classes cannot be redeclared in Modelica!
Fields of Modelica's classes can be defined on the creation site:
record A Real x; end A; record B Real x,y; end B; record C A a(x=1.0)); end C; C c(a = B(x=2.0, y=0.0))); Real z = c.x; //2.0
class JC extends MObject { MObject a; public JC(MObject ctxt, ...) { this.a = (MObject) ctxt. getOrEval("a", JA. CREATE_NEW.apply(1.0))); ... } }
Every class takes a context parameter (holding all modifications). In case the ctxt does not provide a value, the default constructor is called.
Type applications are reflected at object level
record A Real x=1.0; end A; record B Real x=2.0,y=1.0; end B; record C replaceble record R = A; A a; end C; C c(redeclare record R = B); Real z = c.x; //2.0
class JC extends MObject { MFunction create_a; public JC(MObject ctxt, ...) { this.create_a = (MFunction) ctxt.getOrElse("$create_a", JA.CREATE_NEW); this.a = create_a.apply(); ... } }
Every type is translated into an instantiation function. Type redeclaration is translated into a modification of this function.
Modelica contains rather complicated type-expressions:
In particular, there is no specific order of type evaluation specified in Modelica.
Modim evaluates types lazily:
case class NamedType(name : List[String], expr : TExp, context : Either[NamedType, Map[String, Type]], sort : Sort = Class, actualParams : Map[String, Type] = Map()) extends Type
Simulation is not the main topic of this talk, but a few hints:
Modim (Modelica Incremental Modules) is a toolchain for the separate compilation of Modelica models
Usable
Work in progress
To be done
Modim
Frontend
Abstract Syntax
Parser
Type Evaluator
Type Checker
Coder
Models
Functions
Equations
Relations
Runtime
Instantiation
Simulation
User Interface
Type Inspection
Simulation
Verification
model Faculty "recurses n! times" constant Integer n; Faculty[n] faculties(each n = n - 1) if (n > 1); Real root if n == 1; end Faculty;
This model is an instantiation stress-test:
Compiled to Java we can instantiate an insane amount of instances:
n | variables | instances | time |
---|---|---|---|
1 | 1 | 1 | 15ms |
2 | 2 | 3 | 20ms |
3 | 6 | 10 | 1 |
4 | 24 | 41 | 2ms |
5 | 120 | 206 | 10ms |
6 | 720 | 1237 | 60ms |
7 | 5040 | 8660 | 308ms |
8 | 40320 | 69281 | 476ms |
9 | 362880 | 623530 | 873ms |
/
#