Skip to content

Commit

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

commit c892f9a
Author: joaquin.f.fernandez <[email protected]>
Date:   Tue Aug 27 20:16:12 2024 -0300

    Fix compilation.

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

    Remove dead code.

commit aadb026
Author: joaquin.f.fernandez <[email protected]>
Date:   Tue Aug 27 19:16:22 2024 -0300

    Update vs code settings file.

commit 0f7d622
Author: joaquin.f.fernandez <[email protected]>
Date:   Tue Aug 27 19:16:08 2024 -0300

    Update file generator and added new annotations.

commit 31e98b3
Author: joaquin.f.fernandez <[email protected]>
Date:   Tue Aug 27 19:15:00 2024 -0300

    Update model instance and generator code.

commit 8c62ce3
Author: joaquin.f.fernandez <[email protected]>
Date:   Tue Aug 27 19:14:20 2024 -0300

    Update annotations interface.
  • Loading branch information
joaquinffernandez committed Aug 27, 2024
1 parent e27a5c0 commit 757f4e1
Show file tree
Hide file tree
Showing 9 changed files with 381 additions and 342 deletions.
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

Loading

0 comments on commit 757f4e1

Please sign in to comment.