Skip to content

Commit

Permalink
prevent throw for division by zero in non-linear system solver, sinc…
Browse files Browse the repository at this point in the history
…e the solver can manage that.

 


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13934 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Nov 17, 2012
1 parent 234451a commit d6c1fa4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
7 changes: 7 additions & 0 deletions SimulationRuntime/c/simulation/solver/nonlinearSystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ int solve_nonlinear_system(DATA *data, int sysNumber)

data->simulationInfo.currentNonlinearSystemIndex = sysNumber;

/* enable to avoid division by zero */
data->simulationInfo.noThrowDivZero = 1;

/* strategy for solving nonlinear system
*
*
Expand All @@ -127,6 +130,10 @@ int solve_nonlinear_system(DATA *data, int sysNumber)
success = solveHybrd(data, sysNumber);
nonlinsys[sysNumber].solved = success;

/* enable to avoid division by zero */
data->simulationInfo.noThrowDivZero = 0;


return 0;
}

Expand Down
17 changes: 9 additions & 8 deletions SimulationRuntime/c/simulation_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,16 @@
modelica_string outputFormat;
modelica_string variableFilter;

modelica_boolean initial; /* =1 during initialization, 0 otherwise. */
modelica_boolean terminal; /* =1 at the end of the simulation, 0 otherwise. */
modelica_boolean discreteCall; /* =1 for a discrete step, otherwise 0 */
modelica_boolean needToIterate; /* =1 if reinit has been activated, iteration about the system is needed */
/* indicators for simulations state */
modelica_boolean initial; /* =1 during initialization, 0 otherwise. */
modelica_boolean terminal; /* =1 at the end of the simulation, 0 otherwise. */
modelica_boolean discreteCall; /* =1 for a discrete step, otherwise 0 */
modelica_boolean needToIterate; /* =1 if reinit has been activated, iteration about the system is needed */
modelica_boolean simulationSuccess; /*=0 the simulation run successful, otherwise an error code is set */
modelica_boolean sampleActivated; /* =1 a sample expresion if going to be actived, 0 otherwise */
modelica_boolean solveContinuous; /* =1 for the first step to initialize all relation, 0 otherwise. */
modelica_boolean sampleActivated; /* =1 a sample expresion if going to be actived, 0 otherwise */
modelica_boolean solveContinuous; /* =1 during the continuous integration to avoid zero-crossings jums, 0 otherwise. */
modelica_boolean noThrowDivZero; /* =1 if solving nonlinear system to avoid THROW for division by zero, 0 otherwise. */
modelica_boolean found_solution; /* helper for mixed systems */

void** extObjs; /* External objects */

Expand Down Expand Up @@ -384,8 +387,6 @@
NONLINEAR_SYSTEM_DATA* nonlinearSystemData;
int currentNonlinearSystemIndex;

int found_solution; /* helper for mixed systems */

/* delay vars */
double tStart;
RINGBUFFER **delayStructure;
Expand Down
26 changes: 21 additions & 5 deletions SimulationRuntime/c/util/division.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,33 @@
#include "division.h"
#include "omc_error.h"

modelica_real division_error_time(modelica_real b, const char* division_str, modelica_real time, const char* file, long line)
modelica_real division_error_time(modelica_real b, const char* division_str, modelica_real time, const char* file, long line, modelica_boolean noThrow)
{
WARNING1(LOG_STDOUT, "division by zero in partial equation: %s", division_str);
WARNING1(LOG_STDOUT, "at Time=%f", time);
WARNING2(LOG_STDOUT, "[line] %ld | [file] %s", line, file);
if (noThrow){
WARNING1(LOG_UTIL, "division by zero in partial equation: %s", division_str);
WARNING1(LOG_UTIL, "at Time=%f", time);
WARNING(LOG_UTIL, "solver will try to handle that.");
} else {
WARNING1(LOG_STDOUT, "division by zero in partial equation: %s", division_str);
WARNING1(LOG_STDOUT, "at Time=%f", time);
WARNING2(LOG_STDOUT, "[line] %ld | [file] %s", line, file);
#ifndef __APPLE_CC__
THROW("division by zero");
THROW("division by zero");
#endif
}
return b;
}

modelica_real division_warning_time(modelica_real b, const char* division_str, modelica_real time, const char* file, long line)
{
WARNING1(LOG_STDOUT, "division by zero in partial equation: %s", division_str);
WARNING1(LOG_STDOUT, "at Time=%f", time);

return b;
}



modelica_real division_error(modelica_real b, const char* division_str, const char* file, long line)
{
WARNING1(LOG_STDOUT, "division by zero in partial equation: %s", division_str);
Expand Down
4 changes: 2 additions & 2 deletions SimulationRuntime/c/util/division.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
#ifdef CHECK_NAN
#define DIVISION(a,b,c) (((b) != 0) ? (isnan_error(((a) / (b)), c, __FILE__, __LINE__)) : ((a) / division_error(b, c, __FILE__, __LINE__)))
#else
#define DIVISION(a,b,c) (((b) != 0) ? ((a) / (b)) : ((a) / division_error_time(b, c, time, __FILE__, __LINE__)))
#define DIVISION(a,b,c) (((b) != 0) ? ((a) / (b)) : ((a) / division_error_time(b, c, time, __FILE__, __LINE__,data->simulationInfo.noThrowDivZero?1:0)))
#endif
#define DIVISIONNOTIME(a,b,c) (((b) != 0) ? ((a) / (b)) : ((a) / division_error(b, c, __FILE__, __LINE__)))

modelica_real division_error_time(modelica_real b, const char* division_str, modelica_real time, const char* file, long line);
modelica_real division_error_time(modelica_real b, const char* division_str, modelica_real time, const char* file, long line, modelica_boolean noThrow);
modelica_real division_error(modelica_real b, const char* division_str, const char* file, long line);
modelica_real isnan_error(modelica_real b, const char* division_str, const char* file, long line);

Expand Down

0 comments on commit d6c1fa4

Please sign in to comment.