From 1d26a1aa04a0ee67cb9bd79d92f72776022cebf0 Mon Sep 17 00:00:00 2001 From: phannebohm Date: Fri, 5 Apr 2024 16:53:44 +0200 Subject: [PATCH] [C] Constrain nls iteration variables (#12201) --- .../c/simulation/solver/nonlinearSolverHybrd.c | 4 ++++ .../Modelica.Fluid.Examples.Tanks.TanksWithOverflow.mos | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/OMCompiler/SimulationRuntime/c/simulation/solver/nonlinearSolverHybrd.c b/OMCompiler/SimulationRuntime/c/simulation/solver/nonlinearSolverHybrd.c index ff0a4c1c58b..d66823bce86 100644 --- a/OMCompiler/SimulationRuntime/c/simulation/solver/nonlinearSolverHybrd.c +++ b/OMCompiler/SimulationRuntime/c/simulation/solver/nonlinearSolverHybrd.c @@ -498,6 +498,10 @@ NLS_SOLVER_STATUS solveHybrd(DATA *data, threadData_t *threadData, NONLINEAR_SYS /* start solving loop */ while(!giveUp && !success) { + /* constrain x */ + for(i=0; in; i++) + hybrdData->x[i] = fmax(nlsData->min[i], fmin(hybrdData->x[i], nlsData->max[i])); + for(i=0; in; i++) hybrdData->xScalefactors[i] = fmax(fabs(hybrdData->x[i]), nlsData->nominal[i]); diff --git a/testsuite/simulation/libraries/msl32/Modelica.Fluid.Examples.Tanks.TanksWithOverflow.mos b/testsuite/simulation/libraries/msl32/Modelica.Fluid.Examples.Tanks.TanksWithOverflow.mos index 88cfc6d729c..041556f4562 100644 --- a/testsuite/simulation/libraries/msl32/Modelica.Fluid.Examples.Tanks.TanksWithOverflow.mos +++ b/testsuite/simulation/libraries/msl32/Modelica.Fluid.Examples.Tanks.TanksWithOverflow.mos @@ -28,12 +28,15 @@ runScript(modelTesting);getErrorString(); // OpenModelicaModelTesting.Kind.SimpleSimulation // Modelica.Fluid.Examples.Tanks.TanksWithOverflow // {"upperTank.level","upperTank.medium.h","lowerTank.level","lowerTank.medium.h"} -// Simulation options: startTime = 0.0, stopTime = 25000.0, numberOfIntervals = 5000, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'Modelica.Fluid.Examples.Tanks.TanksWithOverflow', options = '', outputFormat = 'mat', variableFilter = 'time|upperTank.level|upperTank.medium.h|lowerTank.level|lowerTank.medium.h', cflags = '', simflags = ' -abortSlowSimulation -alarm=360 -emit_protected' +// Simulation options: startTime = 0.0, stopTime = 25000.0, numberOfIntervals = 5000, tolerance = 1e-6, method = 'dassl', fileNamePrefix = 'Modelica.Fluid.Examples.Tanks.TanksWithOverflow', options = '', outputFormat = 'mat', variableFilter = 'time|upperTank.level|upperTank.medium.h|lowerTank.level|lowerTank.medium.h', cflags = '', simflags = ' -abortSlowSimulation -alarm=360 -emit_protected' // Result file: Modelica.Fluid.Examples.Tanks.TanksWithOverflow_res.mat // Messages: LOG_ASSERT | warning | [Modelica 3.2.1+maint.om/Media/package.mo:5259:7-5259:55:writable] // | | | | The following assertion has been violated at time 0.000000 -// | | | | ((overflow.flowModel.states[1].p >= 0.0 and overflow.flowModel.states[1].p <= 100000000.0)) --> "Variable violating min/max constraint: 0.0 <= overflow.flowModel.states[1].p <= 100000000.0, has value: -93465.5" +// | | | | ((overflow.flowModel.states[1].p >= 0.0 and overflow.flowModel.states[1].p <= 1e8)) --> "Variable violating min/max constraint: 0.0 <= overflow.flowModel.states[1].p <= 1e8, has value: -93465.5" // LOG_SUCCESS | info | The initialization finished successfully with 3 homotopy steps. +// LOG_ASSERT | info | [Modelica 3.2.1+maint.om/Fluid/Interfaces.mo:15:5-15:79:writable] +// | | | | The following assertion has been violated at time 0.325042 +// | | | | ((lowerTank.ports[2].p >= 0.0 and lowerTank.ports[2].p <= 1e8)) --> "Variable violating min/max constraint: 0.0 <= lowerTank.ports[2].p <= 1e8, has value: -419515" // LOG_SUCCESS | info | The simulation finished successfully. // // "true