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

[samples] Fix heat release in ic_engine.py #820

Merged
merged 3 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions interfaces/cython/cantera/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,15 @@ def __init__(self, phase, shape=(0,), states=None, extra=None):
self._indices = list(np.ndindex(self._shape))
self._output_dummy = self._states[..., 0]

reserved = self.__dir__()

self._extra = {}
if isinstance(extra, dict):
for name, v in extra.items():
if name in reserved:
raise ValueError(
"Unable to create extra column '{}': name is already "
"used by SolutionArray objects.".format(name))
if not np.shape(v):
self._extra[name] = [v]*self._shape[0]
elif len(v) == self._shape[0]:
Expand All @@ -526,6 +532,10 @@ def __init__(self, phase, shape=(0,), states=None, extra=None):

elif extra and self._shape == (0,):
for name in extra:
if name in reserved:
raise ValueError(
"Unable to create extra column '{}': name is already "
"used by SolutionArray objects.".format(name))
self._extra[name] = []

elif extra:
Expand Down
11 changes: 4 additions & 7 deletions interfaces/cython/cantera/examples/reactors/ic_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def piston_speed(t):
# set up output data arrays
states = ct.SolutionArray(
cyl.thermo,
extra=('t', 'ca', 'V', 'm', 'mdot_in', 'mdot_out', 'dWv_dt', 'heat_release_rate'),
extra=('t', 'ca', 'V', 'm', 'mdot_in', 'mdot_out', 'dWv_dt'),
)

# simulate with a maximum resolution of 1 deg crank angle
Expand All @@ -169,17 +169,14 @@ def piston_speed(t):
# calculate results to be stored
dWv_dt = - (cyl.thermo.P - ambient_air.thermo.P) * A_piston * \
piston_speed(sim.time)
heat_release_rate = - cyl.volume * ct.gas_constant * cyl.T * \
np.sum(gas.standard_enthalpies_RT * cyl.thermo.net_production_rates, 0)

# append output data
states.append(cyl.thermo.state,
t=sim.time, ca=crank_angle(sim.time),
V=cyl.volume, m=cyl.mass,
mdot_in=inlet_valve.mdot(sim.time),
mdot_out=outlet_valve.mdot(sim.time),
dWv_dt=dWv_dt,
heat_release_rate=heat_release_rate)
dWv_dt=dWv_dt)


#######################################################################
Expand Down Expand Up @@ -221,7 +218,7 @@ def ca_ticks(t):

# heat of reaction and expansion work
fig, ax = plt.subplots()
ax.plot(t, 1.e-3 * states.heat_release_rate, label=r'$\dot{Q}$')
ax.plot(t, 1.e-3 * states.heat_release_rate * states.V, label=r'$\dot{Q}$')
ax.plot(t, 1.e-3 * states.dWv_dt, label=r'$\dot{W}_v$')
ax.set_ylim(-1e2, 1e3)
ax.legend(loc=0)
Expand All @@ -247,7 +244,7 @@ def ca_ticks(t):
######################################################################

# heat release
Q = trapz(states.heat_release_rate, t)
Q = trapz(states.heat_release_rate * states.V, t)
print('{:45s}{:>4.1f} kW'.format('Heat release rate per cylinder (estimate):',
Q / t[-1] / 1000.))

Expand Down
4 changes: 4 additions & 0 deletions interfaces/cython/cantera/test/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,10 @@ def test_slicing_ndim(self):
self.assertArrayNear(col3.T, 900*np.ones(2))
self.assertArrayNear(row2.T, 900*np.ones(5))

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

def test_append(self):
states = ct.SolutionArray(self.gas, 5)
states.TPX = np.linspace(500, 1000, 5), 2e5, 'H2:0.5, O2:0.4'
Expand Down