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

Allow mole fractions to be normalized if they do not sum to one #1809

Merged
merged 2 commits into from
Dec 9, 2019
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
17 changes: 17 additions & 0 deletions rmgpy/rmg/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,23 @@ def simple_reactor(temperature,
[not isinstance(x, list) for x in initialMoleFractions.values()]):
nSims = 1

# normalize mole fractions if not using a mole fraction range
if all([not isinstance(x, list) for x in initialMoleFractions.values()]):
total_initial_moles = sum(initialMoleFractions.values())
if total_initial_moles != 1:
logging.warning('Initial mole fractions do not sum to one; normalizing.')
logging.info('')
logging.info('Original composition:')
for spec, molfrac in initialMoleFractions.items():
logging.info('{0} = {1}'.format(spec, molfrac))
for spec in initialMoleFractions:
initialMoleFractions[spec] /= total_initial_moles
logging.info('')
logging.info('Normalized mole fractions:')
for spec, molfrac in initialMoleFractions.items():
logging.info('{0} = {1}'.format(spec, molfrac))
logging.info('')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really minor, but I think it would be nice to put the blank lines before Original composition and Normalized mole fractions like before.


termination = []
if terminationConversion is not None:
for spec, conv in terminationConversion.items():
Expand Down
238 changes: 236 additions & 2 deletions rmgpy/rmg/inputTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
###############################################################################

import unittest
from unittest.mock import patch

import rmgpy.rmg.input as inp
from rmgpy.rmg.main import RMG


###################################################

def setUpModule(self):
def setUpModule():
"""
A method that is run before the class.
"""
Expand All @@ -46,7 +47,7 @@ def setUpModule(self):
inp.set_global_rmg(rmg)


def tearDownModule(self):
def tearDownModule():
# remove RMG object
global rmg
rmg = None
Expand Down Expand Up @@ -146,5 +147,238 @@ def test_themo_central_database(self):
self.assertEqual(rmg.thermo_central_database.client, None)


class TestInputReactors(unittest.TestCase):
"""
Contains unit tests for reactor input classes
"""

def setUp(self):
"""This method is run before every test in this class"""
# Create a mock species dictionary
# In reality, the values would be Species objects, but it doesn't matter for testing
species_dict = {
'A': 'A',
'B': 'B',
'C': 'C',
'X': 'X',
}

# Assign to global variable in the input module
inp.species_dict = species_dict

# Initialize the rmg.reaction_systems attribute
global rmg
rmg.reaction_systems = []

def tearDown(self):
"""This method is run after every test in this class"""
# Reset the global species_dict variable in the input module
inp.species_dict = {}

# Reset the rmg.reaction_systems attribute
global rmg
rmg.reaction_systems = []

def test_simple_reactor_mole_fractions(self):
"""Test that SimpleReactor mole fractions are set properly"""
inp.simple_reactor(
temperature=(1000, 'K'),
pressure=(1, 'atm'),
initialMoleFractions={
'A': 0.5,
'B': 0.3,
'C': 0.2,
},
terminationTime=(1, 's'),
)

global rmg
reactor = rmg.reaction_systems[0]
self.assertEqual(reactor.initial_mole_fractions['A'], 0.5)
self.assertEqual(reactor.initial_mole_fractions['B'], 0.3)
self.assertEqual(reactor.initial_mole_fractions['C'], 0.2)

@patch('rmgpy.rmg.input.logging')
def test_simple_reactor_mole_fractions_normalize_1(self, mock_logging):
"""Test that SimpleReactor mole fractions are normalized properly"""
inp.simple_reactor(
temperature=(1000, 'K'),
pressure=(1, 'atm'),
initialMoleFractions={
'A': 5,
'B': 3,
'C': 2,
},
terminationTime=(1, 's'),
)

global rmg
reactor = rmg.reaction_systems[0]
self.assertEqual(reactor.initial_mole_fractions['A'], 0.5)
self.assertEqual(reactor.initial_mole_fractions['B'], 0.3)
self.assertEqual(reactor.initial_mole_fractions['C'], 0.2)

mock_logging.warning.assert_called_with(
'Initial mole fractions do not sum to one; normalizing.'
)

@patch('rmgpy.rmg.input.logging')
def test_simple_reactor_mole_fractions_normalize_2(self, mock_logging):
"""Test that SimpleReactor mole fractions are normalized properly"""
inp.simple_reactor(
temperature=[(1000, 'K'), (2000, 'K')],
pressure=[(1, 'atm'), (10, 'atm')],
initialMoleFractions={
'A': 5,
'B': 3,
'C': 2,
},
terminationTime=(1, 's'),
)

global rmg
reactor = rmg.reaction_systems[0]
self.assertEqual(reactor.initial_mole_fractions['A'], 0.5)
self.assertEqual(reactor.initial_mole_fractions['B'], 0.3)
self.assertEqual(reactor.initial_mole_fractions['C'], 0.2)

mock_logging.warning.assert_called_with(
'Initial mole fractions do not sum to one; normalizing.'
)

def test_simple_reactor_mole_fractions_ranged(self):
"""Test that SimpleReactor ranged mole fractions are not normalized"""
inp.simple_reactor(
temperature=[(1000, 'K'), (2000, 'K')],
pressure=[(1, 'atm'), (10, 'atm')],
initialMoleFractions={
'A': [5, 8],
'B': 3,
'C': 2,
},
terminationTime=(1, 's'),
)

global rmg
reactor = rmg.reaction_systems[0]
self.assertEqual(reactor.initial_mole_fractions['A'], [5, 8])
self.assertEqual(reactor.initial_mole_fractions['B'], 3)
self.assertEqual(reactor.initial_mole_fractions['C'], 2)

def test_liquid_reactor_concentrations(self):
"""Test that LiquidReactor concentrations are set properly"""
inp.liquid_reactor(
temperature=(1000, 'K'),
initialConcentrations={
'A': (0.3, 'mol/L'),
'B': (0.2, 'mol/L'),
'C': (0.1, 'mol/L'),
},
terminationTime=(1, 's'),
)

global rmg
reactor = rmg.reaction_systems[0]

# Values get converted to default SI units, mol/m^3
self.assertEqual(reactor.initial_concentrations['A'], 300)
self.assertEqual(reactor.initial_concentrations['B'], 200)
self.assertEqual(reactor.initial_concentrations['C'], 100)

def test_surface_reactor_mole_fractions(self):
"""Test that SurfaceReactor mole fractions are set properly"""
inp.surface_reactor(
temperature=(1000, 'K'),
initialPressure=(1, 'atm'),
initialGasMoleFractions={
'A': 0.5,
'B': 0.3,
'C': 0.2,
},
initialSurfaceCoverages={'X': 1.0},
surfaceVolumeRatio=(1e1, 'm^-1'),
terminationTime=(1, 's'),
)

global rmg
reactor = rmg.reaction_systems[0]
self.assertEqual(reactor.initial_gas_mole_fractions['A'], 0.5)
self.assertEqual(reactor.initial_gas_mole_fractions['B'], 0.3)
self.assertEqual(reactor.initial_gas_mole_fractions['C'], 0.2)

@patch('rmgpy.rmg.input.logging')
def test_surface_reactor_mole_fractions_normalize_1(self, mock_logging):
"""Test that SurfaceReactor mole fractions are normalized properly"""
inp.surface_reactor(
temperature=(1000, 'K'),
initialPressure=(1, 'atm'),
initialGasMoleFractions={
'A': 5,
'B': 3,
'C': 2,
},
initialSurfaceCoverages={'X': 1.0},
surfaceVolumeRatio=(1e1, 'm^-1'),
terminationTime=(1, 's'),
)

global rmg
reactor = rmg.reaction_systems[0]
self.assertEqual(reactor.initial_gas_mole_fractions['A'], 0.5)
self.assertEqual(reactor.initial_gas_mole_fractions['B'], 0.3)
self.assertEqual(reactor.initial_gas_mole_fractions['C'], 0.2)

mock_logging.warning.assert_called_with(
'Initial gas mole fractions do not sum to one; renormalizing.'
)

def test_mb_sampled_reactor_mole_fractions(self):
"""Test that MBSampledReactor mole fractions are set properly"""
inp.mb_sampled_reactor(
temperature=(1000, 'K'),
pressure=(1, 'atm'),
initialMoleFractions={
'A': 0.5,
'B': 0.3,
'C': 0.2,
},
mbsamplingRate=3500,
terminationTime=(1, 's'),
constantSpecies=['B', 'C'],
)

global rmg
reactor = rmg.reaction_systems[0]
self.assertEqual(reactor.initial_mole_fractions['A'], 0.5)
self.assertEqual(reactor.initial_mole_fractions['B'], 0.3)
self.assertEqual(reactor.initial_mole_fractions['C'], 0.2)

@patch('rmgpy.rmg.input.logging')
def test_mb_sampled_reactor_mole_fractions_normalize_1(self, mock_logging):
"""Test that MBSampledReactor mole fractions are normalized properly"""
inp.mb_sampled_reactor(
temperature=(1000, 'K'),
pressure=(1, 'atm'),
initialMoleFractions={
'A': 5,
'B': 3,
'C': 2,
},
mbsamplingRate=3500,
terminationTime=(1, 's'),
constantSpecies=['B', 'C'],
)

global rmg
reactor = rmg.reaction_systems[0]
self.assertEqual(reactor.initial_mole_fractions['A'], 0.5)
self.assertEqual(reactor.initial_mole_fractions['B'], 0.3)
self.assertEqual(reactor.initial_mole_fractions['C'], 0.2)

mock_logging.warning.assert_called_with(
'Initial mole fractions do not sum to one; normalizing.'
)


if __name__ == '__main__':
unittest.main()