Skip to content

Commit

Permalink
Auto-format code using Clang-Format (#256)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Actions <[email protected]>
  • Loading branch information
github-actions[bot] and actions-user authored Oct 9, 2024
1 parent a336063 commit 3758660
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/acom_music_box/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
BOLTZMANN_CONSTANT = 1.380649e-23 # joules / Kelvin
AVOGADRO_CONSTANT = 6.02214076e23 # / mole
GAS_CONSTANT = BOLTZMANN_CONSTANT * AVOGADRO_CONSTANT # joules / Kelvin-mole
GAS_CONSTANT = BOLTZMANN_CONSTANT * AVOGADRO_CONSTANT # joules / Kelvin-mole
2 changes: 1 addition & 1 deletion src/acom_music_box/music_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def check_config(self, boxConfigPath):

if dup_names:
raise Exception(f"Error: Duplicate reaction names specified within {boxConfigPath}: {dup_names}. "
"Please remove or rename the duplicates.")
"Please remove or rename the duplicates.")

# look for duplicate reaction names in the initial conditions
if (self.initial_conditions):
Expand Down
2 changes: 1 addition & 1 deletion src/acom_music_box/species_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def from_config_JSON(cls, path_to_json, config_JSON):

# assumes species file is first in the list
if (len(config['camp-files']) > 0):
species_file_path = os.path.join( os.path.dirname(config_file_path), config['camp-files'][0])
species_file_path = os.path.join(os.path.dirname(config_file_path), config['camp-files'][0])
with open(species_file_path, 'r') as species_file:
species_data = json.load(species_file)
# loads species by names from camp files
Expand Down
12 changes: 9 additions & 3 deletions src/acom_music_box/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
from .constants import GAS_CONSTANT, AVOGADRO_CONSTANT


def extract_unit(data, key):
"""Extract the value and unit from the key in data."""
pattern = re.compile(rf'{key} \[(.+)\]')
Expand All @@ -10,6 +11,7 @@ def extract_unit(data, key):
return float(v), match.group(1)
return None, None


def convert_time(data, key):
"""
Convert the time from the input data to seconds.
Expand All @@ -21,7 +23,7 @@ def convert_time(data, key):
float: The time in seconds.
"""
time_value, unit = extract_unit(data, key)

if unit == 'sec':
return time_value
elif unit == 'min':
Expand All @@ -33,6 +35,7 @@ def convert_time(data, key):
else:
raise ValueError(f"Unsupported time unit: {unit}")


def convert_pressure(data, key):
"""
Convert the pressure from the input data to Pascals.
Expand All @@ -44,7 +47,7 @@ def convert_pressure(data, key):
float: The pressure in Pascals.
"""
pressure_value, unit = extract_unit(data, key)

if unit == 'Pa':
return pressure_value
elif unit == 'atm':
Expand All @@ -58,6 +61,7 @@ def convert_pressure(data, key):
else:
raise ValueError(f"Unsupported pressure unit: {unit}")


def convert_temperature(data, key):
"""
Convert the temperature from the input data to Kelvin.
Expand All @@ -69,7 +73,7 @@ def convert_temperature(data, key):
float: The temperature in Kelvin.
"""
temperature_value, unit = extract_unit(data, key)

if unit == 'K':
return temperature_value
elif unit == 'C':
Expand All @@ -79,6 +83,7 @@ def convert_temperature(data, key):
else:
raise ValueError(f"Unsupported temperature unit: {unit}")


def convert_concentration(data, key, temperature, pressure):
"""
Convert the concentration from the input data to molecules per cubic meter.
Expand Down Expand Up @@ -113,6 +118,7 @@ def convert_concentration(data, key, temperature, pressure):
else:
raise ValueError(f"Unsupported concentration unit: {unit}")


def calculate_air_density(temperature, pressure):
"""
Calculate the air density in moles/m^3.
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/test_mixing_ratios.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ def run_example(self, example):
rel_tol=1e-8,
), f"Arrays differ at index ({i}, 2)"


def test_mol_mol_1(self):
current_dir = os.path.dirname(__file__)
example = os.path.join(current_dir, "configs", "mixing_ratio", "mol mol-1", "my_config.json")
self.run_example(example)

def test_ppth(self):
current_dir = os.path.dirname(__file__)
example = os.path.join(current_dir, "configs", "mixing_ratio", "ppth", "my_config.json")
Expand All @@ -99,7 +98,7 @@ def test_ppm(self):
current_dir = os.path.dirname(__file__)
example = os.path.join(current_dir, "configs", "mixing_ratio", "ppm", "my_config.json")
self.run_example(example)

def test_ppb(self):
current_dir = os.path.dirname(__file__)
example = os.path.join(current_dir, "configs", "mixing_ratio", "ppb", "my_config.json")
Expand Down
65 changes: 34 additions & 31 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
import pytest
from acom_music_box.utils import (
convert_time,
convert_pressure,
convert_temperature,
convert_concentration,
convert_time,
convert_pressure,
convert_temperature,
convert_concentration,
calculate_air_density
)
import math


@pytest.mark.parametrize("data, key, expected", [
({'time [sec]': 60}, 'time', 60),
({'time [min]': 1}, 'time', 60),
({'time [hour]': 1}, 'time', 3600),
({'time [day]': 1}, 'time', 86400),
({'time [sec]': 60}, 'time', 60),
({'time [min]': 1}, 'time', 60),
({'time [hour]': 1}, 'time', 3600),
({'time [day]': 1}, 'time', 86400),
])
def test_convert_time(data, key, expected):
assert convert_time(data, key) == expected
assert convert_time(data, key) == expected


@pytest.mark.parametrize("data, key, expected", [
({'pressure [Pa]': 101325}, 'pressure', 101325),
({'pressure [atm]': 1}, 'pressure', 101325),
({'pressure [bar]': 1}, 'pressure', 100000),
({'pressure [kPa]': 101.325}, 'pressure', 101325),
({'pressure [hPa]': 1013.25}, 'pressure', 101325),
({'pressure [Pa]': 101325}, 'pressure', 101325),
({'pressure [atm]': 1}, 'pressure', 101325),
({'pressure [bar]': 1}, 'pressure', 100000),
({'pressure [kPa]': 101.325}, 'pressure', 101325),
({'pressure [hPa]': 1013.25}, 'pressure', 101325),
])
def test_convert_pressure(data, key, expected):
assert convert_pressure(data, key) == expected
assert convert_pressure(data, key) == expected


@pytest.mark.parametrize("data, key, expected", [
({'temp [K]': 273.15}, 'temp', 273.15),
({'temp [C]': 0}, 'temp', 273.15),
({'temp [F]': 32}, 'temp', 273.15),
({'temp [K]': 273.15}, 'temp', 273.15),
({'temp [C]': 0}, 'temp', 273.15),
({'temp [F]': 32}, 'temp', 273.15),
])
def test_convert_temperature(data, key, expected):
assert convert_temperature(data, key) == expected
assert convert_temperature(data, key) == expected


@pytest.mark.parametrize("data, key, temperature, pressure, expected", [
({'concentration [mol m-3]': 1}, 'concentration', 298.15, 101325, 1),
({'concentration [mol cm-3]': 1e-3}, 'concentration', 298.15, 101325, 1),
({'concentration [molec m-3]': 6.02214076e+23}, 'concentration', 298.15, 101325, 1),
({'concentration [molec cm-3]': 6.02214076e+20}, 'concentration', 298.15, 101325, 1),
({'concentration [molecule m-3]': 6.02214076e+23}, 'concentration', 298.15, 101325, 1),
({'concentration [molecule cm-3]': 6.02214076e+20}, 'concentration', 298.15, 101325, 1),
({'concentration [ppth]': 1e-3}, 'concentration', 298.15, 101325, 1e-6 * calculate_air_density(298.15, 101325)),
({'concentration [ppm]': 1}, 'concentration', 298.15, 101325, 1e-6 * calculate_air_density(298.15, 101325)),
({'concentration [ppb]': 1}, 'concentration', 298.15, 101325, 1e-9 * calculate_air_density(298.15, 101325)),
({'concentration [ppt]': 1}, 'concentration', 298.15, 101325, 1e-12 * calculate_air_density(298.15, 101325)),
({'concentration [mol mol-1]': 1}, 'concentration', 298.15, 101325, 1 * calculate_air_density(298.15, 101325)),
({'concentration [mol m-3]': 1}, 'concentration', 298.15, 101325, 1),
({'concentration [mol cm-3]': 1e-3}, 'concentration', 298.15, 101325, 1),
({'concentration [molec m-3]': 6.02214076e+23}, 'concentration', 298.15, 101325, 1),
({'concentration [molec cm-3]': 6.02214076e+20}, 'concentration', 298.15, 101325, 1),
({'concentration [molecule m-3]': 6.02214076e+23}, 'concentration', 298.15, 101325, 1),
({'concentration [molecule cm-3]': 6.02214076e+20}, 'concentration', 298.15, 101325, 1),
({'concentration [ppth]': 1e-3}, 'concentration', 298.15, 101325, 1e-6 * calculate_air_density(298.15, 101325)),
({'concentration [ppm]': 1}, 'concentration', 298.15, 101325, 1e-6 * calculate_air_density(298.15, 101325)),
({'concentration [ppb]': 1}, 'concentration', 298.15, 101325, 1e-9 * calculate_air_density(298.15, 101325)),
({'concentration [ppt]': 1}, 'concentration', 298.15, 101325, 1e-12 * calculate_air_density(298.15, 101325)),
({'concentration [mol mol-1]': 1}, 'concentration', 298.15, 101325, 1 * calculate_air_density(298.15, 101325)),
])
def test_convert_concentration(data, key, temperature, pressure, expected):
assert math.isclose(convert_concentration(data, key, temperature, pressure), expected)
assert math.isclose(convert_concentration(data, key, temperature, pressure), expected)


def test_invalid_concentration():
data = {'invalid_concentration': 100}
Expand Down

0 comments on commit 3758660

Please sign in to comment.