Skip to content

Commit

Permalink
Remove redundant unique_name attribute
Browse files Browse the repository at this point in the history
* Resolve conflation of gas.ID and gas.name in unit tests
* Also fixes #691
  • Loading branch information
Ingmar Schoegl committed Aug 16, 2019
1 parent 5dbaca2 commit a20ae78
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 44 deletions.
12 changes: 11 additions & 1 deletion include/cantera/thermo/Phase.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,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 CT_PHASE_H
#define CT_PHASE_H
Expand All @@ -29,6 +29,8 @@ namespace Cantera
* support thermodynamic calculations (see \ref thermoprops).
*/

class SolutionBase;

//! Class Phase is the base class for phases of matter, managing the species and
//! elements in a phase, as well as the independent variables of temperature,
//! mass density, species mass/mole fraction, and other generalized forces and
Expand Down Expand Up @@ -747,6 +749,11 @@ class Phase
//! change in state is detected
virtual void invalidateCache();

//! Set root SolutionBase holding all phase information
virtual void setRoot(std::shared_ptr<SolutionBase> root) {
m_root = root;
}

protected:
//! Cached for saved calculations within each ThermoPhase.
/*!
Expand Down Expand Up @@ -848,6 +855,9 @@ class Phase

//! Entropy at 298.15 K and 1 bar of stable state pure elements (J kmol-1)
vector_fp m_entropy298;

//! reference to SolutionBase
std::weak_ptr<SolutionBase> m_root;
};

}
Expand Down
12 changes: 1 addition & 11 deletions include/cantera/thermo/ThermoPhase.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

// 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.

#ifndef CT_THERMOPHASE_H
#define CT_THERMOPHASE_H
Expand Down Expand Up @@ -40,8 +40,6 @@ const int cSS_CONVENTION_VPSS = 1;
const int cSS_CONVENTION_SLAVE = 2;
//@}

class SolutionBase;

//! Base class for a phase with thermodynamic properties.
/*!
* Class ThermoPhase is the base class for the family of classes that represent
Expand Down Expand Up @@ -1618,11 +1616,6 @@ class ThermoPhase : public Phase

//@}

//! Set root SolutionBase holding all phase information
virtual void setRoot(std::shared_ptr<SolutionBase> root) {
m_root = root;
}

protected:
//! Fills `names` and `data` with the column names and species thermo
//! properties to be included in the output of the reportCSV method.
Expand Down Expand Up @@ -1667,9 +1660,6 @@ class ThermoPhase : public Phase

//! last value of the temperature processed by reference state
mutable doublereal m_tlast;

//! reference to SolutionBase
std::weak_ptr<SolutionBase> m_root;
};

//! typedef for the ThermoPhase class
Expand Down
2 changes: 1 addition & 1 deletion interfaces/cython/cantera/_cantera.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -941,8 +941,8 @@ cdef class GasTransportData:
cdef _assign(self, shared_ptr[CxxTransportData] other)

cdef class _SolutionBase:
cdef CxxSolutionBase* base
cdef shared_ptr[CxxSolutionBase] _base
cdef CxxSolutionBase* base
cdef shared_ptr[CxxThermoPhase] _thermo
cdef CxxThermoPhase* thermo
cdef shared_ptr[CxxKinetics] _kinetics
Expand Down
20 changes: 13 additions & 7 deletions interfaces/cython/cantera/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,28 @@ cdef class _SolutionBase:
else:
raise ValueError('Missing required keyword `base_type`.')

phasename = pystr(self.thermo.name())
phase_name = pystr(self.thermo.id())
name = kwargs.get('name', None)
if name is not None:
self.unique_name = name
self.name = name
elif phase_name in _phase_counts:
_phase_counts[phase_name] += 1
n = _phase_counts[phase_name]
self.name = '{0}_{1}'.format(phase_name, n)
else:
_phase_counts[phasename] += 1
n = _phase_counts[phasename]
self.unique_name = '{0}_{1}'.format(phasename, n)
_phase_counts[phase_name] = 0
self.name = phase_name

property type:
"""The type of the SolutionBase object."""
def __get__(self):
return pystr(self.base.type())

property unique_name:
"""The unique name of the SolutionBase object."""
property name:
"""
The name assigned to this SolutionBase object. The default is
taken from the CTI/XML/YAML input file.
"""
def __get__(self):
return pystr(self.base.name())

Expand Down
5 changes: 3 additions & 2 deletions interfaces/cython/cantera/test/test_mixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ def test_charge(self):

def test_phase_moles(self):
M = self.mix.phase_moles()
name = self.phase2.name
self.assertEqual(M[0], self.mix.phase_moles(0))
self.assertEqual(M[1], self.mix.phase_moles('air'))
self.assertEqual(M[1], self.mix.phase_moles(name))

self.mix.set_phase_moles('air', 4)
self.mix.set_phase_moles(name, 4)
self.assertEqual(self.mix.phase_moles(1), 4)

def test_species_moles(self):
Expand Down
12 changes: 4 additions & 8 deletions interfaces/cython/cantera/test/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def setUp(self):
def test_cpp_attributes(self):
self.assertTrue(isinstance(self.phase.type, str))
self.assertTrue(self.phase.type=='Solution')
self.assertTrue(isinstance(self.phase.unique_name, str))
self.phase.unique_name = 'spam'
self.assertTrue(self.phase.unique_name=='spam')
self.assertTrue(isinstance(self.phase.name, str))
self.phase.name = 'spam'
self.assertTrue(self.phase.name=='spam')
with self.assertRaises(AttributeError):
self.phase.type = 'eggs'

Expand Down Expand Up @@ -308,18 +308,14 @@ def test_default_report(self):
self.assertNotIn(name, report)

def test_name(self):
self.assertEqual(self.phase.name, 'ohmech')

self.phase.name = 'something'
self.assertEqual(self.phase.name, 'something')
self.assertIn('something', self.phase.report())

def test_ID(self):
self.assertEqual(self.phase.ID, 'ohmech')

self.phase.ID = 'something'
self.assertEqual(self.phase.ID, 'something')
self.assertEqual(self.phase.name, 'ohmech')

def test_badLength(self):
X = np.zeros(5)
Expand Down Expand Up @@ -854,7 +850,7 @@ class ImportTest(utilities.CanteraTest):
Test the various ways of creating a Solution object
"""
def check(self, gas, name, T, P, nSpec, nElem):
self.assertEqual(gas.name, name)
self.assertEqual(gas.ID, name)
self.assertNear(gas.T, T)
self.assertNear(gas.P, P)
self.assertEqual(gas.n_species, nSpec)
Expand Down
11 changes: 0 additions & 11 deletions interfaces/cython/cantera/thermo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,6 @@ cdef class ThermoPhase(_SolutionBase):
def __call__(self, *args, **kwargs):
print(self.report(*args, **kwargs))

property name:
"""
The name assigned to this phase. The default is taken from the CTI/XML
input file.
"""
def __get__(self):
return pystr(self.thermo.name())
def __set__(self, name):
self.thermo.setName(stringify(name))

property ID:
"""
The ID of the phase. The default is taken from the CTI/XML input file.
Expand All @@ -303,7 +293,6 @@ cdef class ThermoPhase(_SolutionBase):
def __set__(self, id_):
self.thermo.setID(stringify(id_))


property basis:
"""
Determines whether intensive thermodynamic properties are treated on a
Expand Down
17 changes: 14 additions & 3 deletions src/thermo/Phase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
*/

// 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.

#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 @@ -77,12 +78,22 @@ void Phase::setID(const std::string& id_)

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

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

size_t Phase::nElements() const
Expand Down
1 change: 1 addition & 0 deletions src/thermo/ThermoFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ void addSpecies(ThermoPhase& thermo, const AnyValue& names, const AnyValue& spec
void setupPhase(ThermoPhase& thermo, AnyMap& phaseNode, const AnyMap& rootNode)
{
thermo.setName(phaseNode["name"].asString());
thermo.setID(phaseNode["name"].asString());
if (rootNode.hasKey("__file__")) {
phaseNode["__file__"] = rootNode["__file__"];
}
Expand Down

0 comments on commit a20ae78

Please sign in to comment.