Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Thermo] Add property to call species charges in phase #863

Merged
merged 9 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ John Hewson (@jchewson), Sandia National Laboratory
Trevor Hickey (@luxe)
Yuanjie Jiang
Benjamin Kee (@lionkey)
Daniel Korff (@korffdm), Colorado School of Mines
Jon Kristofer
Samesh Lakothia (@sameshl)
Kyle Linevitch, Jr. (@KyleLinevitchJr)
Expand Down
1 change: 1 addition & 0 deletions include/cantera/clib/ct.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extern "C" {
CANTERA_CAPI int thermo_setMassFractionsByName(int n, const char* y);
CANTERA_CAPI int thermo_getAtomicWeights(int n, size_t lenm, double* atw);
CANTERA_CAPI int thermo_getMolecularWeights(int n, size_t lenm, double* mw);
CANTERA_CAPI int thermo_getSpeciesCharges(int n, size_t lenm, double* sc);
CANTERA_CAPI int thermo_getElementName(int n, size_t k, size_t lennm, char* nm);
CANTERA_CAPI int thermo_getSpeciesName(int n, size_t m, size_t lennm, char* nm);
CANTERA_CAPI int thermo_getName(int n, size_t lennm, char* nm);
Expand Down
1 change: 1 addition & 0 deletions include/cantera/cython/wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ THERMO_1D(getConcentrations)
THERMO_1D(setConcentrations)

THERMO_1D(getMolecularWeights)
THERMO_1D(getSpeciesCharges)
THERMO_1D(getChemPotentials)
THERMO_1D(getElectrochemPotentials)
THERMO_1D(getPartialMolarEnthalpies)
Expand Down
4 changes: 4 additions & 0 deletions include/cantera/thermo/Phase.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ class Phase
//! units = kg / kmol
const vector_fp& molecularWeights() const;

//! Copy the vector of species charges into array charges.
//! @param charges Output array of species charges (elem. charge)
const vector_fp& getSpeciesCharges(double* charges) const;
speth marked this conversation as resolved.
Show resolved Hide resolved

/// @name Composition
//@{

Expand Down
1 change: 1 addition & 0 deletions interfaces/cython/cantera/_cantera.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ cdef extern from "cantera/cython/wrappers.h":

# other ThermoPhase methods
cdef void thermo_getMolecularWeights(CxxThermoPhase*, double*) except +translate_exception
cdef void thermo_getSpeciesCharges(CxxThermoPhase*, double*) except +translate_exception

# Kinetics per-reaction properties
cdef void kin_getFwdRatesOfProgress(CxxKinetics*, double*) except +translate_exception
Expand Down
2 changes: 1 addition & 1 deletion interfaces/cython/cantera/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ class SolutionArray:
'element_name', 'element_names', 'atomic_weight', 'atomic_weights',
'n_species', 'species_name', 'species_names', 'species_index',
'species', 'n_atoms', 'molecular_weights', 'min_temp', 'max_temp',
'reference_pressure',
'reference_pressure', 'species_charges',
# From Kinetics
'n_total_species', 'n_reactions', 'n_phases', 'reaction_phase_index',
'kinetics_species_index', 'reaction', 'reactions', 'modify_reaction',
Expand Down
11 changes: 10 additions & 1 deletion interfaces/cython/cantera/test/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ def test_weights(self):
test_weight += aw * self.phase.n_atoms(i,j)
self.assertNear(test_weight, mw)

def test_charges(self):
gas = ct.Solution('ch4_ion.yaml')
charges = gas.species_charges
test = {'E': -1., 'N2': 0., 'H3O+': 1.}
for species, charge in test.items():
self.assertIn(species, gas.species_names)
index = gas.species_index(species)
self.assertEqual(charges[index], charge)

def test_setComposition(self):
X = np.zeros(self.phase.n_species)
X[2] = 1.0
Expand Down Expand Up @@ -1657,7 +1666,7 @@ def test_extra(self):
states = ct.SolutionArray(self.gas, 10, extra=extra)
keys = list(states._extra.keys())
self.assertEqual(keys[0], 'grid')

with self.assertRaises(ValueError):
states = ct.SolutionArray(self.gas, extra=['creation_rates'])

Expand Down
5 changes: 5 additions & 0 deletions interfaces/cython/cantera/thermo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,11 @@ cdef class ThermoPhase(_SolutionBase):
def __get__(self):
return self._getArray1(thermo_getMolecularWeights)

property species_charges:
"""Array of species charges [-]."""
speth marked this conversation as resolved.
Show resolved Hide resolved
def __get__(self):
return self._getArray1(thermo_getSpeciesCharges)

property mean_molecular_weight:
"""The mean molecular weight (molar mass) [kg/kmol]."""
def __get__(self):
Expand Down
12 changes: 12 additions & 0 deletions interfaces/matlab/toolbox/@ThermoPhase/speciesCharges.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function ch = speciesCharges(tp)
% SPECIESCHARGES Get the elementary charge of the species.
speth marked this conversation as resolved.
Show resolved Hide resolved
% x = speciesCharges(tp)
%
% :param tp:
% Instance of class :mat:func:`ThermoPhase` (or another
% object that derives from ThermoPhase)
% :return:
% Vector of species charges. Units: elem. charge
%

ch = phase_get(tp.tp_id, 23);
12 changes: 12 additions & 0 deletions src/clib/ct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,18 @@ extern "C" {
}
}

int thermo_getSpeciesCharges(int n, size_t lenm, double* sc)
{
try {
ThermoPhase& p = ThermoCabinet::item(n);
p.checkSpeciesArraySize(lenm);
const vector_fp& ch = p.getSpeciesCharges(sc);
return 0;
} catch (...) {
return handleAllExceptions(-1, ERR);
}
}

int thermo_getName(int n, size_t lennm, char* nm)
{
try {
Expand Down
3 changes: 3 additions & 0 deletions src/matlab/phasemethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ void phasemethods(int nlhs, mxArray* plhs[],
case 22:
iok = thermo_getMolecularWeights(ph,nsp, &x[0]);
break;
case 23:
iok = thermo_getSpeciesCharges(ph, nsp, &x[0]);
break;
speth marked this conversation as resolved.
Show resolved Hide resolved
default:
mexErrMsgTxt("Unknown job number");
}
Expand Down
7 changes: 7 additions & 0 deletions src/thermo/Phase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,13 @@ const vector_fp& Phase::molecularWeights() const
return m_molwts;
}

const vector_fp& Phase::getSpeciesCharges(double* charges) const
{
const vector_fp& ch = m_speciesCharge;
copy(ch.begin(), ch.end(), charges);
speth marked this conversation as resolved.
Show resolved Hide resolved
return m_speciesCharge;
}

compositionMap Phase::getMoleFractionsByName(double threshold) const
{
compositionMap comp;
Expand Down