diff --git a/OMCompiler/Compiler/Template/CodegenCpp.tpl b/OMCompiler/Compiler/Template/CodegenCpp.tpl index be0dea8fd9b..2f3f2b65b40 100644 --- a/OMCompiler/Compiler/Template/CodegenCpp.tpl +++ b/OMCompiler/Compiler/Template/CodegenCpp.tpl @@ -6543,7 +6543,6 @@ case SIMCODE(modelInfo=MODELINFO(__), extObjInfo=EXTOBJINFO(__)) then #define BASECLASS ExtendedSystem #endif //Forward declaration to speed-up the compilation process - class Functions; class EventHandling; class DiscreteEvents; <%algloopForwardDeclaration(listAppend(listAppend(allEquations, initialEquations), getClockedEquations(getSubPartitions(clockedPartitions))), simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace)%> diff --git a/OMCompiler/Compiler/Template/CodegenCppOld.tpl b/OMCompiler/Compiler/Template/CodegenCppOld.tpl index 3aaf516e3f8..33de4410580 100644 --- a/OMCompiler/Compiler/Template/CodegenCppOld.tpl +++ b/OMCompiler/Compiler/Template/CodegenCppOld.tpl @@ -6770,7 +6770,6 @@ case SIMCODE(modelInfo=MODELINFO(__), extObjInfo=EXTOBJINFO(__)) then #include //Forward declaration to speed-up the compilation process - class Functions; class EventHandling; class DiscreteEvents; <%algloopForwardDeclaration(listAppend(listAppend(allEquations, initialEquations), getClockedEquations(getSubPartitions(clockedPartitions))), simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace)%> diff --git a/OMCompiler/Compiler/Template/CodegenFMUCpp.tpl b/OMCompiler/Compiler/Template/CodegenFMUCpp.tpl index 20c93821af6..51accb5f6e8 100644 --- a/OMCompiler/Compiler/Template/CodegenFMUCpp.tpl +++ b/OMCompiler/Compiler/Template/CodegenFMUCpp.tpl @@ -111,13 +111,15 @@ template fmuCalcHelperMainfile(SimCode simCode) #include #include "OMCpp<%fileNamePrefix%>Types.h" - #include "OMCpp<%fileNamePrefix%>.h" #include "OMCpp<%fileNamePrefix%>Functions.h" + #include "OMCpp<%fileNamePrefix%>.h" + #include "OMCpp<%fileNamePrefix%>Jacobian.h" #include "OMCpp<%fileNamePrefix%>Mixed.h" #include "OMCpp<%fileNamePrefix%>StateSelection.h" #include "OMCpp<%fileNamePrefix%>WriteOutput.h" #include "OMCpp<%fileNamePrefix%>Initialize.h" + #include "OMCpp<%fileNamePrefix%>FMU.h" #include "OMCpp<%fileNamePrefix%>AlgLoopMain.cpp" diff --git a/OMCompiler/Compiler/Template/CodegenFMUCppOld.tpl b/OMCompiler/Compiler/Template/CodegenFMUCppOld.tpl index 122f5c1aa7f..c7bc162b2f0 100644 --- a/OMCompiler/Compiler/Template/CodegenFMUCppOld.tpl +++ b/OMCompiler/Compiler/Template/CodegenFMUCppOld.tpl @@ -110,13 +110,15 @@ template fmuCalcHelperMainfile(SimCode simCode) #include #include "OMCpp<%fileNamePrefix%>Types.h" - #include "OMCpp<%fileNamePrefix%>.h" #include "OMCpp<%fileNamePrefix%>Functions.h" + #include "OMCpp<%fileNamePrefix%>.h" + #include "OMCpp<%fileNamePrefix%>Jacobian.h" #include "OMCpp<%fileNamePrefix%>Mixed.h" #include "OMCpp<%fileNamePrefix%>StateSelection.h" #include "OMCpp<%fileNamePrefix%>WriteOutput.h" #include "OMCpp<%fileNamePrefix%>Initialize.h" + #include "OMCpp<%fileNamePrefix%>FMU.h" #include "OMCpp<%fileNamePrefix%>AlgLoopMain.cpp" diff --git a/testsuite/openmodelica/cppruntime/Makefile b/testsuite/openmodelica/cppruntime/Makefile index b86b9ba5efb..e41fc9761c8 100644 --- a/testsuite/openmodelica/cppruntime/Makefile +++ b/testsuite/openmodelica/cppruntime/Makefile @@ -27,6 +27,7 @@ functionPointerTest.mos \ recordTupleReturnTest.mos \ RefArrayDim2.mos \ solveTest.mos \ +solveOneNonlinearEquationTest.mos \ testArrayEquations.mos \ testMatrixIO.mos \ testMatrixState.mos \ diff --git a/testsuite/openmodelica/cppruntime/fmu/modelExchange/2.0/Makefile b/testsuite/openmodelica/cppruntime/fmu/modelExchange/2.0/Makefile index 6cac3c79365..d363d5ce8f3 100644 --- a/testsuite/openmodelica/cppruntime/fmu/modelExchange/2.0/Makefile +++ b/testsuite/openmodelica/cppruntime/fmu/modelExchange/2.0/Makefile @@ -3,6 +3,7 @@ TEST = ../../../../../rtest -v TESTFILES = \ DIC_FMU2_CPP.mos \ +solveOneNonlinearEquationTest.mos \ testArrayEquations.mos \ testFMU2MatrixIO.mos \ testModelDescription.mos \ diff --git a/testsuite/openmodelica/cppruntime/fmu/modelExchange/2.0/solveOneNonlinearEquationTest.mos b/testsuite/openmodelica/cppruntime/fmu/modelExchange/2.0/solveOneNonlinearEquationTest.mos new file mode 100644 index 00000000000..87087a9647a --- /dev/null +++ b/testsuite/openmodelica/cppruntime/fmu/modelExchange/2.0/solveOneNonlinearEquationTest.mos @@ -0,0 +1,82 @@ +// name: solveOneNonlinearEquationTest +// keywords: closure +// status: correct +// teardown_command: rm -f *SolveOneNonlinearEquationTest* + +setCommandLineOptions("+simCodeTarget=Cpp"); + +loadModel(Modelica, {"3.2.3"}); +loadString(" +model SolveOneNonlinearEquationTest +\"Modelica.Media.Examples.SolveOneNonlinearEquation with input\" + import Modelica.Utilities.Streams.print; + extends Modelica.Icons.Example; + + parameter Real y_zero = 0.5 \"Desired value of A*sin(w*x)\"; + parameter Real x_min = -1.7 \"Minimum value of x_zero\"; + parameter Real x_max = 1.7 \"Maximum value of x_zero\"; + input Real A(start = 1) \"Amplitude of sine\"; + input Real w(start = 1) \"Angular frequency of sine\"; + Real x_zero \"y_zero = A*sin(w*x_zero)\"; + + function f_nonlinear \"Define sine as non-linear equation to be solved\" + extends Modelica.Math.Nonlinear.Interfaces.partialScalarFunction; + input Real A = 1 \"Amplitude of sine\"; + input Real w = 1 \"Angular frequency of sine\"; + input Real s = 0 \"Shift of sine\"; + algorithm + y := A*Modelica.Math.sin(w*u) + s; + end f_nonlinear; + +equation + x_zero = Modelica.Math.Nonlinear.solveOneNonlinearEquation( + function f_nonlinear(A=A, w=w, s=-y_zero), x_min, x_max); + annotation (experiment(StopTime=0)); +end SolveOneNonlinearEquationTest; +"); +getErrorString(); + +translateModelFMU(SolveOneNonlinearEquationTest, version = "2.0"); getErrorString(); +loadModel(Modelica, {"3.2.3"}); getErrorString(); +importFMU("SolveOneNonlinearEquationTest.fmu"); getErrorString(); +loadFile("SolveOneNonlinearEquationTest_me_FMU.mo"); getErrorString(); + +setCommandLineOptions("--simCodeTarget=C"); getErrorString(); +simulate(SolveOneNonlinearEquationTest_me_FMU, simflags="-override=A=1,w=1,stopTime=0.0"); getErrorString(); + +val(A, 0); +val(w, 0); +val(x_zero, 0); +getErrorString(); + +// Result: +// true +// true +// true +// "" +// "SolveOneNonlinearEquationTest.fmu" +// "" +// true +// "" +// "SolveOneNonlinearEquationTest_me_FMU.mo" +// "" +// true +// "" +// true +// "" +// record SimulationResult +// resultFile = "SolveOneNonlinearEquationTest_me_FMU_res.mat", +// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'SolveOneNonlinearEquationTest_me_FMU', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-override=A=1,w=1,stopTime=0.0'", +// messages = "LOG_STDOUT | warning | Start or stop time was overwritten, but no new integrator step size was provided. +// | | info | | Re-calculating step size for 500 intervals. +// | | | | | Add `stepSize=` to `-override=` or override file to silence this warning. +// LOG_SUCCESS | info | The initialization finished successfully without homotopy method. +// LOG_SUCCESS | info | The simulation finished successfully. +// " +// end SimulationResult; +// "" +// 1.0 +// 1.0 +// 0.5235987755982987 +// "" +// endResult diff --git a/testsuite/openmodelica/cppruntime/solveOneNonlinearEquationTest.mos b/testsuite/openmodelica/cppruntime/solveOneNonlinearEquationTest.mos new file mode 100644 index 00000000000..a62224d4b58 --- /dev/null +++ b/testsuite/openmodelica/cppruntime/solveOneNonlinearEquationTest.mos @@ -0,0 +1,59 @@ +// name: solveOneNonlinearEquationTest +// keywords: closure +// status: correct +// teardown_command: rm -f *SolveOneNonlinearEquationTest* + +setCommandLineOptions("+simCodeTarget=Cpp"); + +loadModel(Modelica, {"3.2.3"}); +loadString(" +model SolveOneNonlinearEquationTest +\"Modelica.Media.Examples.SolveOneNonlinearEquation with input\" + import Modelica.Utilities.Streams.print; + extends Modelica.Icons.Example; + + parameter Real y_zero = 0.5 \"Desired value of A*sin(w*x)\"; + parameter Real x_min = -1.7 \"Minimum value of x_zero\"; + parameter Real x_max = 1.7 \"Maximum value of x_zero\"; + input Real A(start = 1) \"Amplitude of sine\"; + input Real w(start = 1) \"Angular frequency of sine\"; + Real x_zero \"y_zero = A*sin(w*x_zero)\"; + + function f_nonlinear \"Define sine as non-linear equation to be solved\" + extends Modelica.Math.Nonlinear.Interfaces.partialScalarFunction; + input Real A = 1 \"Amplitude of sine\"; + input Real w = 1 \"Angular frequency of sine\"; + input Real s = 0 \"Shift of sine\"; + algorithm + y := A*Modelica.Math.sin(w*u) + s; + end f_nonlinear; + +equation + x_zero = Modelica.Math.Nonlinear.solveOneNonlinearEquation( + function f_nonlinear(A=A, w=w, s=-y_zero), x_min, x_max); + annotation (experiment(StopTime=0)); +end SolveOneNonlinearEquationTest; +"); +getErrorString(); + +simulate(SolveOneNonlinearEquationTest); +val(A, 0); +val(w, 0); +val(x_zero, 0); +getErrorString(); + +// Result: +// true +// true +// true +// "" +// record SimulationResult +// resultFile = "SolveOneNonlinearEquationTest_res.mat", +// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'SolveOneNonlinearEquationTest', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''", +// messages = "" +// end SimulationResult; +// 1.0 +// 1.0 +// 0.5235987755982987 +// "" +// endResult