Skip to content

Commit

Permalink
Merge branch 'main' into 169-air-density
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-drews authored Aug 13, 2024
2 parents 4df7ff0 + e06393a commit ad6f7bd
Show file tree
Hide file tree
Showing 22 changed files with 3,393 additions and 5,095 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/CI_Tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ jobs:
cache: 'pip'

- name: Install dependencies
run: python -m pip install --upgrade pip
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Install this package
run: pip install -e .

- name: Run pytest
run: cd tests && pytest

- name: Run the smoke tests
run: |
music_box configFile=tests/configs/analytical_config/my_config.json outputDir=tests/configs/analytical_config
Expand Down
45 changes: 45 additions & 0 deletions .github/workflows/pep8_autoformat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Pep8

on:
push:
branches:
- main

jobs:
autopep8:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: autopep8
uses: peter-evans/autopep8@v2
with:
args: --recursive --in-place --aggressive --aggressive .

- name: Check for changes
id: check-changes
run: git diff --exit-code
continue-on-error: true

- name: Commit and push changes
# a failue of this step means changes were detected
if: steps.check-changes.outcome != 'success'
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "[email protected]"
git commit -am "Auto-format code using Clang-Format" || echo "No changes to commit"
- name: Push changes to main-autopep8 branch
if: steps.check-changes.outcome != 'success'
run: git push origin HEAD:main-autopep8

- name: Create Pull Request
if: steps.check-changes.outcome != 'success'
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: "Auto-format code using autopep8"
title: "Auto-format code by autopep8"
body: "This is an auto-generated PR with fixes by autopep8."
branch: main-autopep8
66 changes: 40 additions & 26 deletions .github/workflows/publish-package.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
name: Publish a pypi package
name: Publish Python Package

on:
workflow_dispatch:
release:
types: [created]
types:
- published

jobs:
publish_to_pypi:
build_sdist:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Build SDist and Wheel
run: pipx run build --sdist --wheel

- name: Check metadata
run: pipx run twine check dist/*

- uses: actions/upload-artifact@v4
with:
name: cibw-sdist
path: dist/*

upload_all:
name: Upload release
needs: [build_sdist]
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/acom_music_box
permissions:
contents: read
packages: write
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.9'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Build and publish package
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
pipx run build
pipx run twine check dist/*
python -m twine upload --repository pypi dist/*
- uses: actions/setup-python@v5
with:
python-version: "3.x"

- uses: actions/download-artifact@v4
with:
pattern: cibw-*
path: dist
merge-multiple: true

- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
2 changes: 1 addition & 1 deletion src/acom_music_box/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
This package contains modules for handling various aspects of a music box,
including species, products, reactants, reactions, and more.
"""
__version__ = "2.1.1"
__version__ = "2.1.5"

from .utils import convert_time, convert_pressure, convert_temperature, convert_concentration
from .music_box_species import Species
Expand Down
15 changes: 6 additions & 9 deletions src/acom_music_box/music_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def generateReactionConfig(self):

return json.dumps(reactionsJson, indent=4)

def create_solver(self, path_to_config):
def create_solver(self, path_to_config, solver_type = musica.micmsolver.rosenbrock, number_of_grid_cells = 1):
"""
Creates a micm solver object using the CAMP configuration files.
Expand All @@ -417,10 +417,10 @@ def create_solver(self, path_to_config):
None
"""
# Create a solver object using the configuration file
self.solver = musica.create_solver(path_to_config, musica.micmsolver.rosenbrock, 1)
self.solver = musica.create_solver(path_to_config, musica.micmsolver.rosenbrock, number_of_grid_cells)


def solve(self, path_to_output = None):
def solve(self, output_path = None):
"""
Solves the box model simulation and optionally writes the output to a file.
Expand All @@ -440,9 +440,6 @@ def solve(self, path_to_output = None):
#sets up initial conditions to be current conditions
curr_conditions = self.initial_conditions

#sets up initial concentraion values
curr_concentrations = self.initial_conditions.get_concentration_array()

#sets up next condition if evolving conditions is not empty
next_conditions = None
next_conditions_time = 0
Expand Down Expand Up @@ -541,9 +538,9 @@ def solve(self, path_to_output = None):
curr_time += self.box_model_options.chem_step_time

#outputs to file if output is present
if(path_to_output != None):
logger.info("path_to_output = {}".format(path_to_output))
with open(path_to_output, 'w', newline='') as output:
if(output_path != None):
logger.info("path_to_output = {}".format(output_path))
with open(output_path, 'w', newline='') as output:
writer = csv.writer(output)
writer.writerows(output_array)

Expand Down
8 changes: 8 additions & 0 deletions src/acom_music_box/music_box_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def __init__(self, pressure=None, temperature=None, species_concentrations=None,
self.temperature = temperature
self.species_concentrations = species_concentrations if species_concentrations is not None else []
self.reaction_rates = reaction_rates if reaction_rates is not None else []

def __repr__(self):
return f"Conditions(pressure={self.pressure}, temperature={self.temperature}, species_concentrations={self.species_concentrations}, reaction_rates={self.reaction_rates})"

def __str__(self):
return f"Pressure: {self.pressure}, Temperature: {self.temperature}, Species Concentrations: {self.species_concentrations}, Reaction Rates: {self.reaction_rates}"

@classmethod
def from_UI_JSON(cls, UI_JSON, species_list, reaction_list):
Expand Down Expand Up @@ -129,6 +135,8 @@ def from_config_JSON(cls, path_to_json, config_JSON, species_list, reaction_list
species_concentrations.append(SpeciesConcentration(species, concentration))

for species in species_list.species:
if species.tracer_type == 'THIRD_BODY':
continue
if not any(conc.species.name == species.name for conc in species_concentrations):
species_concentrations.append(SpeciesConcentration(species, 0))

Expand Down
6 changes: 6 additions & 0 deletions src/acom_music_box/music_box_reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def __init__(self, name=None, reaction_type=None, reactants=None, products=None,
self.products = products if products is not None else []
self.scaling_factor = scaling_factor

def __str__(self):
return f"{self.name}: {self.reaction_type}"

def __repr__(self):
return f"{self.name}: {self.reaction_type}"

def add_reactant(self, reactant):
"""
Add a Reactant instance to the list of reactants.
Expand Down
6 changes: 6 additions & 0 deletions src/acom_music_box/music_box_reaction_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ def __init__(self, reaction, rate):
"""
self.reaction = reaction
self.rate = rate

def __str__(self):
return f"{self.reaction.name}: {self.rate}"

def __repr__(self):
return f"{self.reaction.name}: {self.rate}"
23 changes: 18 additions & 5 deletions src/acom_music_box/music_box_species.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
class Species:
"""
Represents a species with various attributes such as name, absolute tolerance, phase, molecular weight, and density.
Represents a species with various attributes such as name, absolute tolerance, phase, molecular weight
Attributes:
name (str): The name of the species.
absolute_tolerance (float): The absolute tolerance of the species.
phase (str): The phase of the species.
molecular_weight (float): The molecular weight of the species in kg mol^-1.
density (float): The density of the species in kg m^-3.
"""

def __init__(self, name=None, absolute_tolerance=None, phase=None, molecular_weight=None, density=None):
def __init__(self, name=None, absolute_tolerance=None, phase=None, molecular_weight=None, tracer_type=None, diffusion_coefficient=None):
"""
Initializes a new instance of the Species class.
Expand All @@ -19,10 +18,24 @@ def __init__(self, name=None, absolute_tolerance=None, phase=None, molecular_wei
absolute_tolerance (float): The absolute tolerance of the species.
phase (str): The phase of the species.
molecular_weight (float): The molecular weight of the species in kg mol^-1.
density (float): The density of the species in kg m^-3.
tracer_type (str): The type of the tracer. Default is None. Other options are THIRD_BODY or CONSTANT
diffusion_coefficient (float): The diffusion coefficient of the species in m^2 s^-1. Default is None.
"""
self.name = name
self.absolute_tolerance = absolute_tolerance
self.phase = phase
self.molecular_weight = molecular_weight
self.density = density
self.tracer_type = tracer_type
self.diffusion_coefficient = diffusion_coefficient

def __repr__(self):
return (f"Species(name={self.name!r}, absolute_tolerance={self.absolute_tolerance!r}, "
f"phase={self.phase!r}, molecular_weight={self.molecular_weight!r}, "
f"tracer_type={self.tracer_type!r}, diffusion_coefficient={self.diffusion_coefficient!r})")

def __str__(self):
return (f"Species: {self.name}, Phase: {self.phase}, "
f"Molecular Weight: {self.molecular_weight} kg/mol, "
f"Tolerance: {self.absolute_tolerance}, "
f"Tracer Type: {self.tracer_type}, "
f"Diffusion Coefficient: {self.diffusion_coefficient} m^2/s")
7 changes: 7 additions & 0 deletions src/acom_music_box/music_box_species_concentration.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ def __init__(self, species, concentration):
"""
self.species = species
self.concentration = concentration

def __str__(self):
return f"{self.species.name}: {self.concentration}"

def __repr__(self):
return f"{self.species.name}: {self.concentration}"

33 changes: 11 additions & 22 deletions src/acom_music_box/music_box_species_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self, species=None, relative_tolerance=1.0e-4):
"""
self.species = species if species is not None else []
self.relative_tolerance = relative_tolerance
self.tracer_type = None

@classmethod
def from_UI_JSON(cls, UI_JSON):
Expand Down Expand Up @@ -62,7 +63,7 @@ def from_config_JSON(cls, path_to_json, config_JSON):
species_from_json = []

#gets config file path
config_file_path = os.path.dirname(path_to_json) + "/" + config_JSON['model components'][0]['configuration file']
config_file_path = os.path.join(os.path.dirname(path_to_json), config_JSON['model components'][0]['configuration file'])

#opnens config path to read species file
with open(config_file_path, 'r') as json_file:
Expand All @@ -74,28 +75,16 @@ def from_config_JSON(cls, path_to_json, config_JSON):
with open(species_file_path, 'r') as species_file:
species_data = json.load(species_file)
#loads species by names from camp files
for properties in species_data['camp-data']:
if properties.get('name') is not None:
name = properties.get('name')
species_from_json.append(Species(name, None, None, None, None))


#chceks if species are in the config file and updates attributes accordingly
for chem_spec in config_JSON['chemical species']:
for species in species_from_json:
if species.name == chem_spec:
# Add attributes to species
if 'absolute tolerance' in chem_spec:
species.absolute_tolerance = chem_spec['absolute tolerance']
if 'molecular weight' in chem_spec:
species.molecular_weight = chem_spec['molecular weight']
if 'density' in chem_spec:
species.density = chem_spec['density']
if 'phase' in chem_spec:
species.phase = chem_spec['phase']
for species in species_data['camp-data']:
if species['type'] == 'CHEM_SPEC':
tolerance = species.get('absolute tolerance', None)
molecular_weight = species.get('molecular weight [kg mol-1]', None)
phase = species.get('phase', None)
diffusion_coefficient = species.get('diffusion coefficient [m2 s-1]', None)
tracer_type = species.get('tracer type', None)
name = species.get('name')
species_from_json.append(Species(name=name, absolute_tolerance=tolerance, molecular_weight=molecular_weight, phase=phase, diffusion_coefficient=diffusion_coefficient, tracer_type=tracer_type))



return cls(species_from_json)


Expand Down
2 changes: 1 addition & 1 deletion tests/configs/chapman_config/camp_data/species.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
"name": "M",
"type": "CHEM_SPEC",
"tracer type": "CONSTANT"
"tracer type": "THIRD_BODY"
},
{
"name": "Ar",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
{
"camp-files": [
"species.json",
"reactions.json"
]
}
{"camp-files": ["species.json", "reactions.json"]}
Loading

0 comments on commit ad6f7bd

Please sign in to comment.