Cantera
Loading...
Searching...
No Matches
ReactorFactory.cpp
Go to the documentation of this file.
1//! @file ReactorFactory.cpp
2
3// This file is part of Cantera. See License.txt in the top-level directory or
4// at https://cantera.org/license.txt for license and copyright information.
5
15#include "cantera/zeroD/IdealGasConstPressureReactor.h"
18
19namespace Cantera
20{
21
22ReactorFactory* ReactorFactory::s_factory = 0;
23std::mutex ReactorFactory::reactor_mutex;
24
25ReactorFactory::ReactorFactory()
26{
27 reg("Reservoir",
28 [](shared_ptr<Solution> sol, bool clone, const string& name)
29 { return new Reservoir(sol, clone, name); });
30 reg("Reactor",
31 [](shared_ptr<Solution> sol, bool clone, const string& name)
32 { return new Reactor(sol, clone, name); });
33 reg("ConstPressureReactor",
34 [](shared_ptr<Solution> sol, bool clone, const string& name)
35 { return new ConstPressureReactor(sol, clone, name); });
36 reg("FlowReactor",
37 [](shared_ptr<Solution> sol, bool clone, const string& name)
38 { return new FlowReactor(sol, clone, name); });
39 reg("IdealGasReactor",
40 [](shared_ptr<Solution> sol, bool clone, const string& name)
41 { return new IdealGasReactor(sol, clone, name); });
42 reg("IdealGasConstPressureReactor",
43 [](shared_ptr<Solution> sol, bool clone, const string& name)
44 { return new IdealGasConstPressureReactor(sol, clone, name); });
45 reg("ExtensibleReactor",
46 [](shared_ptr<Solution> sol, bool clone, const string& name)
47 { return new ReactorDelegator<Reactor>(sol, clone, name); });
48 reg("ExtensibleIdealGasReactor",
49 [](shared_ptr<Solution> sol, bool clone, const string& name)
50 { return new ReactorDelegator<IdealGasReactor>(sol, clone, name); });
51 reg("ExtensibleConstPressureReactor",
52 [](shared_ptr<Solution> sol, bool clone, const string& name)
53 { return new ReactorDelegator<ConstPressureReactor>(sol, clone, name); });
54 reg("ExtensibleIdealGasConstPressureReactor",
55 [](shared_ptr<Solution> sol, bool clone, const string& name)
56 { return new ReactorDelegator<IdealGasConstPressureReactor>(sol, clone, name); });
57 reg("ExtensibleMoleReactor",
58 [](shared_ptr<Solution> sol, bool clone, const string& name)
59 { return new ReactorDelegator<MoleReactor>(sol, clone, name); });
60 reg("ExtensibleConstPressureMoleReactor",
61 [](shared_ptr<Solution> sol, bool clone, const string& name)
62 { return new ReactorDelegator<ConstPressureMoleReactor>(sol, clone, name); });
63 reg("ExtensibleIdealGasMoleReactor",
64 [](shared_ptr<Solution> sol, bool clone, const string& name)
65 { return new ReactorDelegator<IdealGasMoleReactor>(sol, clone, name); });
66 reg("ExtensibleIdealGasConstPressureMoleReactor",
67 [](shared_ptr<Solution> sol, bool clone, const string& name)
68 { return new ReactorDelegator<IdealGasConstPressureMoleReactor>(sol, clone, name); });
69 reg("IdealGasConstPressureMoleReactor",
70 [](shared_ptr<Solution> sol, bool clone, const string& name)
71 { return new IdealGasConstPressureMoleReactor(sol, clone, name); });
72 reg("IdealGasMoleReactor",
73 [](shared_ptr<Solution> sol, bool clone, const string& name)
74 { return new IdealGasMoleReactor(sol, clone, name); });
75 reg("ConstPressureMoleReactor",
76 [](shared_ptr<Solution> sol, bool clone, const string& name)
77 { return new ConstPressureMoleReactor(sol, clone, name); });
78 reg("MoleReactor",
79 [](shared_ptr<Solution> sol, bool clone, const string& name)
80 { return new MoleReactor(sol, clone, name); });
81}
82
83ReactorFactory* ReactorFactory::factory() {
84 std::unique_lock<std::mutex> lock(reactor_mutex);
85 if (!s_factory) {
86 s_factory = new ReactorFactory;
87 }
88 return s_factory;
89}
90
92 std::unique_lock<std::mutex> lock(reactor_mutex);
93 delete s_factory;
94 s_factory = 0;
95}
96
97// ---------- ReactorSurfaceFactory methods ----------
98
99ReactorSurfaceFactory* ReactorSurfaceFactory::s_factory = 0;
100std::mutex ReactorSurfaceFactory::s_mutex;
101
102ReactorSurfaceFactory* ReactorSurfaceFactory::factory() {
103 std::unique_lock<std::mutex> lock(s_mutex);
104 if (!s_factory) {
105 s_factory = new ReactorSurfaceFactory;
106 }
107 return s_factory;
108}
109
111 std::unique_lock<std::mutex> lock(s_mutex);
112 delete s_factory;
113 s_factory = 0;
114}
115
116ReactorSurfaceFactory::ReactorSurfaceFactory()
117{
118 reg("ReactorSurface",
119 [](shared_ptr<Solution> surf, span<shared_ptr<ReactorBase>> reactors,
120 bool clone, const string& name)
121 { return new ReactorSurface(surf, reactors, clone, name); });
122 reg("MoleReactorSurface",
123 [](shared_ptr<Solution> surf, span<shared_ptr<ReactorBase>> reactors,
124 bool clone, const string& name)
125 { return new MoleReactorSurface(surf, reactors, clone, name); });
126 reg("FlowReactorSurface",
127 [](shared_ptr<Solution> surf, span<shared_ptr<ReactorBase>> reactors,
128 bool clone, const string& name)
129 { return new FlowReactorSurface(surf, reactors, clone, name); });
130 reg("ExtensibleReactorSurface",
131 [](shared_ptr<Solution> surf, span<shared_ptr<ReactorBase>> reactors,
132 bool clone, const string& name)
133 { return new ReactorDelegator<ReactorSurface>(surf, reactors, clone, name); });
134 reg("ExtensibleMoleReactorSurface",
135 [](shared_ptr<Solution> surf, span<shared_ptr<ReactorBase>> reactors,
136 bool clone, const string& name)
137 { return new ReactorDelegator<MoleReactorSurface>(surf, reactors, clone, name); });
138 reg("ExtensibleFlowReactorSurface",
139 [](shared_ptr<Solution> surf, span<shared_ptr<ReactorBase>> reactors,
140 bool clone, const string& name)
141 { return new ReactorDelegator<FlowReactorSurface>(surf, reactors, clone, name); });
142}
143
144// ---------- free functions ----------
145
146shared_ptr<ReactorBase> newReactorBase(
147 const string& model, shared_ptr<Solution> phase, bool clone, const string& name)
148{
149 return shared_ptr<ReactorBase>(
150 ReactorFactory::factory()->create(model, phase, clone, name));
151}
152
153shared_ptr<Reactor> newReactor(
154 const string& model, shared_ptr<Solution> phase, bool clone, const string& name)
155{
156 auto reactor = std::dynamic_pointer_cast<Reactor>(
157 newReactorBase(model, phase, clone, name));
158 if (!reactor) {
159 throw CanteraError("newReactor4",
160 "Model type '{}' does not specify a bulk reactor.", model);
161 }
162 return reactor;
163}
164
165shared_ptr<Reservoir> newReservoir(
166 shared_ptr<Solution> phase, bool clone, const string& name)
167{
168 auto reservoir = std::dynamic_pointer_cast<Reservoir>(
169 newReactorBase("Reservoir", phase, clone, name));
170 if (!reservoir) {
171 throw CanteraError("newReservoir",
172 "Caught unexpected inconsistency in factory method.");
173 }
174 return reservoir;
175}
176
177shared_ptr<ReactorSurface> newReactorSurface(shared_ptr<Solution> phase,
178 span<shared_ptr<ReactorBase>> reactors, bool clone, const string& name)
179{
180 if (reactors.empty()) {
181 throw CanteraError("newReactorSurface",
182 "At least one adjacent reactor must be specified.");
183 }
184 string model = "ReactorSurface";
185 if (std::dynamic_pointer_cast<FlowReactor>(reactors[0])) {
186 model = "FlowReactorSurface";
187 }
188 for (const auto& r : reactors) {
189 if (std::dynamic_pointer_cast<MoleReactor>(r)) {
190 model = "MoleReactorSurface";
191 break;
192 }
193 }
194 return shared_ptr<ReactorSurface>(ReactorSurfaceFactory::factory()->create(
195 model, phase, reactors, clone, name));
196}
197
198shared_ptr<ReactorSurface> newReactorSurface(const string& model,
199 shared_ptr<Solution> phase, span<shared_ptr<ReactorBase>> reactors,
200 bool clone, const string& name)
201{
202 return shared_ptr<ReactorSurface>(ReactorSurfaceFactory::factory()->create(
203 model, phase, reactors, clone, name));
204}
205
206}
Base class for exceptions thrown by Cantera classes.
void reg(const string &name, function< ReactorBase *(Args...)> f)
Definition FactoryBase.h:80
Factory class to create reactor objects.
void deleteFactory() override
Virtual abstract function that deletes the factory.
Factory class to create ReactorSurface objects.
void deleteFactory() override
Virtual abstract function that deletes the factory.
A surface where reactions can occur that is in contact with the bulk fluid of a Reactor.
shared_ptr< ReactorSurface > newReactorSurface(shared_ptr< Solution > phase, span< shared_ptr< ReactorBase > > reactors, bool clone, const string &name)
Create a ReactorSurface object with the specified contents and adjacent reactors participating in sur...
shared_ptr< Reactor > newReactor(const string &model, shared_ptr< Solution > phase, bool clone, const string &name)
Create a Reactor object of the specified type and contents.
shared_ptr< ReactorBase > newReactorBase(const string &model, shared_ptr< Solution > phase, bool clone, const string &name)
Create a ReactorBase object of the specified type and contents.
shared_ptr< Reservoir > newReservoir(shared_ptr< Solution > phase, bool clone, const string &name)
Create a Reservoir object with the specified contents.
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595