Skip to content

Commit

Permalink
Update model instance and generator code.
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquinffernandez committed Aug 27, 2024
1 parent 8c62ce3 commit 31e98b3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 37 deletions.
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

0 comments on commit 31e98b3

Please sign in to comment.