Skip to content

Commit

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

commit 17e8c97
Author: joaquin.f.fernandez <[email protected]>
Date:   Tue Aug 27 21:37:24 2024 -0300

    Fix GUI debug flags bug.

commit 46a66ff
Author: joaquin.f.fernandez <[email protected]>
Date:   Tue Aug 27 21:37:10 2024 -0300

    Updated vs code config files.

commit 5067c01
Author: joaquin.f.fernandez <[email protected]>
Date:   Tue Aug 27 21:36:58 2024 -0300

    Added initi code time test.

commit 08aefce
Merge: 2351872 757f4e1
Author: joaquin.f.fernandez <[email protected]>
Date:   Tue Aug 27 21:06:39 2024 -0300

    Merge branch 'qss-solver-dev' into mmoc/iss-249-add-time-var-to-initialization-code

commit 2351872
Merge: 7efbd30 eb9521c
Author: joaquin.f.fernandez <[email protected]>
Date:   Tue Aug 27 18:55:25 2024 -0300

    Merge branch 'qss-solver-dev' into mmoc/iss-249-add-time-var-to-initialization-code

commit 7efbd30
Author: joaquin.f.fernandez <[email protected]>
Date:   Tue Aug 27 10:22:07 2024 -0300

    Added code for time variable in initialization code.

commit e225f11
Author: joaquin.f.fernandez <[email protected]>
Date:   Tue Aug 27 10:21:29 2024 -0300

    Added autonomous method to statements.

commit 8f417c8
Author: joaquin.f.fernandez <[email protected]>
Date:   Tue Aug 27 10:20:52 2024 -0300

    Use expression autonomous method in equation.

commit 76fd9f7
Author: joaquin.f.fernandez <[email protected]>
Date:   Tue Aug 27 10:20:15 2024 -0300

    Added autonomous method to expressions.
  • Loading branch information
joaquinffernandez committed Aug 28, 2024
1 parent a85eb22 commit da5616c
Show file tree
Hide file tree
Showing 13 changed files with 343 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/gui/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bool Utils::isSet(AppFlags flag)
{
QSettings settings(QCoreApplication::applicationDirPath() + "/qss-solver.ini", QSettings::IniFormat);
QString val = settings.value(appFlagName(flag), "Value not found in file qss-solver.ini").toString();
return val.isEmpty();
return val == "true";
}

QString Utils::getDebugValue()
Expand Down
20 changes: 20 additions & 0 deletions src/mmoc/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,26 @@
"ignoreFailures": true
}
]
},

// Use time variable in initialization code
{
"name": "Init Code Time Test model.",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/usr/bin/mmoc",
"args": ["-o /home/joaquin/work/qss-solver/build/init_code_time/init_code_time ", "/home/joaquin/work/qss-solver/src/mmoc/tests/system/gt_data/init_code_time/init_code_time.mo"],
"cwd": "${workspaceFolder}/usr/bin/",
"environment": [],
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}

]
}
12 changes: 9 additions & 3 deletions src/mmoc/generator/model_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ void ModelInstance::initialCode()
{
StatementTable stms = _model.initialCode();
StatementTable::iterator it;
stringstream buffer;
bool autonomous = true;
ModelConfig::instance().setLocalInitSymbols();
ModelConfig::instance().setInitialCode(true);
VarSymbolTable symbols = _model.symbols();
Expand All @@ -380,6 +380,12 @@ void ModelInstance::initialCode()
}
for (Statement stm = stms.begin(it); !stms.end(it); stm = stms.next(it)) {
_writer->write(stm, WRITER::Init_Code);
autonomous = autonomous && stm.autonomous();
}
if (!autonomous) {
stringstream initial_time;
initial_time << "int t = " << _model.annotations().initialTime() << ";";
ModelConfig::instance().addLocalSymbol(initial_time.str());
}
ModelConfig::instance().setInitialCode(false);
ModelConfig::instance().unsetLocalInitSymbols();
Expand Down Expand Up @@ -670,7 +676,6 @@ void QSSModelInstance::header()
_writer->write(buffer, WRITER::Model_Header);
for (Variable var = symbols.begin(it); !symbols.end(it); var = symbols.next(it)) {
if (var.isState()) {
stringstream buffer;
Macros macros(_model, var);
buffer << "// Derivative definition for variable: " << var.name() << endl;
buffer << "#define _der" << var << macros.parameters() << " dx[coeff+1]";
Expand Down Expand Up @@ -792,7 +797,8 @@ void ClassicModelInstance::header()
_writer->write(buffer, WRITER::Model_Header);
for (Variable var = symbols.begin(it); !symbols.end(it); var = symbols.next(it)) {
if (var.isState()) {
stringstream buffer, arguments;
stringstream buffer;
stringstream arguments;
Macros macros(_model, var);
arguments << macros.engineIndexArguments();
buffer << "// Derivative definition for variable: " << var.name() << endl;
Expand Down
4 changes: 1 addition & 3 deletions src/mmoc/ir/equation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <util/model_config.h>
#include <util/util.h>
#include <util/visitors/algebraics.h>
#include <util/visitors/autonomous.h>
#include <util/visitors/called_functions.h>
#include <util/visitors/get_index_variables.h>
#include <util/visitors/is_recursive_def.h>
Expand Down Expand Up @@ -131,8 +130,7 @@ void Equation::initialize(AST_Equation eq)
void Equation::setup()
{
stringstream buffer;
Autonomous autonomous;
_autonomous = autonomous.apply(_rhs.expression());
_autonomous = _rhs.autonomous();
CalledFunctions cf;
_calledFunctions = cf.apply(_rhs.expression());
if (_range) {
Expand Down
7 changes: 7 additions & 0 deletions src/mmoc/ir/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <util/error.h>
#include <util/model_config.h>
#include <util/util.h>
#include <util/visitors/autonomous.h>
#include <util/visitors/expression_printer.h>
#include <util/visitors/get_index_variables.h>
#include <util/visitors/is_constant_index.h>
Expand Down Expand Up @@ -177,5 +178,11 @@ std::multimap<std::string, int> Expression::usedVariables() const
return used_variables.apply(_exp);
}

bool Expression::autonomous() const
{
Autonomous autonomous;
return autonomous.apply(expression());
}

} // namespace IR
} // namespace MicroModelica
1 change: 1 addition & 0 deletions src/mmoc/ir/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Expression {
friend std::ostream& operator<<(std::ostream& out, const Expression& s);

std::multimap<std::string, int> usedVariables() const;
bool autonomous() const;

protected:
std::vector<Expression> usageExps() const;
Expand Down
25 changes: 17 additions & 8 deletions src/mmoc/ir/statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
******************************************************************************/
#include <sstream>

#include "../ast/ast_builder.h"
#include "../ast/statement.h"
#include "../util/model_config.h"
#include "../util/util.h"
#include "../util/process_statement.h"
#include "../util/visitors/called_functions.h"
#include "helpers.h"
#include "statement.h"
#include <ast/ast_builder.h>
#include <ast/statement.h>
#include <ir/helpers.h>
#include <ir/statement.h>
#include <util/model_config.h>
#include <util/util.h>
#include <util/process_statement.h>
#include <util/visitors/called_functions.h>

namespace MicroModelica {
using namespace Util;
Expand Down Expand Up @@ -268,6 +268,15 @@ ExpressionList Statement::assignments(STATEMENT::AssignTerm asg) const
return ExpressionList();
}

bool Statement::autonomous() const
{
bool autonomous = true;
for (const auto& exp : _rhs_assignments) {
autonomous = autonomous && exp.autonomous();
}
return autonomous;
}

std::ostream& operator<<(std::ostream& out, const Statement& s) { return out << s.print(); }
} // namespace IR
} // namespace MicroModelica
15 changes: 6 additions & 9 deletions src/mmoc/ir/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
******************************************************************************/

#ifndef MMO_STATEMENT_H_
#define MMO_STATEMENT_H_
#pragma once

#include "../ast/ast_types.h"
#include "../util/table.h"
#include "index.h"
#include <ast/ast_types.h>
#include <ir/index.h>
#include <util/table.h>

namespace MicroModelica {

Expand All @@ -38,8 +37,7 @@ typedef enum { LHS, RHS, LHS_DISCRETES, LHS_STATES } AssignTerm;
class Statement {
public:
Statement(AST_Statement stm, bool initial = false, const std::string& block = "");
Statement(AST_Statement stm, Option<Range> range, bool initial = false,
const std::string& block = "");
Statement(AST_Statement stm, Option<Range> range, bool initial = false, const std::string& block = "");
Statement() : _stm(nullptr), _range(), _block(), _lhs_assignments(), _rhs_assignments(), _lhs_discretes(), _lhs_states(){};
~Statement() = default;

Expand All @@ -59,6 +57,7 @@ class Statement {
bool isAssignment() const;
bool isForStatement() const;
inline Option<Range> range() { return _range; };
bool autonomous() const;

protected:
void initialize();
Expand All @@ -82,5 +81,3 @@ class Statement {
typedef ModelTable<int, Statement> StatementTable;
} // namespace IR
} // namespace MicroModelica

#endif /* MMO_STATEMENT_H_ */
144 changes: 144 additions & 0 deletions src/mmoc/tests/system/gt_data/init_code_time/init_code_time.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "init_code_time.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 = FALSE;
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)
{
}

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

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,0,0,1000,0,1,0,"init_code_time");
QSS_data modelData = simulator->data;
MODEL_DATA_ACCESS(modelData)
int* states = (int*) malloc(1000*sizeof(int));
int row, eq_var, c_row;
int x_ind;
int _d1;
int _rg_d1;
int i;
int t = 0;
for(i = 1; i<=999; i+=1) {
_init_u(i,0) = _time;
}
_init_u(1000,0) = _time;
for(row = 1; row <= 1000; row++) {
c_row = _c_index(row);
_get_eq_1_var_idxs(row, eq_var);
_get_u_idxs(eq_var);
}
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);
}
SD_setupJacMatrices(modelData->jac_matrices);
simulator->time = QSS_Time(1000,0,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("init_code_time",0,0,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);
}

void CLC_initializeDataStructs(CLC_simulator simulator)
{
}

51 changes: 51 additions & 0 deletions src/mmoc/tests/system/gt_data/init_code_time/init_code_time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Model data access macro.

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

// 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: 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);

// 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]
Loading

0 comments on commit da5616c

Please sign in to comment.