Skip to content

Commit

Permalink
Refactoring non-linear solvers (#9065)
Browse files Browse the repository at this point in the history
### Unified user data structs for all non-linear solvers:
```C
typedef struct NLS_USERDATA {
  DATA *data;
  threadData_t *threadData;

  int sysNumber;                        /* System index, for print messages only */
  NONLINEAR_SYSTEM_DATA* nlsData;       /* Pointer to nonlinear system data */
  ANALYTIC_JACOBIAN* analyticJacobian;  /* Pointer to analytic Jacobian */
} NLS_USERDATA;
```

Contains pointer to `nlsData` that contains this user data. Used to hide omc internal data from 3rdParty function calls.
userData will be passed to residual and Jacobian functions.

Use `initNlsUserData` and `freeNlsUserData` for memory management.

### Simplify NLS functions interface

- `int allocateNewtonData(int size, void** voiddata)` --> `DATA_NEWTON* allocateNewtonData(int size, NLS_USERDATA* userData)`
- `int nlsKinsolFree(void **solverData)` --> `void freeNewtonData(DATA_NEWTON* newtonData)`
- `int solveNLS(DATA *data, threadData_t *threadData, int sysNumber)` --> `modelica_boolean solveNLS(DATA *data, threadData_t *threadData, NONLINEAR_SYSTEM_DATA* nonlinsys)`
- `int _omc_newton(int(*f)(int*, double*, double*, void*, int), DATA_NEWTON* solverData, void* userdata);` --> `int _omc_newton(genericResidualFunc f, DATA_NEWTON* solverData, void* userData);`
- `void printNonLinearSystemSolvingStatistics(DATA *data, int sysNumber, int logLevel)` --> `void printNonLinearSystemSolvingStatistics(NONLINEAR_SYSTEM_DATA* nonlinsys, enum LOG_STREAM stream)`

### Other

- Added more documentation.
- Fixed memory leaks while freeing NLS data.
- Removed unused `currentNonlinearSystemIndex` from `NONLINEAR_SYSTEM_DATA`.
- Added `TRUE` and `FALSE` to openmodelcia_types.h, next to definition of type `modelica_boolean`.
- typedef `genericResidualFunc` function pointer.
  • Loading branch information
AnHeuermann committed Jun 10, 2022
1 parent dbf37cf commit a983f48
Show file tree
Hide file tree
Showing 24 changed files with 1,610 additions and 1,501 deletions.
8 changes: 8 additions & 0 deletions OMCompiler/SimulationRuntime/c/openmodelica_types.h
Expand Up @@ -139,6 +139,14 @@ typedef struct base_array_s base_array_t;
typedef base_array_t string_array_t;

typedef signed char modelica_boolean;
#ifndef FALSE
#define FALSE 0
#endif

#ifndef TRUE
#define TRUE 1
#endif

typedef base_array_t boolean_array_t;

typedef double modelica_real;
Expand Down
11 changes: 0 additions & 11 deletions OMCompiler/SimulationRuntime/c/simulation/solver/cvode_solver.c
Expand Up @@ -48,19 +48,8 @@
#include "epsilon.h"



#ifdef WITH_SUNDIALS

/* Macros for better readability */

#ifndef FALSE
#define FALSE 0
#endif

#ifndef TRUE
#define TRUE 1
#endif

#define CVODE_LMM_MAX 2
const char *CVODE_LMM_NAME[CVODE_LMM_MAX + 1] = {
"undefined",
Expand Down
2 changes: 2 additions & 0 deletions OMCompiler/SimulationRuntime/c/simulation/solver/dassl.c
Expand Up @@ -495,6 +495,8 @@ int printCurrentStatesVector(int logLevel, double* states, DATA* data, double ti
return 0;
}

// TODO AHeu: Remove millionths definition of a simple vector print function

/* \fn printVector(int logLevel, double* y, DATA* data, double time)
*
* \param [in] [logLevel]
Expand Down
13 changes: 0 additions & 13 deletions OMCompiler/SimulationRuntime/c/simulation/solver/ida_solver.h
Expand Up @@ -55,19 +55,6 @@
/* readability */
#define MINIMAL_SCALE_FACTOR 1e-8

#ifndef booleantype
#define booleantype int
#endif

#ifndef FALSE
#define FALSE 0
#endif

#ifndef TRUE
#define TRUE 1
#endif


typedef struct IDA_USERDATA
{
DATA* data;
Expand Down

0 comments on commit a983f48

Please sign in to comment.