Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix forward declaration of C++ Functions in FMU export
The functions may be accessed from closures, see also new solveOneNonlinearEquationTest. So far this only worked for simulations.
- Loading branch information
Showing
8 changed files
with
149 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
testsuite/openmodelica/cppruntime/fmu/modelExchange/2.0/solveOneNonlinearEquationTest.mos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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=<value>` 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 |
59 changes: 59 additions & 0 deletions
59
testsuite/openmodelica/cppruntime/solveOneNonlinearEquationTest.mos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |