Skip to content

Commit ddd2d92

Browse files
committed
Allow species concentration range in schema
1 parent 6323b9d commit ddd2d92

1 file changed

Lines changed: 34 additions & 3 deletions

File tree

t3/schema.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77

88
from enum import Enum
9-
from typing import Dict, List, Optional, Union
9+
from typing import Dict, List, Optional, Tuple, Union
1010

1111
from pydantic import BaseModel, conint, confloat, constr, root_validator, validator
1212

@@ -148,7 +148,7 @@ class RMGSpecies(BaseModel):
148148
A class for validating input.RMG.species arguments
149149
"""
150150
label: str
151-
concentration: confloat(ge=0) = 0
151+
concentration: Union[confloat(ge=0), Tuple[confloat(ge=0), confloat(ge=0)]] = 0
152152
smiles: Optional[str] = None
153153
inchi: Optional[str] = None
154154
adjlist: Optional[str] = None
@@ -165,6 +165,37 @@ class RMGSpecies(BaseModel):
165165
class Config:
166166
extra = "forbid"
167167

168+
@validator('constant')
169+
def check_ranged_concentration_not_constant(cls, value, values):
170+
"""RMGSpecies.constant validator"""
171+
label = ' for ' + values['label'] if 'label' in values else ''
172+
if value and isinstance(values['concentration'], tuple):
173+
raise ValueError(f"A constant species cannot have a concentration range.\n"
174+
f"Got{label}: {values['concentration']}.")
175+
return value
176+
177+
@validator('concentration')
178+
def check_concentration_range_order(cls, value, values):
179+
"""Make sure the concentration range is ordered from the smallest to the largest"""
180+
label = ' for ' + values['label'] if 'label' in values else ''
181+
if isinstance(value, tuple):
182+
if value[0] == value[1]:
183+
raise ValueError(f"A concentration range cannot contain to identical concentrations.\n"
184+
f"Got{label}: {value}.")
185+
if value[0] > value[1]:
186+
value = (value[1], value[0])
187+
return value
188+
189+
@validator('balance')
190+
def check_concentration_of_balance_species(cls, value, values):
191+
"""Make sure the concentration of the balance species is defined, default to 1"""
192+
if value and 'concentration' in values:
193+
if not isinstance(values['concentration'], (int, float)):
194+
raise ValueError(f"The balance species concentration cannot be defined as a range, "
195+
f"got: {values['concentration']}.")
196+
values['concentration'] = values['concentration'] or 1
197+
return value
198+
168199

169200
class RMGReactor(BaseModel):
170201
"""
@@ -492,7 +523,7 @@ def check_model(cls, value):
492523
@validator('pdep')
493524
def check_pdep_only_if_gas_phase(cls, value, values):
494525
"""RMG.pdep validator"""
495-
if value is not None and values['reactors'] is not None:
526+
if value is not None and 'reactors' in values and values['reactors'] is not None:
496527
reactor_types = set([reactor.type for reactor in values['reactors']])
497528
if value is not None and not any(['gas' in reactor for reactor in reactor_types]):
498529
raise ValueError(f'A pdep section can only be specified for gas phase reactors, got: {reactor_types}')

0 commit comments

Comments
 (0)