Skip to content

Commit

Permalink
[iss-253]
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 75ec103
Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com>
Date:   Tue Aug 27 18:36:02 2024 -0300

    Added integration test for discrete integer.

commit d41b199
Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com>
Date:   Tue Aug 27 15:13:36 2024 -0300

    Fix printers to handle discrete integer definitions.

commit 40d55d0
Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com>
Date:   Tue Aug 27 15:12:50 2024 -0300

    Update type module interface.

commit 9e0a6c0
Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com>
Date:   Tue Aug 27 15:12:33 2024 -0300

    Allow discrete integer definitions.

commit 33c47f7
Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com>
Date:   Tue Aug 27 15:12:03 2024 -0300

    Updated vs code files.

commit b982ea0
Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com>
Date:   Tue Aug 27 15:11:43 2024 -0300

    Update variable interface, add cast method for discrete integer variables.
  • Loading branch information
joaquinffernandez committed Aug 27, 2024
1 parent eb9521c commit e27a5c0
Show file tree
Hide file tree
Showing 16 changed files with 432 additions and 386 deletions.
19 changes: 19 additions & 0 deletions src/engine/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@
"ignoreFailures": true
}
]
},

// Advection model
{
"name": "Advection.",
"type": "cppdbg",
"request": "launch",
"program": "/home/joaquin/work/qss-solver/build/advection/advection",
"cwd": "/home/joaquin/work/qss-solver/build/advection/",
"environment": [],
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}

]
}
17 changes: 15 additions & 2 deletions src/engine/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
{
"files.associations": {
"random": "cpp",
"qss_bdf.h": "c"
"qss_bdf.h": "c",
"array": "c",
"compare": "c",
"functional": "c",
"istream": "c",
"ostream": "c",
"ranges": "c",
"tuple": "c",
"type_traits": "c",
"utility": "c",
"typeindex": "c",
"typeinfo": "c",
"qss_data.h": "c"
},
"C_Cpp.dimInactiveRegions": true
"C_Cpp.dimInactiveRegions": true,
"sonarlint.pathToCompileCommands": "${workspaceFolder}/compile_commands.json"
}
13 changes: 7 additions & 6 deletions src/mmoc/ir/mmo_ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,17 @@ void MicroModelicaIR::visit(AST_Element x)
Variable vi(newType_Integer(), tp, current_element(it)->modification(), nullptr, size, array);
_class->insert(current_element(it)->name(), vi, t);
} else {
Variable new_var;
if ((tp & TP_PARAMETER) && c->isInteger()) {
Variable vi(newType_Integer(), tp, current_element(it)->modification(), nullptr, size, array);
_class->insert(current_element(it)->name(), vi, t);
new_var = Variable(newType_Integer(), tp, current_element(it)->modification(), nullptr, size, array);
} else if (c->isString()) {
Variable vi(newType_String(), tp, current_element(it)->modification(), nullptr, size, array);
_class->insert(current_element(it)->name(), vi, t);
new_var = Variable(newType_String(), tp, current_element(it)->modification(), nullptr, size, array);
} else if ((tp & TP_DISCRETE) && c->isInteger()) {
new_var = Variable(newType_Integer(), tp, current_element(it)->modification(), nullptr, size, array);
} else {
Variable vi(newType_Real(), tp, current_element(it)->modification(), nullptr, size, array);
_class->insert(current_element(it)->name(), vi, t);
new_var = Variable(newType_Real(), tp, current_element(it)->modification(), nullptr, size, array);
}
_class->insert(current_element(it)->name(), new_var, t);
}
}
} else if (e == ELCLASS) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "allow_discrete_integer.h"
#include <common/utils.h>
#include <common/model.h>
#include <common/commands.h>
#include <qss/qss_model.h>
#include <classic/classic_model.h>

void MOD_settings(SD_simulationSettings settings)
{
settings->debug = 0;
settings->parallel = FALSE;
settings->hybrid = TRUE;
settings->method = 0;
}

void MOD_definition(int idx, double *x, double *d, double *a, double t, double *dx)
{
int _d1;
int i;
if (_is_var_u(idx)) {
_get_u_idxs(idx);
_apply_usage_eq_1(_d1);
if ((i >= 1 && i <= 1000)) {
_der_u(i,0) = _time;


}
return;
}
}

void MOD_zeroCrossing(int idx, double *x, double *d, double *a, double t, double *zc)
{
switch(idx) {
case _eval_event_1: {
_zc(0) = _time-(0);


return;
}
}
}

void MOD_handlerPos(int idx, double *x, double* q, double *d, double *a, double t)
{
switch(idx) {
case _eval_event_1: {
_init_u((int)_d,0) = 0;
return;
}
}
}

void MOD_handlerNeg(int idx, double *x, double* q, double *d, double *a, double t)
{
}

void MOD_output(int idx, double *x, double *d, double *a, double t, double *out)
{
}

void MOD_jacobian(double *x, double *d, double *a, double t, SD_jacMatrices dvdx, double *jac)
{
int row, row_t, eq_var, c_row, c_row_g;
int col, col_g, col_t;
int x_ind;
double aux;
int _d1;
int _rg_d1;
int i;
SD_cleanJacMatrices(dvdx);
for(row = 1; row <= 1000; row++) {
c_row = _c_index(row);
_get_eq_1_var_idxs(row, eq_var);
_get_u_idxs(eq_var);
}
// Assign Jacobian Matrix values for equation: 0
for (row = 0; row < 1000; row++) {
for (col = 0; col < dvdx->df_dx[0]->size[row]; col++) {
row_t = dvdx->df_dx[0]->index[row][col];
_assign_jac(row_t, dvdx->df_dx[0]->value[row][col]);
}
}
}

void MOD_dependencies(int idx, double *x, double *d, double *a, double t, double *dx, int *map)
{
}

void MOD_BDF_definition(double *x, double *d, double *a, double t, double *dx, int *BDFMap, int nBDF)
{
int idx;
int __bdf_it;
for(__bdf_it = 0; __bdf_it < nBDF; __bdf_it++) {
idx = BDFMap[__bdf_it];
int _d1;
int i;
if (_is_var_u(idx)) {
_get_u_idxs(idx);
_apply_usage_eq_1(_d1);
if ((i >= 1 && i <= 1000)) {
_eval_dep_u(i,1) = _time;


}
continue;
}
}
}

void QSS_initializeDataStructs(QSS_simulator simulator)
{
simulator->data = QSS_Data(1000,1,1,1000,0,1,0,"allow_discrete_integer");
QSS_data modelData = simulator->data;
MODEL_DATA_ACCESS(modelData)
int* states = (int*) malloc(1000*sizeof(int));
int* discretes = (int*) malloc(1*sizeof(int));
int* events = (int*) malloc(1*sizeof(int));
int row, eq_var, c_row;
int x_ind;
int _d1;
int _rg_d1;
int i;
for(row = 1; row <= 1000; row++) {
c_row = _c_index(row);
_get_eq_1_var_idxs(row, eq_var);
_get_u_idxs(eq_var);
}
modelData->event[_idx_event_1].nLHSSt++;
QSS_allocDataMatrix(modelData);
cleanVector(states, 0, 1000);
for(row = 1; row <= 1000; row++) {
c_row = _c_index(row);
_get_eq_1_var_idxs(row, eq_var);
_get_u_idxs(eq_var);
}
cleanVector(events, 0, 1);
modelData->event[_idx_event_1].LHSSt[events[_idx_event_1]++] = _idx_u(0,0);
modelData->event[_idx_event_1].direction = 1;
modelData->event[_idx_event_1].relation = 2;
SD_setupJacMatrices(modelData->jac_matrices);
simulator->time = QSS_Time(1000,1,1000,0,ST_Binary, NULL);
for(i = 1; i<=1000; i+=1) {
modelData->IT[_input_1(i)] = _idx_u(i,0);
}
simulator->output = SD_Output("allow_discrete_integer",0,1,1000,NULL,0,0,CI_Step,SD_Memory,NULL);
SD_output modelOutput = simulator->output;
simulator->model = QSS_Model(MOD_definition, MOD_dependencies, MOD_zeroCrossing, MOD_handlerPos, MOD_handlerNeg, MOD_jacobian, MOD_BDF_definition);
free(states);
free(discretes);
free(events);
}

void CLC_initializeDataStructs(CLC_simulator simulator)
{
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Model data access macro.

#define MODEL_DATA_ACCESS(m) \
double* x = m->x; \
double* d = m->d;

// Coeff multipliers definition.

#define COEFF_MULTIPLIER(c) COEFF_MULTIPLIER_##c
#define COEFF_MULTIPLIER_0 1
#define COEFF_MULTIPLIER_1 1

// Model Variables Macros

// Macros definition for variable: _event_1
#define _idx_event_1 0
#define _eval_event_1 0

// Macros definition for variable: d
#define _idx_d 0
#define _d d[_idx_d]

// Macros definition for variable: u
#define _idx_u(d1,coeff) ((d1-1))
#define _state_idx_u(d1,coeff) ((d1-1))*2 + coeff
#define _u(d1,coeff) x[_state_idx_u(d1,coeff)] * COEFF_MULTIPLIER(coeff)
#define _init_u(d1,coeff) x[_state_idx_u(d1,coeff)]
#define _q_u(d1,coeff) q[_state_idx_u(d1,coeff)] * COEFF_MULTIPLIER(coeff)
#define _eval_u(d1,coeff) ((d1-1))
#define _is_var_u(idx) idx >= 0 && idx < 1000
#define _get_u_idxs(idx)\
_d1 = (idx)+ 1;
#define _eval_dep_u(d1,coeff) dx[_state_idx_u(d1,coeff)]


// Derivative Equations Macros

// Macros for equation: 1
#define _apply_usage_eq_1(_d1) \
i = _d1;
#define _get_eq_1_var_idxs(row, var)\
_rg_d1 = 0 + (row-1)+ 1;\
var = _idx_u(_rg_d1,0);

// Event Macros

// Macros for event: 1

#define _zc(coeff) zc[coeff]

// Input Matrix Macros

#define _input_1(i) ((i-1))

// Jacobian Macros definition.
#define _assign_jac(r, val) \
col_t = dvdx->df_dx_t->size[r] + dvdx->df_dx_t->index[r][0]; \
dvdx->df_dx_t->index[r][0]++; \
jac[col_t] = val;
#define _c_index(i) (i-1)

#define _time t

// Derivative Macros definition.
// Derivative definition for variable: u
#define _der_u(d1,coeff) dx[coeff+1]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
minstep=1.00000e-14;
zchyst=1.00000e-12;
derdelta=1.00000e-08;
symdiff=1;
lps=0;
nodesize=10000;
jacobian=1;
it=0.00000e+00;
ft=1.00000e+01;
sol="QSS";
dqmin=(1.00000e-03);
dqrel=(1.00000e-03);
bdf=0;
BDFPartitionDepth=1;
BDFMaxStep=0.00000e+00;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
model allow_discrete_integer
constant Integer N=1000;
Real u[N];
discrete Integer d;

equation
for i in 1:N loop
der(u[i])=time;
end for;

algorithm
when time > 0 then
reinit(u[d],0);
end when;
annotation(
experiment(
MMO_Description="Use time variable in initialization code.",
MMO_Solver=QSS,
MMO_PartitionMethod=Metis,
Jacobian=Dense,
MMO_BDF_PDepth=1,
MMO_BDF_Max_Step=0,
MMO_RandomSeed=0,
StartTime=0,
StopTime=10,
Tolerance={1e-3},
AbsTolerance={1e-3}
));
end allow_discrete_integer;
3 changes: 1 addition & 2 deletions src/mmoc/tests/system/models_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ TEST_P(IModelTests, GenerateCode)
}

const char* models[] = {"adr", "adr2D", "advection", "advection2D", "advection2D_LI", "advection_quoted",
"advectionFlux", "airconds", "aircont", "bball_downstairs", "boost", "BouncingBall",
"advectionFlux", "airconds", "aircont", "allow_discrete_integer", "bball_downstairs", "boost", "BouncingBall",
"buck", "buckboost", "buck_circuit", "buck_term", "burgers", "cuk",
"cuk2", "interleaved", "inverters", "lc_line", "lotka_volterra", "mliqss_adr",
"mliqss_buck", "mliqss_test", "mliqss_TYSON", "NeuralNetwork1", "par_airconds", "par_airconds_cont",
"rectifier", "rltest", "rltest_LI", "spikings", "testFor", "test_input",
"TYSON", "VIRplanoS", "virus_replication"};

INSTANTIATE_TEST_SUITE_P(Models, IModelTests, testing::ValuesIn(models));

/// @}
2 changes: 1 addition & 1 deletion src/mmoc/util/ast_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ bool EqualExp::equalTraverseElement(AST_Expression a, AST_Expression b)
case EXPCOMPREF: {
AST_Expression_ComponentReference compRefA = a->getAsComponentReference();
Option<Variable> varInfoA = ModelConfig::instance().lookup(CREF_NAME(compRefA));
if (varInfoA && varInfoA->type()->getType() == TYARRAY) {
if (varInfoA && varInfoA->type()->getType() == SymbolType::TYARRAY) {
return compareArrays(compRefA, b->getAsComponentReference());
} else {
return CREF_NAME(a).compare(CREF_NAME(b)) == 0;
Expand Down
1 change: 1 addition & 0 deletions src/mmoc/util/model_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class ModelConfig {
inline void clearLocalSymbols() { _local_symbols.clear(); };
inline void setLocalInitSymbols() { _init_symbols = true; };
inline void unsetLocalInitSymbols() { _init_symbols = false; };

inline void setEvents(IR::EventTable events) { _events = events; }
inline IR::EventTable events() const { return _events; }
inline bool functionOutputs() const { return _function_outputs; }
Expand Down
Loading

0 comments on commit e27a5c0

Please sign in to comment.