Skip to content

Commit

Permalink
Improve Base Modelica export (#12460)
Browse files Browse the repository at this point in the history
- Add version header.
- Add experiment annotation.

Fixes #12457 and #12458
  • Loading branch information
perost committed May 23, 2024
1 parent d5dbbd9 commit c8e35cd
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFEquation.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ public
else IOStream.append(s, "#UNKNOWN EQUATION#");
end match;

s := FlatModelicaUtil.appendElementSourceComment(source(eq), s);
s := FlatModelicaUtil.appendElementSourceComment(source(eq), NFFlatModelicaUtil.ElementType.EQUATION, s);
end toFlatStream;

function toFlatStreamList
Expand Down
4 changes: 3 additions & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ public
FlatModel flat_model = flatModel;
String name = className(flatModel);
algorithm
s := IOStream.append(s, "//! base 0.1.0\n");
s := IOStream.append(s, "package '" + name + "'\n");
flat_model.variables := reconstructRecordInstances(flat_model.variables);

Expand Down Expand Up @@ -281,7 +282,8 @@ public
end if;
end for;

s := FlatModelicaUtil.appendElementSourceCommentAnnotation(flat_model.source, " ", ";\n", s);
s := FlatModelicaUtil.appendElementSourceCommentAnnotation(flat_model.source,
NFFlatModelicaUtil.ElementType.ROOT_CLASS, " ", ";\n", s);
s := IOStream.append(s, " end '" + name + "';\n");
s := IOStream.append(s, "end '" + name + "';\n");
end appendFlatStream;
Expand Down
43 changes: 39 additions & 4 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatModelicaUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ encapsulated package NFFlatModelicaUtil
import System;
import Util;

// Used to indicate what type of element an annotation comes from, to allow
// filtering out specific annotations for dumping.
type ElementType = enumeration(
ROOT_CLASS,
CLASS,
FUNCTION,
COMPONENT,
EQUATION,
ALGORITHM,
OTHER
);

function appendElementSourceCommentString
input DAE.ElementSource source;
input output IOStream.IOStream s;
Expand All @@ -51,26 +63,29 @@ encapsulated package NFFlatModelicaUtil

function appendElementSourceCommentAnnotation
input DAE.ElementSource source;
input ElementType elementType;
input String indent;
input String ending;
input output IOStream.IOStream s;
algorithm
s := appendCommentAnnotation(ElementSource.getOptComment(source), indent, ending, s);
s := appendCommentAnnotation(ElementSource.getOptComment(source), elementType, indent, ending, s);
end appendElementSourceCommentAnnotation;

function appendElementSourceComment
input DAE.ElementSource source;
input ElementType elementType;
input output IOStream.IOStream s;
algorithm
s := appendComment(ElementSource.getOptComment(source), s);
s := appendComment(ElementSource.getOptComment(source), elementType, s);
end appendElementSourceComment;

function appendComment
input Option<SCode.Comment> comment;
input ElementType elementType;
input output IOStream.IOStream s;
algorithm
s := appendCommentString(comment, s);
s := appendCommentAnnotation(comment, " ", "", s);
s := appendCommentAnnotation(comment, elementType, " ", "", s);
end appendComment;

function appendCommentString
Expand All @@ -94,6 +109,7 @@ encapsulated package NFFlatModelicaUtil

function appendCommentAnnotation
input Option<SCode.Comment> comment;
input ElementType elementType;
input String indent;
input String ending;
input output IOStream.IOStream s;
Expand All @@ -104,7 +120,10 @@ encapsulated package NFFlatModelicaUtil
case SOME(SCode.Comment.COMMENT(annotation_ =
SOME(SCode.Annotation.ANNOTATION(modification = mod))))
algorithm
mod := DAEDump.filterStructuralMods(mod);
mod := match elementType
case ElementType.ROOT_CLASS then filterRootClassAnnotations(mod);
else DAEDump.filterStructuralMods(mod);
end match;

if not SCodeUtil.isEmptyMod(mod) then
s := IOStream.append(s, indent);
Expand All @@ -119,6 +138,22 @@ encapsulated package NFFlatModelicaUtil
end match;
end appendCommentAnnotation;

function filterRootClassAnnotations
input output SCode.Mod mod;
protected
function filter
input SCode.SubMod smod;
output Boolean keep;
algorithm
keep := match smod.ident
case "experiment" then true;
else false;
end match;
end filter;
algorithm
mod := SCodeUtil.filterSubMods(mod, filter);
end filterRootClassAnnotations;

function appendAnnotationMod
input SCode.Mod mod;
input output IOStream.IOStream s;
Expand Down
6 changes: 4 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFFunction.mo
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,8 @@ uniontype Function
s := IOStream.append(s, Util.makeQuotedIdentifier(AbsynUtil.pathString(getDerivedFunctionName(fn))));
s := IOStream.append(s, ", ");
s := IOStream.append(s, stringDelimitList(getDerivedInputNames(fn), ", "));
s := FlatModelicaUtil.appendComment(SCodeUtil.getElementComment(InstNode.definition(fn.node)), s);
s := FlatModelicaUtil.appendComment(SCodeUtil.getElementComment(InstNode.definition(fn.node)),
NFFlatModelicaUtil.ElementType.FUNCTION, s);
s := IOStream.append(s, ")");
else
cmt := Util.getOptionOrDefault(SCodeUtil.getElementComment(InstNode.definition(fn.node)), SCode.COMMENT(NONE(), NONE()));
Expand Down Expand Up @@ -930,7 +931,8 @@ uniontype Function
if not SCodeUtil.emptyModOrEquality(annMod) then
cmt := SCode.COMMENT(SOME(SCode.ANNOTATION(annMod)), NONE());
s := FlatModelicaUtil.appendCommentAnnotation(SOME(cmt), indent + " ", ";\n", s);
s := FlatModelicaUtil.appendCommentAnnotation(SOME(cmt),
NFFlatModelicaUtil.ElementType.FUNCTION, indent + " ", ";\n", s);
end if;
s := IOStream.append(s, indent);
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFStatement.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ public
else IOStream.append(s, "#UNKNOWN STATEMENT#");
end match;

s := FlatModelicaUtil.appendElementSourceComment(source(stmt), s);
s := FlatModelicaUtil.appendElementSourceComment(source(stmt), NFFlatModelicaUtil.ElementType.ALGORITHM, s);
end toFlatStream;

function toFlatStreamList
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFVariable.mo
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public
end if;

s := toFlatStreamBinding(var.binding, printBindingType, s);
s := FlatModelicaUtil.appendComment(var.comment, s);
s := FlatModelicaUtil.appendComment(var.comment, NFFlatModelicaUtil.ElementType.COMPONENT, s);
end toFlatStream;

function toFlatStreamBinding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ equation
end CombineSubscripts3;

// Result:
// //! base 0.1.0
// package 'CombineSubscripts3'
// model 'CombineSubscripts3'
// Real[3] 'b.p';
Expand Down
1 change: 1 addition & 0 deletions testsuite/openmodelica/basemodelica/ArrayBinding1.mo
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ model ArrayBinding1
end ArrayBinding1;

// Result:
// //! base 0.1.0
// package 'ArrayBinding1'
// model 'ArrayBinding1'
// final parameter Real 'P' = 1.0;
Expand Down
4 changes: 3 additions & 1 deletion testsuite/openmodelica/basemodelica/Comments.mo
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ model Comments "Model to test comments in Flat Modelica output"
equation
x + y = 0 "Some equation" annotation(__A = true);
x - y = 1 "Some other equation";
annotation(version = "1.0.0");
annotation(version = "1.0.0", experiment(StopTime = 1.0));
end Comments;

// Result:
// //! base 0.1.0
// package 'Comments'
// model 'Comments' "Model to test comments in Flat Modelica output"
// Real 'x' "Some variable" annotation(Evaluate = false);
// Real 'y' "Some other variable";
// equation
// 'x' + 'y' = 0.0 "Some equation";
// 'x' - 'y' = 1.0 "Some other equation";
// annotation(experiment(StopTime = 1.0));
// end 'Comments';
// end 'Comments';
// endResult
1 change: 1 addition & 0 deletions testsuite/openmodelica/basemodelica/Expression1.mo
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ equation
end Expression1;

// Result:
// //! base 0.1.0
// package 'Expression1'
// function 'f'
// input Real[:] 'X';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ equation
end InStreamNominalThreshold;

// Result:
// //! base 0.1.0
// package 'InStreamNominalThreshold'
// function '$OMC$PositiveMax'
// input Real 'flowValue';
Expand Down
1 change: 1 addition & 0 deletions testsuite/openmodelica/basemodelica/Record1.mo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ equation
end Record1;

// Result:
// //! base 0.1.0
// package 'Record1'
// record 'R'
// Real 'x';
Expand Down
1 change: 1 addition & 0 deletions testsuite/openmodelica/basemodelica/Record2.mo
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ equation
end Record2;

// Result:
// //! base 0.1.0
// package 'Record2'
// record 'R'
// Real 'x';
Expand Down
1 change: 1 addition & 0 deletions testsuite/openmodelica/basemodelica/SD.mo
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ equation
end SD;

// Result:
// //! base 0.1.0
// package 'SD'
// model 'SD'
// parameter Integer 'N' = 3;
Expand Down
1 change: 1 addition & 0 deletions testsuite/openmodelica/basemodelica/SimpleCoolingCycle.mo
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ model SimpleCoolingCycle
end SimpleCoolingCycle;

// Result:
// //! base 0.1.0
// package 'SimpleCoolingCycle'
// function 'SimpleCoolingCycle.heatExchange_CounterFlowNTU.MediumA.specificHeatCapacityCp'
// input 'heatExchange_CounterFlowNTU.MediumA.ThermodynamicState' 'state';
Expand Down
2 changes: 1 addition & 1 deletion testsuite/openmodelica/basemodelica/Tables.mos
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ res.resultFile;
// ""
// true
// true
// "[openmodelica/basemodelica/Tables.mo:32:3-46:43:writable] Warning: Pure function ''Test33'.'Modelica.Utilities.Strings.isEmpty'' contains a call to impure function ''Test33'.'Modelica.Utilities.Strings.Advanced.skipWhiteSpace''.
// "[openmodelica/basemodelica/Tables.mo:33:3-47:43:writable] Warning: Pure function ''Test33'.'Modelica.Utilities.Strings.isEmpty'' contains a call to impure function ''Test33'.'Modelica.Utilities.Strings.Advanced.skipWhiteSpace''.
// "
// "Tables_res.mat"
// endResult

0 comments on commit c8e35cd

Please sign in to comment.