Skip to content

Commit

Permalink
Associate phase ID with SolutionBase object
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingmar Schoegl committed Aug 17, 2019
1 parent a20ae78 commit 037f2d0
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 48 deletions.
44 changes: 28 additions & 16 deletions include/cantera/base/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,42 @@ class SolutionBase : public std::enable_shared_from_this<SolutionBase>

//! String indicating the type of the SolutionBase object. Corresponds
//! to the type of phase originally instantiated
std::string type() const {
return m_type;
}
std::string type() const;

//! Set the type of this SolutionBase object
void setType(const std::string& type){
m_type = type;
}
void setType(const std::string& type);

/*! Name and ID
* Class SolutionBase references two strings that identify a SolutionBase.
* The ID is the value of the ID attribute of the XML/YAML node that is used
* to initialize a phase when it is read. The name field is also initialized
* to the value of the ID attribute of the XML/YAML node.
*
* However, the name field may be changed to another value during the course
* of a calculation. For example, if a SolutionBase is located in two places,
* but has the same constitutive input, the IDs of the two SolutionBases
* will be the same, but the names of the two SOlutionBases may be different.
*
* It is an error to have two phases in a single problem with the same name
* and ID (or the name from one phase being the same as the id of another
* SolutionBase). Thus, it is expected that there is a 1-1 correspondence
* between names and unique SolutionBase objects within a Cantera problem.
*/

//! Return the string id for the SolutionBase.
std::string id() const;

//! Set the string id for the SolutionBase.
void setID(const std::string& id);

//! Return the name of this SolutionBase object
std::string name() const {
return m_name;
}
std::string name() const;

//! Set the name of this SolutionBase object
void setName(const std::string& name){
m_name = name;
}
void setName(const std::string& name);

//! Generate self-documenting YAML string
virtual std::string toYAML() const {
throw NotImplementedError("SolutionBase::toYAML");
}
virtual std::string toYAML() const;

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

std::string m_type; //! type of SolutionBase object
std::string m_name; //! name of SolutionBase object
};

}
Expand Down
2 changes: 1 addition & 1 deletion include/cantera/thermo/ThermoFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

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

#ifndef THERMO_FACTORY_H
#define THERMO_FACTORY_H
Expand Down
6 changes: 2 additions & 4 deletions interfaces/cython/cantera/_cantera.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ cdef extern from "cantera/base/Base.h" namespace "Cantera":
CxxSolutionBase()
string type()
string setType(string)
string id()
void setID(string)
string name()
void setName(string)
void setThermoPhase(shared_ptr[CxxThermoPhase])
Expand All @@ -142,10 +144,6 @@ cdef extern from "cantera/thermo/ThermoPhase.h" namespace "Cantera":
# miscellaneous
string type()
string report(cbool, double) except +translate_exception
string name()
void setName(string)
string id()
void setID(string)
double minTemp() except +translate_exception
double maxTemp() except +translate_exception
double refPressure() except +translate_exception
Expand Down
17 changes: 15 additions & 2 deletions interfaces/cython/cantera/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ cdef class _SolutionBase:
else:
raise ValueError('Missing required keyword `base_type`.')

phase_name = pystr(self.thermo.id())
phase_name = pystr(self.base.id())
name = kwargs.get('name', None)
if name is not None:
self.name = name
Expand All @@ -73,10 +73,23 @@ cdef class _SolutionBase:
self.name = phase_name

property type:
"""The type of the SolutionBase object."""
"""
The type of the SolutionBase object. The type is set during object
instantiation and cannot be modified.
"""
def __get__(self):
return pystr(self.base.type())

property ID:
"""
The ID of the SolutionBase object. The default is taken from the
CTI/XML/YAML input file.
"""
def __get__(self):
return pystr(self.base.id())
def __set__(self, id_):
self.base.setID(stringify(id_))

property name:
"""
The name assigned to this SolutionBase object. The default is
Expand Down
9 changes: 0 additions & 9 deletions interfaces/cython/cantera/thermo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,6 @@ cdef class ThermoPhase(_SolutionBase):
def __call__(self, *args, **kwargs):
print(self.report(*args, **kwargs))

property ID:
"""
The ID of the phase. The default is taken from the CTI/XML input file.
"""
def __get__(self):
return pystr(self.thermo.id())
def __set__(self, id_):
self.thermo.setID(stringify(id_))

property basis:
"""
Determines whether intensive thermodynamic properties are treated on a
Expand Down
50 changes: 48 additions & 2 deletions src/base/Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ SolutionBase::SolutionBase() :
m_thermo(nullptr),
m_kinetics(nullptr),
m_transport(nullptr),
m_type("<SolutionBase_type>"),
m_name("<unique_name>")
m_type("<SolutionBase_type>")
{}

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

std::string SolutionBase::type() const {
return m_type;
}

void SolutionBase::setType(const std::string& type){
m_type = type;
}

std::string SolutionBase::id() const {
// currently managed by ThermoPhase
if (m_thermo) {
return m_thermo->id();
} else {
throw CanteraError("SolutionBase::id()", "Missing ThermoPhase.");
}
}

void SolutionBase::setID(const std::string& id) {
// currently managed by ThermoPhase
if (m_thermo) {
return m_thermo->setID(id);
} else {
throw CanteraError("SolutionBase::setID()", "Missing ThermoPhase.");
}
}

std::string SolutionBase::name() const {
// currently managed by ThermoPhase
if (m_thermo) {
return m_thermo->name();
} else {
throw CanteraError("SolutionBase::name()", "Missing ThermoPhase.");
}
}

void SolutionBase::setName(const std::string& name){
// currently managed by ThermoPhase
if (m_thermo) {
return m_thermo->setName(name);
} else {
throw CanteraError("SolutionBase::setName()", "Missing ThermoPhase.");
}
}

std::string SolutionBase::toYAML() const {
throw NotImplementedError("SolutionBase::toYAML");
}

void SolutionBase::setThermoPhase(shared_ptr<ThermoPhase> thermo) {
m_thermo = thermo;
Expand Down
17 changes: 3 additions & 14 deletions src/thermo/Phase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
*/

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

#include "cantera/thermo/Phase.h"
#include "cantera/base/utilities.h"
#include "cantera/base/stringUtils.h"
#include "cantera/base/ctml.h"
#include "cantera/base/Base.h"
#include "cantera/thermo/ThermoFactory.h"

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

std::string Phase::name() const
{
if (m_root.expired()) {
return m_name;
} else {
auto root = m_root.lock();
return root->name();
}
return m_name;
}

void Phase::setName(const std::string& nm)
{
if (m_root.expired()) {
m_name = nm;
} else {
auto root = m_root.lock();
root->setName(nm);
}
m_name = nm;
}

size_t Phase::nElements() const
Expand Down

0 comments on commit 037f2d0

Please sign in to comment.