Skip to content

Commit 8066333

Browse files
author
Ingmar Schoegl
committed
Associate phase ID with SolutionBase object
1 parent a20ae78 commit 8066333

File tree

7 files changed

+97
-48
lines changed

7 files changed

+97
-48
lines changed

include/cantera/base/Base.h

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,42 @@ class SolutionBase : public std::enable_shared_from_this<SolutionBase>
3131

3232
//! String indicating the type of the SolutionBase object. Corresponds
3333
//! to the type of phase originally instantiated
34-
std::string type() const {
35-
return m_type;
36-
}
34+
std::string type() const;
3735

3836
//! Set the type of this SolutionBase object
39-
void setType(const std::string& type){
40-
m_type = type;
41-
}
37+
void setType(const std::string& type);
38+
39+
/*! Name and ID
40+
* Class SolutionBase references two strings that identify a SolutionBase.
41+
* The ID is the value of the ID attribute of the XML/YAML node that is used
42+
* to initialize a phase when it is read. The name field is also initialized
43+
* to the value of the ID attribute of the XML/YAML node.
44+
*
45+
* However, the name field may be changed to another value during the course
46+
* of a calculation. For example, if a SolutionBase is located in two places,
47+
* but has the same constitutive input, the IDs of the two SolutionBases
48+
* will be the same, but the names of the two SOlutionBases may be different.
49+
*
50+
* It is an error to have two phases in a single problem with the same name
51+
* and ID (or the name from one phase being the same as the id of another
52+
* SolutionBase). Thus, it is expected that there is a 1-1 correspondence
53+
* between names and unique SolutionBase objects within a Cantera problem.
54+
*/
55+
56+
//! Return the string id for the SolutionBase.
57+
std::string id() const;
58+
59+
//! Set the string id for the SolutionBase.
60+
void setID(const std::string& id);
4261

4362
//! Return the name of this SolutionBase object
44-
std::string name() const {
45-
return m_name;
46-
}
63+
std::string name() const;
4764

4865
//! Set the name of this SolutionBase object
49-
void setName(const std::string& name){
50-
m_name = name;
51-
}
66+
void setName(const std::string& name);
5267

5368
//! Generate self-documenting YAML string
54-
virtual std::string toYAML() const {
55-
throw NotImplementedError("SolutionBase::toYAML");
56-
}
69+
virtual std::string toYAML() const;
5770

5871
//! Set the ThermoPhase object
5972
void setThermoPhase(shared_ptr<ThermoPhase> thermo);
@@ -70,7 +83,6 @@ class SolutionBase : public std::enable_shared_from_this<SolutionBase>
7083
shared_ptr<Transport> m_transport; //! Transport manager
7184

7285
std::string m_type; //! type of SolutionBase object
73-
std::string m_name; //! name of SolutionBase object
7486
};
7587

7688
}

interfaces/cython/cantera/_cantera.pxd

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ cdef extern from "cantera/base/Base.h" namespace "Cantera":
126126
CxxSolutionBase()
127127
string type()
128128
string setType(string)
129+
string id()
130+
void setID(string)
129131
string name()
130132
void setName(string)
131133
void setThermoPhase(shared_ptr[CxxThermoPhase])
@@ -142,10 +144,6 @@ cdef extern from "cantera/thermo/ThermoPhase.h" namespace "Cantera":
142144
# miscellaneous
143145
string type()
144146
string report(cbool, double) except +translate_exception
145-
string name()
146-
void setName(string)
147-
string id()
148-
void setID(string)
149147
double minTemp() except +translate_exception
150148
double maxTemp() except +translate_exception
151149
double refPressure() except +translate_exception

interfaces/cython/cantera/base.pyx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ cdef class _SolutionBase:
6060
else:
6161
raise ValueError('Missing required keyword `base_type`.')
6262

63-
phase_name = pystr(self.thermo.id())
63+
phase_name = pystr(self.base.id())
6464
name = kwargs.get('name', None)
6565
if name is not None:
6666
self.name = name
@@ -73,10 +73,23 @@ cdef class _SolutionBase:
7373
self.name = phase_name
7474

7575
property type:
76-
"""The type of the SolutionBase object."""
76+
"""
77+
The type of the SolutionBase object. The type is set during object
78+
instantiation and cannot be modified.
79+
"""
7780
def __get__(self):
7881
return pystr(self.base.type())
7982

83+
property ID:
84+
"""
85+
The ID of the SolutionBase object. The default is taken from the
86+
CTI/XML/YAML input file.
87+
"""
88+
def __get__(self):
89+
return pystr(self.base.id())
90+
def __set__(self, id_):
91+
self.base.setID(stringify(id_))
92+
8093
property name:
8194
"""
8295
The name assigned to this SolutionBase object. The default is

interfaces/cython/cantera/thermo.pyx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,6 @@ cdef class ThermoPhase(_SolutionBase):
284284
def __call__(self, *args, **kwargs):
285285
print(self.report(*args, **kwargs))
286286

287-
property ID:
288-
"""
289-
The ID of the phase. The default is taken from the CTI/XML input file.
290-
"""
291-
def __get__(self):
292-
return pystr(self.thermo.id())
293-
def __set__(self, id_):
294-
self.thermo.setID(stringify(id_))
295-
296287
property basis:
297288
"""
298289
Determines whether intensive thermodynamic properties are treated on a

src/base/Base.cpp

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ SolutionBase::SolutionBase() :
1515
m_thermo(nullptr),
1616
m_kinetics(nullptr),
1717
m_transport(nullptr),
18-
m_type("<SolutionBase_type>"),
19-
m_name("<unique_name>")
18+
m_type("<SolutionBase_type>")
2019
{}
2120

2221
SolutionBase::SolutionBase(const std::string& infile,
@@ -27,6 +26,53 @@ SolutionBase::SolutionBase(const std::string& infile,
2726
throw NotImplementedError("SolutionBase constructor from file");
2827
}
2928

29+
std::string SolutionBase::type() const {
30+
return m_type;
31+
}
32+
33+
void SolutionBase::setType(const std::string& type){
34+
m_type = type;
35+
}
36+
37+
std::string SolutionBase::id() const {
38+
// currently managed by ThermoPhase
39+
if (m_thermo) {
40+
return m_thermo->id();
41+
} else {
42+
throw CanteraError("SolutionBase::id()", "Missing ThermoPhase.");
43+
}
44+
}
45+
46+
void SolutionBase::setID(const std::string& id) {
47+
// currently managed by ThermoPhase
48+
if (m_thermo) {
49+
return m_thermo->setID(id);
50+
} else {
51+
throw CanteraError("SolutionBase::setID()", "Missing ThermoPhase.");
52+
}
53+
}
54+
55+
std::string SolutionBase::name() const {
56+
// currently managed by ThermoPhase
57+
if (m_thermo) {
58+
return m_thermo->name();
59+
} else {
60+
throw CanteraError("SolutionBase::name()", "Missing ThermoPhase.");
61+
}
62+
}
63+
64+
void SolutionBase::setName(const std::string& name){
65+
// currently managed by ThermoPhase
66+
if (m_thermo) {
67+
return m_thermo->setName(name);
68+
} else {
69+
throw CanteraError("SolutionBase::setName()", "Missing ThermoPhase.");
70+
}
71+
}
72+
73+
std::string SolutionBase::toYAML() const {
74+
throw NotImplementedError("SolutionBase::toYAML");
75+
}
3076

3177
void SolutionBase::setThermoPhase(shared_ptr<ThermoPhase> thermo) {
3278
m_thermo = thermo;

src/thermo/Phase.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
*/
55

66
// This file is part of Cantera. See License.txt in the top-level directory or
7-
// at https://cantera.org/license.txt for license and copyright information.
7+
// at http://www.cantera.org/license.txt for license and copyright information.
88

99
#include "cantera/thermo/Phase.h"
1010
#include "cantera/base/utilities.h"
1111
#include "cantera/base/stringUtils.h"
1212
#include "cantera/base/ctml.h"
13-
#include "cantera/base/Base.h"
1413
#include "cantera/thermo/ThermoFactory.h"
1514

1615
using namespace std;
@@ -78,22 +77,12 @@ void Phase::setID(const std::string& id_)
7877

7978
std::string Phase::name() const
8079
{
81-
if (m_root.expired()) {
82-
return m_name;
83-
} else {
84-
auto root = m_root.lock();
85-
return root->name();
86-
}
80+
return m_name;
8781
}
8882

8983
void Phase::setName(const std::string& nm)
9084
{
91-
if (m_root.expired()) {
92-
m_name = nm;
93-
} else {
94-
auto root = m_root.lock();
95-
root->setName(nm);
96-
}
85+
m_name = nm;
9786
}
9887

9988
size_t Phase::nElements() const

src/thermo/ThermoFactory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
// This file is part of Cantera. See License.txt in the top-level directory or
8-
// at http://www.cantera.org/license.txt for license and copyright information.
8+
// at https://cantera.org/license.txt for license and copyright information.
99

1010
#include "cantera/thermo/ThermoFactory.h"
1111

0 commit comments

Comments
 (0)