Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make number of continuous states and event indicators variable #431

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions BouncingBall/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
#define CO_SIMULATION
#define MODEL_EXCHANGE

// define model size
#define NX 2
#define NZ 1
#define HAS_CONTINUOUS_STATES
#define HAS_EVENT_INDICATORS

#define SET_FLOAT64
#define GET_OUTPUT_DERIVATIVE
Expand Down
13 changes: 10 additions & 3 deletions BouncingBall/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ void eventUpdate(ModelInstance *comp) {
M(g) = 0;
}

// reset previous event indicators
getEventIndicators(comp, comp->z, NZ);

comp->valuesOfContinuousStatesChanged = true;
} else {
comp->valuesOfContinuousStatesChanged = false;
Expand All @@ -146,6 +143,16 @@ void eventUpdate(ModelInstance *comp) {
comp->nextEventTimeDefined = false;
}

size_t getNumberOfEventIndicators(ModelInstance* comp) {
UNUSED(comp);
return 1;
}

size_t getNumberOfContinuousStates(ModelInstance* comp) {
UNUSED(comp);
return 2;
}

void getContinuousStates(ModelInstance *comp, double x[], size_t nx) {
UNUSED(nx);
x[0] = M(h);
Expand Down
4 changes: 0 additions & 4 deletions Clocks/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@

#define DEFAULT_STOP_TIME 3

// define model size
#define NX 0
#define NZ 0

#define EVENT_UPDATE
#define ACTIVATE_CLOCK
#define GET_INT32
Expand Down
4 changes: 1 addition & 3 deletions Dahlquist/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
#define CO_SIMULATION
#define MODEL_EXCHANGE

// define model size
#define NX 1
#define NZ 0
#define HAS_CONTINUOUS_STATES

#define SET_FLOAT64

Expand Down
5 changes: 5 additions & 0 deletions Dahlquist/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ Status setFloat64(ModelInstance* comp, ValueReference vr, const double values[],
}
}

size_t getNumberOfContinuousStates(ModelInstance* comp) {
UNUSED(comp);
return 1;
}

void getContinuousStates(ModelInstance *comp, double x[], size_t nx) {
UNUSED(nx);
x[0] = M(x);
Expand Down
3 changes: 0 additions & 3 deletions Feedthrough/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
#define CO_SIMULATION
#define MODEL_EXCHANGE

#define NX 0
#define NZ 0

#define GET_FLOAT32
#define GET_FLOAT64
#define GET_INT8
Expand Down
1 change: 0 additions & 1 deletion Feedthrough/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ Status setUInt16(ModelInstance* comp, ValueReference vr, const uint16_t values[]
return OK;
}


Status setInt32(ModelInstance* comp, ValueReference vr, const int32_t values[], size_t nValues, size_t* index) {

ASSERT_NVALUES(1);
Expand Down
4 changes: 0 additions & 4 deletions LinearTransform/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
#define MODEL_IDENTIFIER LinearTransform
#define INSTANTIATION_TOKEN "{D773325B-AB94-4630-BF85-643EB24FCB78}"

// define model size
#define NX 0
#define NZ 0

#define CO_SIMULATION
#define MODEL_EXCHANGE

Expand Down
4 changes: 0 additions & 4 deletions Resource/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
#define CO_SIMULATION
#define MODEL_EXCHANGE

// define model size
#define NX 0
#define NZ 0

#define GET_INT32

#define FIXED_SOLVER_STEP 1
Expand Down
4 changes: 0 additions & 4 deletions Stair/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
#define CO_SIMULATION
#define MODEL_EXCHANGE

// define model size
#define NX 0
#define NZ 0

#define GET_INT32
#define EVENT_UPDATE

Expand Down
4 changes: 1 addition & 3 deletions VanDerPol/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
#define CO_SIMULATION
#define MODEL_EXCHANGE

// define model size
#define NX 2
#define NZ 0
#define HAS_CONTINUOUS_STATES

#define SET_FLOAT64

Expand Down
5 changes: 5 additions & 0 deletions VanDerPol/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ Status setFloat64(ModelInstance* comp, ValueReference vr, const double values[],
}
}

size_t getNumberOfContinuousStates(ModelInstance* comp) {
UNUSED(comp);
return 2;
}

void getContinuousStates(ModelInstance *comp, double x[], size_t nx) {
UNUSED(nx);
x[0] = M(x0);
Expand Down
20 changes: 10 additions & 10 deletions examples/jacobian.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "util.h"

#define NX 2

int main(int argc, char* argv[]) {

Expand All @@ -12,9 +13,8 @@ int main(int argc, char* argv[]) {
size_t i, j;
fmi3Float64 time = 0;

size_t nx = NX;
fmi3ValueReference vr_x[] = { vr_x0, vr_x1 };
fmi3ValueReference vr_dx[] = { vr_der_x0, vr_der_x1 };
fmi3ValueReference vr_x[NX] = { vr_x0, vr_x1 };
fmi3ValueReference vr_dx[NX] = { vr_der_x0, vr_der_x1 };

// variables:
fmi3Float64 x[NX];
Expand All @@ -34,7 +34,7 @@ int main(int argc, char* argv[]) {

CALL(FMI3EnterContinuousTimeMode(S));

CALL(FMI3GetContinuousStates(S, x, nx));
CALL(FMI3GetContinuousStates(S, x, NX));

// tag::JacobianVariables[]
// from the XML file:
Expand All @@ -54,14 +54,14 @@ int main(int argc, char* argv[]) {

// set time, states and inputs
CALL(FMI3SetTime(S, time));
CALL(FMI3SetContinuousStates(S, x, nx));
CALL(FMI3SetContinuousStates(S, x, NX));
// fmi3Set{VariableType}(s, ...)

// if required at this step, compute the Jacobian as a dense matrix
for (i = 0; i < nx; i++) {
for (i = 0; i < NX; i++) {
// construct the Jacobian matrix column wise
CALL(FMI3GetDirectionalDerivative(S, vr_dx, nx, &vr_x[i], 1, &dk, 1, c, nx));
for (j = 0; j < nx; j++) {
CALL(FMI3GetDirectionalDerivative(S, vr_dx, NX, &vr_x[i], 1, &dk, 1, c, NX));
for (j = 0; j < NX; j++) {
J[j][i] = c[j];
}
}
Expand All @@ -73,9 +73,9 @@ int main(int argc, char* argv[]) {
assert(J[1][1] == -3);

// tag::GetJacobianAdjoint[]
for (i = 0; i < nx; i++) {
for (i = 0; i < NX; i++) {
// construct the Jacobian matrix column wise
CALL(FMI3GetAdjointDerivative(S, &vr_dx[i], 1, vr_x, nx, &dk, 1, &J[i][0], nx));
CALL(FMI3GetAdjointDerivative(S, &vr_dx[i], 1, vr_x, NX, &dk, 1, &J[i][0], NX));
}
// end::GetJacobianAdjoint[]

Expand Down
6 changes: 5 additions & 1 deletion fmusim/FMICVode.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ Solver* FMICVodeCreate(FMIInstance* S, const FMIModelDescription* modelDescripti
solver->abstol = N_VNew_Serial(NV_LENGTH_S(solver->x), solver->sunctx);
ASSERT_NOT_NULL(solver->abstol);

CALL_FMI(solver->get_nominals(solver->S, NV_DATA_S(solver->abstol), solver->nx));
if (solver->nx > 0) {
CALL_FMI(solver->get_nominals(solver->S, NV_DATA_S(solver->abstol), solver->nx));
} else {
NV_DATA_S(solver->abstol)[0] = 1;
}

for (size_t i = 0; i < NV_LENGTH_S(solver->x); i++) {
NV_DATA_S(solver->abstol)[i] *= tolerance;
Expand Down
18 changes: 13 additions & 5 deletions include/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,22 @@ typedef struct {

ModelData modelData;

#if NZ > 0
// event indicators
double z[NZ];
#endif

// internal solver steps
uint64_t nSteps;

// Co-Simulation
bool earlyReturnAllowed;
bool eventModeUsed;

// solver
size_t nz;
double* z;
double* prez;

size_t nx;
double* x;
double* dx;

} ModelInstance;

ModelInstance *createModelInstance(
Expand All @@ -166,6 +170,8 @@ ModelInstance *createModelInstance(

void freeModelInstance(ModelInstance *comp);

void exitInitializationMode(ModelInstance* comp);

void reset(ModelInstance* comp);

void setStartValues(ModelInstance* comp);
Expand Down Expand Up @@ -208,6 +214,8 @@ Status getInterval(ModelInstance* comp, ValueReference vr, double* interval, int

Status activateModelPartition(ModelInstance* comp, ValueReference vr, double activationTime);

size_t getNumberOfEventIndicators(ModelInstance* comp);
size_t getNumberOfContinuousStates(ModelInstance* comp);
void getContinuousStates(ModelInstance *comp, double x[], size_t nx);
void setContinuousStates(ModelInstance *comp, const double x[], size_t nx);
void getDerivatives(ModelInstance *comp, double dx[], size_t nx);
Expand Down
Loading