Skip to content

Commit

Permalink
Handle dassl better in opc-ua
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed May 20, 2017
1 parent 7b440a1 commit 16a7626
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
9 changes: 5 additions & 4 deletions SimulationRuntime/c/simulation/solver/embedded_server.c
Expand Up @@ -42,7 +42,7 @@
#define DLL_EXT ".so"
#endif

void* no_embedded_server_init(DATA *data, double tout, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port, int *didEventStep)
void* no_embedded_server_init(DATA *data, double tout, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port)
{
return NULL;
}
Expand All @@ -51,15 +51,16 @@ void no_embedded_server_deinit(void *handle)
{
}

void no_embedded_server_update(void *handle, double tout)
int no_embedded_server_update(void *handle, double tout)
{
return 0;
}

void* (*embedded_server_init)(DATA *data, double tout, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port, int *didEventStep) = no_embedded_server_init;
void* (*embedded_server_init)(DATA *data, double tout, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port) = no_embedded_server_init;
void (*embedded_server_deinit)(void*) = no_embedded_server_deinit;
// Tells the embedded server that a simulation step has passed; the server
// can read/write values from/to the simulator
void (*embedded_server_update)(void*, double tout) = no_embedded_server_update;
int (*embedded_server_update)(void*, double tout) = no_embedded_server_update;

void* embedded_server_load_functions(const char *server_name)
{
Expand Down
4 changes: 2 additions & 2 deletions SimulationRuntime/c/simulation/solver/embedded_server.h
Expand Up @@ -37,12 +37,12 @@
extern "C" {
#endif

extern void* (*embedded_server_init)(DATA *data, double tout, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port, int *didEventStep);
extern void* (*embedded_server_init)(DATA *data, double tout, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port);
extern void (*embedded_server_deinit)(void *handle);
/* Tells the embedded server that a simulation step has passed; the server
* can read/write values from/to the simulator
*/
extern void (*embedded_server_update)(void *handle, double tout);
extern int (*embedded_server_update)(void *handle, double tout);
/* Give the filename or generic name to use for loading an embedded server */
extern void* embedded_server_load_functions(const char *name);
extern void embedded_server_unload_functions(void *dllHandle);
Expand Down
14 changes: 10 additions & 4 deletions SimulationRuntime/c/simulation/solver/perform_simulation.c
Expand Up @@ -183,7 +183,7 @@ static void fmtInit(DATA* data, MEASURE_TIME* mt)
}
}

static void fmtEmitStep(DATA* data, threadData_t *threadData, MEASURE_TIME* mt, int didEventStep)
static void fmtEmitStep(DATA* data, threadData_t *threadData, MEASURE_TIME* mt, SOLVER_INFO* solverInfo)
{
if(mt->fmtReal)
{
Expand Down Expand Up @@ -218,11 +218,17 @@ static void fmtEmitStep(DATA* data, threadData_t *threadData, MEASURE_TIME* mt,
}

/* prevent emit if noEventEmit flag is used, if it's an event */
if ((omc_flag[FLAG_NOEVENTEMIT] && didEventStep == 0) || !omc_flag[FLAG_NOEVENTEMIT]) {
if ((omc_flag[FLAG_NOEVENTEMIT] && solverInfo->didEventStep == 0) || !omc_flag[FLAG_NOEVENTEMIT]) {
sim_result.emit(&sim_result, data, threadData);
}
#if !defined(OMC_MINIMAL_RUNTIME)
embedded_server_update(data->embeddedServerState, data->localData[0]->timeValue);
if (embedded_server_update(data->embeddedServerState, data->localData[0]->timeValue)) {
solverInfo->didEventStep = 1;
overwriteOldSimulationData(data);
storePreValues(data); // Maybe??
storeOldValues(data); // Maybe??
sim_result.emit(&sim_result, data, threadData);
}
if (data->real_time_sync.enabled) {
double time = data->localData[0]->timeValue;
int64_t res = rt_ext_tp_sync_nanosec(&data->real_time_sync.clock, (uint64_t) (data->real_time_sync.scaling*(time-data->real_time_sync.time)*1e9));
Expand Down Expand Up @@ -455,7 +461,7 @@ int prefixedName_performSimulation(DATA* data, threadData_t *threadData, SOLVER_
syncStep = simulationUpdate(data, threadData, solverInfo);
retry = 0; /* reset retry */

fmtEmitStep(data, threadData, &fmt, solverInfo->didEventStep);
fmtEmitStep(data, threadData, &fmt, solverInfo);
saveIntegratorStats(solverInfo);
checkSimulationTerminated(data, solverInfo);

Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/simulation/solver/solver_main.c
Expand Up @@ -705,7 +705,7 @@ int solver_main(DATA* data, threadData_t *threadData, const char* init_initMetho
}
}
}
data->embeddedServerState = embedded_server_init(data, data->localData[0]->timeValue, solverInfo.currentStepSize, argv_0, omc_real_time_sync_update, port, &solverInfo.didEventStep);
data->embeddedServerState = embedded_server_init(data, data->localData[0]->timeValue, solverInfo.currentStepSize, argv_0, omc_real_time_sync_update, port);
#endif
if(0 == retVal) {
retVal = -1;
Expand Down
17 changes: 8 additions & 9 deletions SimulationRuntime/opc/ua/omc_opc_ua.c
Expand Up @@ -58,7 +58,6 @@ typedef struct {
int *boolValsInputIndex;
int reinitStateFlag;
int *stateWasUpdatedFlag;
int *didEventStep;
double *updatedStates;
double real_time_sync_scaling;
void (*omc_real_time_sync_update)(DATA *data, double scaling);
Expand Down Expand Up @@ -449,7 +448,7 @@ static inline omc_opc_ua_state* addAliasVars(omc_opc_ua_state *state, var_kind_t
}
return state;
}
void* omc_embedded_server_init(DATA *data, double t, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port, int *didEventStep)
void* omc_embedded_server_init(DATA *data, double t, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port)
{
MODEL_DATA *modelData = data->modelData;
omc_opc_ua_state *state = (omc_opc_ua_state*) malloc(sizeof(omc_opc_ua_state));
Expand All @@ -467,7 +466,6 @@ void* omc_embedded_server_init(DATA *data, double t, double step, const char *ar
state->server_running = 1;
state->time = t;
state->omc_real_time_sync_update = omc_real_time_sync_update;
state->didEventStep = didEventStep;

pthread_cond_init(&state->cond_pause, NULL);
pthread_mutex_init(&state->mutex_pause, NULL);
Expand Down Expand Up @@ -620,13 +618,15 @@ void omc_embedded_server_deinit(void *state_vp)
free(state);
}

void omc_embedded_server_update(void *state_vp, double t)
int omc_embedded_server_update(void *state_vp, double t)
{
omc_opc_ua_state *state = (omc_opc_ua_state*) state_vp;
int i, realIndex=0, boolIndex=0;
int i, realIndex=0, boolIndex=0, res=0;
DATA *data = state->data;
MODEL_DATA *modelData = data->modelData;

waitForStep(state);

pthread_rwlock_wrlock(&state->rwlock);

state->time = t;
Expand All @@ -639,12 +639,12 @@ void omc_embedded_server_update(void *state_vp, double t)
}

if (state->gotNewInput) {
*state->didEventStep = 1; /* Trigger an event in the solver, restarting it */
res = 1; /* Trigger an event in the solver, restarting it */
memcpy(data->simulationInfo->inputVars, state->inputVarsBackup, modelData->nInputVars * sizeof(double));
}

if (state->reinitStateFlag) {
*state->didEventStep = 1; /* Trigger an event in the solver, restarting it */
res = 1; /* Trigger an event in the solver, restarting it */
for (i = 0; i < modelData->nStates; i++) {
if (state->stateWasUpdatedFlag[i]) {
state->stateWasUpdatedFlag[i] = 0;
Expand All @@ -654,6 +654,5 @@ void omc_embedded_server_update(void *state_vp, double t)
}

pthread_rwlock_unlock(&state->rwlock);

waitForStep(state);
return res;
}
4 changes: 2 additions & 2 deletions SimulationRuntime/opc/ua/omc_opc_ua.h
Expand Up @@ -47,9 +47,9 @@
#endif
#endif

OPC_UA_EXPORT void* omc_embedded_server_init(DATA *data, double t, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port, int *didEventStep);
OPC_UA_EXPORT void* omc_embedded_server_init(DATA *data, double t, double step, const char *argv_0, void (*omc_real_time_sync_update)(DATA *data, double scaling), int port);
OPC_UA_EXPORT void omc_embedded_server_deinit(void*);
OPC_UA_EXPORT void omc_embedded_server_update(void*, double t);
OPC_UA_EXPORT int omc_embedded_server_update(void*, double t);

#define OMC_OPC_NODEID_STEP 10000
#define OMC_OPC_NODEID_RUN 10001
Expand Down

0 comments on commit 16a7626

Please sign in to comment.