Skip to content

Commit

Permalink
Update ModelFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Michele Vascellari authored and Michele Vascellari committed Jan 23, 2018
1 parent d0b1846 commit c05ae13
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ lib
test.csv
test.txt
compile_commands.json
cython/pkpc.cpp
11 changes: 8 additions & 3 deletions pkp.pyx → cython/pkpc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from libcpp cimport bool
cdef extern from "reactor.hpp" namespace "pkp":
cdef cppclass CxxReactor "pkp::Reactor":
CxxReactor(string, vector[double])
CxxReactor(string)
void solve(double dt, bool verbose)
vector[double] getParameters()
void setParameters(vector[double])
Expand All @@ -22,7 +23,7 @@ cdef class Reactor:
Devolatilization reactor with prescribed particle temperature.
"""
cdef CxxReactor* _c_reactor
def __cinit__(self, name, parameters):
def __cinit__(self, name, parameters=None):
"""
Parameters
----------
Expand All @@ -32,8 +33,12 @@ cdef class Reactor:
List of parameters
"""
cdef string c_name = name.encode()
cdef vector[double] c_parameters = parameters
self._c_reactor = new CxxReactor(c_name, c_parameters)
cdef vector[double] c_parameters
if parameters is None:
self._c_reactor = new CxxReactor(c_name)
else:
c_parameters = parameters
self._c_reactor = new CxxReactor(c_name, c_parameters)

def __dealloc__(self):
del self._c_reactor
Expand Down
11 changes: 8 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from Cython.Build import cythonize
from distutils.extension import Extension

sourcefiles = ['pkp.pyx']
sourcefiles = ['cython/pkpc.pyx']
compile_opts = ['-std=c++11']
ext = [Extension('*',
ext = [Extension('pkpc',
sourcefiles,
extra_compile_args=compile_opts,
extra_link_args=['-lpkp'],
Expand All @@ -16,5 +16,10 @@
]

setup(
ext_modules=cythonize(ext)
name="pkpc",
ext_modules=cythonize(ext),
description="FLUT Python class",
author='Michele Vascellari',
author_email='[email protected]',
version="0.0.1"
)
23 changes: 16 additions & 7 deletions src/ModelFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace pkp{
//ModelFactory::ModelFactory() {}
//ModelFactory::~ModelFactory() {}

std::shared_ptr<Model> ModelFactory::instantiate(const std::string& name)
std::shared_ptr<Model> ModelFactory::create(const std::string& name)
{
auto it = ModelFactory::registry().find(name);
//return it == ModelFactory::registry().end() ? nullptr : (it->second)();
Expand All @@ -20,16 +20,25 @@ namespace pkp{
return nullptr;
}

// TODO fix it
// std::shared_ptr<Model> ModelFactory::instantiate(const std::string& name, const dvector &par)
// {
// auto it = ModelFactory::registry().find(name);
// return it == ModelFactory::registry().end() ? nullptr : (it->second)();
// }
std::shared_ptr<Model> ModelFactory::create(const std::string& name, const dvector & par)
{
auto it = ModelFactory::registry_par().find(name);
//return it == ModelFactory::registry().end() ? nullptr : (it->second)();
if (it != ModelFactory::registry_par().end())
return it->second(par);
else
return nullptr;
}

ModelFactory::registry_map & ModelFactory::registry()
{
static registry_map impl;
return impl;
}

ModelFactory::registry_map_par & ModelFactory::registry_par()
{
static registry_map_par impl;
return impl;
}
}
11 changes: 5 additions & 6 deletions src/ModelFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ namespace pkp {
class ModelFactory {
private:
public:
//ModelFactory();
//~ModelFactory();
typedef std::unordered_map<std::string, std::function<std::shared_ptr<Model>()>> registry_map;
//typedef std::unordered_map<std::string, std::function<std::shared_ptr<Model>(const dvector &)>> registry_map_par;
static std::shared_ptr<Model> instantiate(const std::string& name);
//static std::shared_ptr<Model> instantiate(const std::string& name, const dvector& par);
typedef std::unordered_map<std::string, std::function<std::shared_ptr<Model>(const dvector &)>> registry_map_par;
static std::shared_ptr<Model> create(const std::string& name);
static std::shared_ptr<Model> create(const std::string& name, const dvector& par);
static registry_map & registry();
//static registry_map & registry_par();
static registry_map_par & registry_par();
};

template<typename T> struct ModelFactoryRegister
{
ModelFactoryRegister(std::string name)
{
ModelFactory::registry()[name] = []() {return std::make_shared<T>();};
ModelFactory::registry_par()[name] = [](const dvector& par) {return std::make_shared<T>(par);};
std::cout << "Registering Model class '" << name << "'\n";
}
};
Expand Down
20 changes: 1 addition & 19 deletions src/ReactorFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// PKP
//
// Created by <author> on 12/01/2018.
// TODO remove not necessary anymore
//
//

Expand All @@ -18,8 +19,6 @@
#include "C2SM.hpp"

namespace pkp{


struct ModelFactory2{
static std::shared_ptr<Model> create(const std::string &model_type, const dvector &parameters){
typedef std::unordered_map<std::string,std::function<std::shared_ptr<Model>(const dvector &)>> registry_map;
Expand All @@ -36,23 +35,6 @@ namespace pkp{

}
};

// struct ReactorFactory{
// static std::shared_ptr<Model> create(const std::string &model_type, const dvector &parameters){
// typedef std::unordered_map<std::string,std::function<std::shared_ptr<ReactorT<Model>>(const dvector &)>> registry_map;
// static registry_map mapping {
// {"SFOR", [](const dvector &p){return std::make_shared<ReactorT<SFOR>>(p);}},
// {"C2SM", [](const dvector &p){return std::make_shared<ReactorT<C2SM>>(p);}},
// };
// auto it = mapping.find(model_type);
// if (it != mapping.end()){
// return it->second(parameters);
// }
// else
// return nullptr;
//
// }
// };
} // end namespace pkp

#endif /* ReactorFactory_hpp */
5 changes: 4 additions & 1 deletion src/reactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ namespace pkp{
}

Reactor::Reactor(const std::string& modelType, dvector parameters):
model(ModelFactory2::create(modelType, parameters)){}
model(ModelFactory::create(modelType, parameters)){}

Reactor::Reactor(const std::string& modelType):
model(ModelFactory::create(modelType)){}

Reactor::~Reactor(){}

Expand Down
16 changes: 2 additions & 14 deletions src/reactor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
#ifndef reactor_hpp
#define reactor_hpp

//#include <memory>
#include "model.hpp"
#include "SFOR.hpp"
#include "C2SM.hpp"
//#include "ModelFactory.hpp"
#include "ReactorFactory.hpp"
#include "ModelFactory.hpp"
#include <iostream>
#include <string>
#include "algorithm"
Expand Down Expand Up @@ -64,17 +62,7 @@ namespace pkp {
public:
//enum models {sfor, c2sm};
Reactor(const std::string& modelType, dvector parameters);
// Reactor(models modelType)
// {
// switch (modelType)
// {
// case sfor:
// model = new SFOR(); break;
// case c2sm:
// model = new C2SM(); break;
// }
// //throw 0;
// }
Reactor(const std::string& modelType);
~Reactor();
void solve(double dt=1e-4, bool verbose=false);
void solve(std::vector<std::vector<double>> points, double dt=1e-4, bool verbose=false);
Expand Down
8 changes: 6 additions & 2 deletions test/test_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ int main(int argc, char* argv[])
auto model = ModelFactory2::create("SFOR", parameters);
model->printParameters();

auto model1 = ModelFactory::instantiate("SFOR");
auto model1 = ModelFactory::create("C2SM");
model1->printParameters();


auto model2 = ModelFactory::create("SFOR", parameters);
model2->printParameters();

std::cout << *model << "\n";
std::cout << *model1 << "\n";
return 0;
}
8 changes: 6 additions & 2 deletions test_pkp.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import pkp
import pkpc
import numpy as np
import matplotlib.pyplot as plt
from dualplot import DualPlot

reactor = pkp.Reactor('SFOR', [1e6, 50e6, 0.5])
reactor = pkpc.Reactor('C2SM')

print('C2SM', reactor.parameters)

reactor = pkpc.Reactor('SFOR', [1e6, 50e6, 0.5])

print(reactor.parameters)

Expand Down

0 comments on commit c05ae13

Please sign in to comment.