-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parallel Differential Equation runner
- Loading branch information
Showing
6 changed files
with
141 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from NanoParticleTools.differential_kinetics.util import ( | ||
get_templates, run_one_rate_eq, save_data_to_hdf5, load_data_from_hdf5, | ||
run_and_save_one, get_diff_kinetics_parser) | ||
from NanoParticleTools.differential_kinetics.runner import DifferentialKinetics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from NanoParticleTools.differential_kinetics import (get_templates, | ||
run_one_rate_eq, | ||
save_data_to_hdf5) | ||
from NanoParticleTools.species_data import Dopant | ||
from maggma.core import Builder, Store | ||
from argparse import ArgumentParser | ||
from h5py import File | ||
|
||
from typing import Iterator | ||
|
||
|
||
class DifferentialKinetics(Builder): | ||
""" | ||
Builder that processes and averages NPMC documents | ||
""" | ||
|
||
def __init__(self, args: ArgumentParser, **kwargs): | ||
|
||
self.source = None | ||
self.args = args | ||
self.target = None | ||
self.kwargs = kwargs | ||
self._file = None | ||
|
||
super().__init__(sources=None, targets=None, chunk_size=1, **kwargs) | ||
|
||
def connect(self): | ||
# Since we aren't using stores, do nothing | ||
return | ||
|
||
@property | ||
def file(self): | ||
if self._file is None: | ||
self._file = File(self.args.output_file, 'w') | ||
return self._file | ||
|
||
def get_items(self) -> Iterator[dict]: | ||
for sample_id, template in enumerate(get_templates(self.args)): | ||
yield (sample_id, template) | ||
|
||
def process_item(self, item: tuple[int, dict]) -> dict: | ||
sample_id, template = item | ||
dopants = [ | ||
Dopant(el, x) | ||
for el, x in zip(template['dopants'], template['dopant_concs']) | ||
] | ||
output = run_one_rate_eq( | ||
dopants, | ||
excitation_wavelength=template['excitation_wavelength'], | ||
excitation_power=template['excitation_power'], | ||
include_spectra=self.args.include_spectra) | ||
|
||
group_id = int(sample_id // self.args.max_data_per_group) | ||
data_id = int(sample_id % self.args.max_data_per_group) | ||
return (group_id, data_id, output) | ||
|
||
def update_targets(self, items: list[dict]) -> None: | ||
for item in items: | ||
group_id, data_id, output = item | ||
save_data_to_hdf5(self.file, group_id, data_id, output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from NanoParticleTools.differential_kinetics.runner import DifferentialKinetics | ||
from NanoParticleTools.differential_kinetics.util import ( | ||
get_diff_kinetics_parser, load_data_from_hdf5) | ||
from h5py import File | ||
|
||
|
||
def test_runner(tmp_path): | ||
args = get_diff_kinetics_parser().parse_args([ | ||
'-n', '2', '-w', '400', '700', '-p', '10', '100000', '-o', | ||
f'{tmp_path}/out.h5', '-s' | ||
]) | ||
dk = DifferentialKinetics(args) | ||
dk.run() | ||
|
||
data_points = [] | ||
with File(tmp_path / 'out.h5', 'r') as f: | ||
for i in range(0, len(f['group_0'])): | ||
data_points.append(load_data_from_hdf5(f, 0, i)) | ||
assert len(data_points) == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters