Skip to content

Commit

Permalink
Merge branch 'qss-solver-dev' into mmoc/iss-249-add-time-var-to-initi…
Browse files Browse the repository at this point in the history
…alization-code
  • Loading branch information
joaquinffernandez committed Aug 28, 2024
2 parents 2351872 + 757f4e1 commit 08aefce
Show file tree
Hide file tree
Showing 24 changed files with 786 additions and 701 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"
}
3 changes: 2 additions & 1 deletion src/mmoc/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,6 @@
"docwriter.progress.trackFunctions": true,
"docwriter.progress.trackMethods": false,
"docwriter.progress.trackClasses": false,
"docwriter.progress.trackTypes": false
"docwriter.progress.trackTypes": false,
"sonarlint.pathToCompileCommands": "${workspaceFolder}/compile_commands.json"
}
58 changes: 33 additions & 25 deletions src/mmoc/generator/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,20 @@ using namespace Util;
namespace Generator {

Files::Files(ModelInstancePtr modelInstance, Model& model, CompileFlags& flags)
: _fname(model.name()), _model(model), _modelInstance(modelInstance), _writer(new FileWriter()), _flags(flags)
: _fname(model.name()), _model(model), _modelInstance(modelInstance), _writer(std::make_shared<FileWriter>()), _flags(flags)
{
if (_flags.hasOutputFile()) {
_fname = _flags.outputFile();
}
}

Files::Files(string name, CompileFlags& flags) : _fname(name), _model(), _writer(new FileWriter()), _flags(flags)
Files::Files(string name, CompileFlags& flags) : _fname(name), _model(), _writer(std::make_shared<FileWriter>()), _flags(flags)
{
if (_flags.hasOutputFile()) {
_fname = _flags.outputFile();
}
}

Files::~Files() {}

void Files::makefile()
{
stringstream buffer;
Expand Down Expand Up @@ -99,11 +97,10 @@ void Files::makefile()
}
if (_flags.hasObjects()) {
list<string> objects = _flags.objects();
for (list<string>::iterator it = objects.begin(); it != objects.end(); it++) {
string inc = *it;
unsigned int f = inc.rfind("/");
inc.erase(inc.begin() + f, inc.end());
include.insert(inc, inc);
for (auto obj : objects) {
unsigned long f = obj.rfind("/");
obj.erase(obj.begin() + f, obj.end());
include.insert(obj, obj);
}
}
string pinclude = Utils::instance().environmentVariable("MMOC_INCLUDE");
Expand Down Expand Up @@ -136,8 +133,8 @@ void Files::makefile()
if (_flags.hasObjects()) {
buffer << "SRC := ";
list<string> objects = _flags.objects();
for (list<string>::iterator it = objects.begin(); it != objects.end(); it++) {
buffer << *it << " ";
for (const auto& obj : objects) {
buffer << obj << " ";
}
_writer->print(buffer);
}
Expand Down Expand Up @@ -305,9 +302,10 @@ void Files::settings(ModelAnnotation annotation)
_writer->print("sol=\"" + annotation.solverString() + "\";");
list<double> dq = annotation.dqmin();
buffer << "dqmin=(";
int count = 0, size = dq.size();
for (list<double>::iterator it = dq.begin(); it != dq.end(); it++) {
buffer << *it;
unsigned long count = 0;
unsigned long size = dq.size();
for (const auto& it : dq) {
buffer << it;
if (++count < size) {
buffer << ",";
}
Expand All @@ -318,8 +316,8 @@ void Files::settings(ModelAnnotation annotation)
buffer << "dqrel=(";
count = 0;
size = dq.size();
for (list<double>::iterator it = dq.begin(); it != dq.end(); it++) {
buffer << *it;
for (const auto& it : dq) {
buffer << it;
if (++count < size) {
buffer << ",";
}
Expand All @@ -340,19 +338,31 @@ void Files::settings(ModelAnnotation annotation)
_writer->print(buffer);
buffer << "BDFMaxStep=" << annotation.BDFMaxStep() << ";";
_writer->print(buffer);
addAnnotation(annotation, "CVODEMaxOrder", IntegerAnnotations::CVODEMaxOrder);
addAnnotation(annotation, "XOutput", IntegerAnnotations::XOutput);
_writer->clearFile();
}

void Files::printList(list<string> ann, string tag)
void Files::addAnnotation(const IR::ModelAnnotation& annotation, const string& mmo_name, IntegerAnnotations name)
{
if (annotation.hasAnnotation(name)) {
stringstream buffer;
buffer << mmo_name << "=" << annotation.getAnnotation(name) << ";";
_writer->print(buffer);
}
}

void Files::printList(const list<string>& ann, const string& tag) const
{
stringstream buffer;
if (ann.empty()) {
return;
}
buffer << tag << "=(";
int count = 0, size = ann.size();
for (list<string>::iterator it = ann.begin(); it != ann.end(); it++) {
buffer << *it;
unsigned long count = 0;
unsigned long size = ann.size();
for (const auto& it : ann) {
buffer << it;
if (++count < size) {
buffer << ",";
}
Expand All @@ -366,19 +376,17 @@ void Files::bdfPartition()
AST_ExpressionList BDF_exps = _model.annotations().BDFPartition();
AST_ExpressionListIterator bdf_exp_it;
list<int> variables;
list<int>::iterator var_it;
foreach (bdf_exp_it, BDF_exps) {
PartitionInterval partition_interval;
list<int> ret = partition_interval.apply(current_element(bdf_exp_it));
variables.splice(variables.end(), ret);
}
string file_name = _fname + "_BDF.part";
ofstream partition(file_name.c_str(), ios::out);
int partition_size = variables.size();
unsigned long partition_size = variables.size();
partition << partition_size << endl;
for (var_it = variables.begin(); var_it != variables.end(); var_it++) {
int val = *var_it;
partition << val << endl;
for (const auto& var_it : variables) {
partition << var_it << endl;
}
partition.close();
}
Expand Down
8 changes: 5 additions & 3 deletions src/mmoc/generator/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@ class Files {
public:
Files(ModelInstancePtr modelInstance, IR::Model& model, Util::CompileFlags& flags);
Files(string name, Util::CompileFlags& flags);
~Files();
~Files() = default;
void makefile();
void run();
void plot();
void settings(IR::ModelAnnotation annotation);
void graph();
void bdfPartition();

private:
protected:
std::string variablePlotSettings();
void printList(list<string> ann, string tag);
void printList(const list<string>& ann, const string& tag) const;
void addAnnotation(const IR::ModelAnnotation& annotation, const string& mmo_name, IR::IntegerAnnotations name);

string _fname;
IR::Model _model;
ModelInstancePtr _modelInstance;
Expand Down
30 changes: 14 additions & 16 deletions src/mmoc/generator/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ using namespace IR;
using namespace Util;
namespace Generator {

Generator::Generator(const StoredDefinition& std, CompileFlags& flags)
Generator::Generator(const StoredDefinition& std, const CompileFlags& flags)
: _std(std), _flags(flags), _model_instance(nullptr), _writer(nullptr), _includes(), _fheader()
{
if (_flags.output()) {
_writer = WriterPtr(new MemoryWriter());
_writer = WriterPtr(std::make_shared<MemoryWriter>());
} else {
_writer = WriterPtr(new FileWriter());
_writer = WriterPtr(std::make_shared<FileWriter>());
}
}

Expand All @@ -56,15 +56,15 @@ int Generator::generate()
}
_writer->setFile(base_name + ".c");
switch (model.annotations().solver()) {
case DOPRI:
case DASSL:
case CVODE_BDF:
case IDA:
case CVODE_AM:
_model_instance = ModelInstancePtr(new ClassicModelInstance(model, _flags, _writer));
case Solver::DOPRI:
case Solver::DASSL:
case Solver::CVODE_BDF:
case Solver::IDA:
case Solver::CVODE_AM:
_model_instance = ModelInstancePtr(std::make_shared<ClassicModelInstance>(model, _flags, _writer));
break;
default:
_model_instance = ModelInstancePtr(new QSSModelInstance(model, _flags, _writer));
_model_instance = ModelInstancePtr(std::make_shared<QSSModelInstance>(model, _flags, _writer));
}
_model_instance->generate();
_writer->clearFile();
Expand All @@ -77,7 +77,7 @@ int Generator::generate()
files.run();
files.plot();
files.settings(model.annotations());
if (model.annotations().solver() == LIQSS_BDF && !model.annotations().BDFPartition()->empty()) {
if (model.annotations().solver() == Solver::LIQSS_BDF && !model.annotations().BDFPartition()->empty()) {
files.bdfPartition();
}
if (model.externalFunctions()) {
Expand All @@ -103,7 +103,7 @@ int Generator::generate()
return Error::instance().errors();
}

void Generator::generateIncludes(string name)
void Generator::generateIncludes(const string& name)
{
stringstream buffer;
buffer << "#include <math.h>" << endl;
Expand All @@ -112,15 +112,13 @@ void Generator::generateIncludes(string name)
_writer->write(buffer, WRITER::Function_Header);
}

void Generator::generateModel() {}

void Generator::calledFunctionHeader(string file_name)
void Generator::calledFunctionHeader(const string& file_name)
{
string indent = _writer->indent(1);
string file = file_name;
file.append(".h");
_writer->setFile(file);
for (list<string>::iterator it = _fheader.begin(); it != _fheader.end(); it++) {
for (auto it = _fheader.begin(); it != _fheader.end(); it++) {
_writer->print(*it);
}
_writer->clearFile();
Expand Down
10 changes: 4 additions & 6 deletions src/mmoc/generator/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
******************************************************************************/

#pragma once
#pragma once

#include <fstream>
#include <iostream>
Expand All @@ -37,14 +37,13 @@ namespace Generator {

class Generator {
public:
Generator(const IR::StoredDefinition& std, Util::CompileFlags& flags);
Generator(const IR::StoredDefinition& std, const Util::CompileFlags& flags);
~Generator() = default;
int generate();

private:
void generateIncludes(string name);
void generateModel();
void calledFunctionHeader(string file_name);
void generateIncludes(const string& name);
void calledFunctionHeader(const string& file_name);
IR::StoredDefinition _std;
IR::Function _function;
IR::Package _package;
Expand All @@ -57,4 +56,3 @@ class Generator {
};
} // namespace Generator
} // namespace MicroModelica

32 changes: 17 additions & 15 deletions src/mmoc/generator/model_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Generator {

namespace MODEL_INSTANCE {

typedef enum {
enum class Component {
Model_Settings,
Model,
Deps,
Expand All @@ -47,9 +47,10 @@ typedef enum {
BdfModel,
CLC_Init,
QSS_Init
} Component;
};

enum class NodeType { SD, SZ, HD, HZ, DD };

typedef enum { SD, SZ, HD, HZ, DD } NodeType;
} // namespace MODEL_INSTANCE

class ModelInstance {
Expand Down Expand Up @@ -118,14 +119,14 @@ class QSSModelInstance : public ModelInstance {
public:
QSSModelInstance();
QSSModelInstance(IR::Model &model, Util::CompileFlags &flags, WriterPtr writer);
~QSSModelInstance() = default;
void initializeDataStructures();
Graph computationalGraph();
void generate();
void header();
~QSSModelInstance() override = default;
void initializeDataStructures() override;
Graph computationalGraph() override;
void generate() override;
void header() override;

protected:
void definition();
void definition() override;
void dependencies();
void bdfDefinition();

Expand All @@ -142,13 +143,13 @@ class QSSModelInstance : public ModelInstance {
class ClassicModelInstance : public ModelInstance {
public:
ClassicModelInstance(IR::Model &model, Util::CompileFlags &flags, WriterPtr writer);
~ClassicModelInstance() = default;
void initializeDataStructures();
void generate();
void header();
~ClassicModelInstance() override = default;
void initializeDataStructures() override;
void generate() override;
void header() override;

protected:
void definition();
void definition() override;

private:
void allocateSolver();
Expand All @@ -159,6 +160,7 @@ class ClassicModelInstance : public ModelInstance {
WriterPtr _writer;
};

typedef std::shared_ptr<ModelInstance> ModelInstancePtr;
using ModelInstancePtr = std::shared_ptr<ModelInstance>;

} // namespace Generator
} // namespace MicroModelica
Loading

0 comments on commit 08aefce

Please sign in to comment.