Skip to content

Commit

Permalink
added room_capacity as optional (and therefore results in l/s/p)
Browse files Browse the repository at this point in the history
  • Loading branch information
lrdossan committed Aug 28, 2024
1 parent 37c5dea commit 4f984b2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
13 changes: 7 additions & 6 deletions caimira/apps/calculator/co2_model_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CO2FormData(FormData):
CO2_data: dict
fitting_ventilation_states: list
fitting_ventilation_type: str
room_capacity: int
room_capacity: typing.Optional[int]

#: The default values for undefined fields. Note that the defaults here
#: and the defaults in the html form must not be contradictory.
Expand All @@ -46,7 +46,7 @@ class CO2FormData(FormData):
'infected_lunch_start': '12:30',
'infected_people': 1,
'infected_start': '08:30',
'room_capacity': 10,
'room_capacity': None,
'room_volume': NO_DEFAULT,
'specific_breaks': '{}',
'total_people': NO_DEFAULT,
Expand All @@ -65,10 +65,11 @@ def validate(self):
self.validate_population_parameters()

# Validate room capacity
if type(self.room_capacity) is not int:
raise TypeError(f'The room capacity should be a valid integer (> 0). Got {type(self.room_capacity)}.')
if self.room_capacity <= 0:
raise TypeError(f'The room capacity should be a valid integer (> 0). Got {self.room_capacity}.')
if self.room_capacity:
if type(self.room_capacity) is not int:
raise TypeError(f'The room capacity should be a valid integer (> 0). Got {type(self.room_capacity)}.')
if self.room_capacity <= 0:
raise TypeError(f'The room capacity should be a valid integer (> 0). Got {self.room_capacity}.')

# Validate specific inputs - breaks (exposed and infected)
if self.specific_breaks != {}:
Expand Down
8 changes: 8 additions & 0 deletions caimira/apps/calculator/form_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ def _safe_int_cast(value) -> int:
return int(value)
else:
raise TypeError(f"Unable to safely cast {value} ({type(value)} type) to int")


def _safe_optional_int_cast(value) -> typing.Optional[int]:
if value is None or value == '':
return None
return _safe_int_cast(value)


#: Mapping of field name to a callable which can convert values from form
Expand All @@ -427,6 +433,8 @@ def cast_class_fields(cls):
_CAST_RULES_NATIVE_TO_FORM_ARG[_field.name] = time_minutes_to_string
elif _field.type is int:
_CAST_RULES_FORM_ARG_TO_NATIVE[_field.name] = _safe_int_cast
elif _field.type is typing.Optional[int]:
_CAST_RULES_FORM_ARG_TO_NATIVE[_field.name] = _safe_optional_int_cast
elif _field.type is float:
_CAST_RULES_FORM_ARG_TO_NATIVE[_field.name] = float
elif _field.type is bool:
Expand Down
7 changes: 3 additions & 4 deletions caimira/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class Room:
humidity: _VectorisedFloat = 0.5

#: The maximum occupation of the room - design limit
capacity: float = 10
capacity: typing.Optional[int] = None


@dataclass(frozen=True)
Expand Down Expand Up @@ -1596,11 +1596,10 @@ def fun(x):
the_predictive_CO2 = self.CO2_concentrations_from_params(the_CO2_concentration_model)

# Ventilation in L/s
flow_rates_l_s = [vent / 3600 * self.room.volume * 1000
for vent in ventilation_values] # 1m^3 = 1000L
flow_rates_l_s = [vent / 3600 * self.room.volume * 1000 for vent in ventilation_values] # 1m^3 = 1000L

# Ventilation in L/s/person
flow_rates_l_s_p = [flow_rate / self.room.capacity for flow_rate in flow_rates_l_s]
flow_rates_l_s_p = [flow_rate / self.room.capacity for flow_rate in flow_rates_l_s] if self.room.capacity else None

return {
"exhalation_rate": exhalation_rate,
Expand Down

0 comments on commit 4f984b2

Please sign in to comment.