Skip to content

Commit

Permalink
Merge pull request #46 from BlauGroup/tests
Browse files Browse the repository at this point in the history
Add examples and more tests
  • Loading branch information
sivonxay authored Nov 6, 2023
2 parents 5521c45 + 46b0bd6 commit f81a604
Show file tree
Hide file tree
Showing 12 changed files with 605 additions and 49 deletions.
238 changes: 238 additions & 0 deletions examples/rate_equations_1.ipynb

Large diffs are not rendered by default.

193 changes: 193 additions & 0 deletions examples/rate_equations_2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Imports"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/sivonxay/opt/anaconda3/envs/npmc_env/lib/python3.10/site-packages/maggma/utils.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from tqdm.autonotebook import tqdm\n"
]
}
],
"source": [
"from NanoParticleTools.differential_kinetics import get_diff_kinetics_parser, DifferentialKinetics\n",
"from monty.serialization import dumpfn"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"High-throughput rate equation solver uses the maggma builder."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"dk = DifferentialKinetics(num_samples=10,\n",
" excitation_power=[1e3, 1e5],\n",
" excitation_wavelength=[800, 980],\n",
" possible_dopants=['Yb', 'Er', 'Nd', 'Tm'],\n",
" max_dopants=3,\n",
" include_spectra=True,\n",
" output_file='out.h5',\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we run the builder. This function call runs each sample sequentially"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"10it [00:00, 6657.63it/s]"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"2023-11-03 15:04:53,807 - DifferentialKinetics - INFO - Processing batch of 1000 items\n",
"2023-11-03 15:05:04,698 - SpectralKinetics - INFO - Found solution in 10.89 seconds\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/sivonxay/opt/anaconda3/envs/npmc_env/code/NanoParticleTools/src/NanoParticleTools/util/conversions.py:11: RuntimeWarning: divide by zero encountered in divide\n",
" return 1 / wavenumber * 1e7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"2023-11-03 15:05:16,538 - SpectralKinetics - INFO - Found solution in 11.84 seconds\n",
"2023-11-03 15:05:16,539 - SpectralKinetics - INFO - Using user input initial population\n",
"2023-11-03 15:05:18,130 - SpectralKinetics - INFO - Found solution in 1.59 seconds\n",
"2023-11-03 15:05:18,159 - SpectralKinetics - INFO - Found solution in 0.03 seconds\n",
"2023-11-03 15:05:18,159 - SpectralKinetics - INFO - Using user input initial population\n",
"2023-11-03 15:05:18,167 - SpectralKinetics - INFO - Found solution in 0.01 seconds\n",
"2023-11-03 15:05:18,167 - SpectralKinetics - INFO - Using user input initial population\n",
"2023-11-03 15:05:18,180 - SpectralKinetics - INFO - Found solution in 0.01 seconds\n",
"2023-11-03 15:05:18,180 - SpectralKinetics - INFO - Using user input initial population\n",
"2023-11-03 15:05:18,185 - SpectralKinetics - INFO - Found solution in 0.01 seconds\n",
"2023-11-03 15:05:19,591 - SpectralKinetics - INFO - Found solution in 1.41 seconds\n",
"2023-11-03 15:05:19,592 - SpectralKinetics - INFO - Using user input initial population\n",
"2023-11-03 15:05:20,524 - SpectralKinetics - INFO - Found solution in 0.93 seconds\n",
"2023-11-03 15:05:20,524 - SpectralKinetics - INFO - Using user input initial population\n",
"2023-11-03 15:05:20,985 - SpectralKinetics - INFO - Found solution in 0.46 seconds\n",
"2023-11-03 15:05:20,991 - SpectralKinetics - INFO - Found solution in 0.00 seconds\n",
"2023-11-03 15:05:20,991 - SpectralKinetics - INFO - Using user input initial population\n",
"2023-11-03 15:05:20,996 - SpectralKinetics - INFO - Found solution in 0.01 seconds\n",
"2023-11-03 15:05:20,996 - SpectralKinetics - INFO - Using user input initial population\n",
"2023-11-03 15:05:20,998 - SpectralKinetics - INFO - Found solution in 0.00 seconds\n",
"2023-11-03 15:05:27,735 - SpectralKinetics - INFO - Found solution in 6.74 seconds\n",
"2023-11-03 15:05:27,736 - SpectralKinetics - INFO - Using user input initial population\n",
"2023-11-03 15:05:28,482 - SpectralKinetics - INFO - Found solution in 0.75 seconds\n",
"2023-11-03 15:05:30,691 - SpectralKinetics - INFO - Found solution in 2.21 seconds\n",
"2023-11-03 15:05:30,691 - SpectralKinetics - INFO - Using user input initial population\n",
"2023-11-03 15:05:31,170 - SpectralKinetics - INFO - Found solution in 0.48 seconds\n",
"2023-11-03 15:05:32,975 - SpectralKinetics - INFO - Found solution in 1.80 seconds\n",
"2023-11-03 15:05:55,912 - SpectralKinetics - INFO - Found solution in 22.94 seconds\n",
"2023-11-03 15:06:00,037 - SpectralKinetics - INFO - Found solution in 4.12 seconds\n",
"2023-11-03 15:06:00,037 - SpectralKinetics - INFO - Using user input initial population\n",
"2023-11-03 15:06:01,291 - SpectralKinetics - INFO - Found solution in 1.25 seconds\n",
"2023-11-03 15:06:01,291 - SpectralKinetics - INFO - Using user input initial population\n",
"2023-11-03 15:06:02,131 - SpectralKinetics - INFO - Found solution in 0.84 seconds\n",
"2023-11-03 15:06:02,131 - SpectralKinetics - INFO - Using user input initial population\n",
"2023-11-03 15:06:02,824 - SpectralKinetics - INFO - Found solution in 0.69 seconds\n",
"2023-11-03 15:06:02,827 - h5py._conv - DEBUG - Creating converter from 5 to 3\n"
]
}
],
"source": [
"dk.run()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To parallelize over available cores on a system, we need to use `mrun` from maggma.\n",
"\n",
"First, we dump the builder to json"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"dumpfn(dk, 'diff_eq.json')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To run the builder with 4 parallel workers, invoke the command: `mrun -n 4 diff_eq.json`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "npmc_env",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
57 changes: 41 additions & 16 deletions src/NanoParticleTools/analysis/simulation_replayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def run(self, step_size=1e-5, normalize=True):
for i, (state, site) in enumerate(
zip(self.initial_states, self.sites)):
_state = state_map_species_name[str(
site.specie)][state]
site.specie.symbol)][state]
states[seed][i, _state] = 1

self.save_populations(states, seed, 0, x,
Expand All @@ -128,8 +128,8 @@ def run(self, step_size=1e-5, normalize=True):
len(x[seed]) * step_size, x,
population_evolution, site_evolution,
step_size)
return (simulation_time, event_statistics, x,
population_evolution, site_evolution)
return (simulation_time, event_statistics, x, population_evolution,
site_evolution)

def update_state(self, states, row, state_map_species_id):
seed = row[0]
Expand All @@ -143,18 +143,43 @@ def update_state(self, states, row, state_map_species_id):
# Update the states for the sites corresponding
# to this interaction event
# Apply the event to the donor site
states[seed][donor_i][state_map_species_id[
_interaction['species_id_1']][_interaction['left_state_1']]] = 0
states[seed][donor_i][state_map_species_id[
_interaction['species_id_1']][_interaction['right_state_1']]] = 1

current_left_state = states[seed][donor_i][state_map_species_id[
_interaction['species_id_1']][_interaction['left_state_1']]]
current_right_state = states[seed][donor_i][state_map_species_id[
_interaction['species_id_1']][_interaction['right_state_1']]]
if current_left_state == 1 and current_right_state == 0:
states[seed][donor_i][state_map_species_id[_interaction[
'species_id_1']][_interaction['left_state_1']]] = 0
states[seed][donor_i][state_map_species_id[_interaction[
'species_id_1']][_interaction['right_state_1']]] = 1
else:
raise RuntimeError('Inconsistent simulation state encountered. '
'Please rerun the simulation. If this issue '
'persists, please raise a github issue')

if _interaction['number_of_sites'] == 2:
# If this is a two-site interaction, apply
# the event to the acceptor ion
states[seed][acceptor_i][state_map_species_id[_interaction[
'species_id_2']][_interaction['left_state_2']]] = 0
states[seed][acceptor_i][state_map_species_id[_interaction[
'species_id_2']][_interaction['right_state_2']]] = 1

current_left_state = states[seed][acceptor_i][state_map_species_id[
_interaction['species_id_2']][_interaction['left_state_2']]]
current_right_state = states[seed][acceptor_i][
state_map_species_id[_interaction['species_id_2']][
_interaction['right_state_2']]]

if current_left_state == 1 and current_right_state == 0:
states[seed][acceptor_i][state_map_species_id[_interaction[
'species_id_2']][_interaction['left_state_2']]] = 0
states[seed][acceptor_i][state_map_species_id[_interaction[
'species_id_2']][_interaction['right_state_2']]] = 1
else:
print('current left state: ', current_left_state)
print('current right state', current_right_state)
print(states)
raise RuntimeError('Inconsistent simulation state encountered.'
'Please rerun the simulation. If this issue'
'persists, please raise a github issue')

def save_populations(self, states, seed, time, x, population_evolution,
site_evolution, step_size):
Expand Down Expand Up @@ -212,8 +237,8 @@ def generate_docs(self,
if data is None:
data = self.run()

(simulation_time, event_statistics, x,
population_evolution, site_evolution) = data
(simulation_time, event_statistics, x, population_evolution,
site_evolution) = data
species_counter = Counter(
[site['species_id'] for site in self.npmc_input.sites.values()])
normalization_factors = np.hstack(
Expand Down Expand Up @@ -339,7 +364,7 @@ def _population_evolution_by_constraint(self,
site.coords for site in self.npmc_input.nanoparticle.dopant_sites
])
species_names = np.array([
str(site.specie)
str(site.specie.symbol)
for site in self.npmc_input.nanoparticle.dopant_sites
])

Expand All @@ -353,8 +378,8 @@ def _population_evolution_by_constraint(self,
np.where(constraint.sites_in_bounds(sites))[0])

for level in site_indices_by_constraint:
indices_inside_constraint = (indices_inside_constraint
- set(level))
indices_inside_constraint = (indices_inside_constraint -
set(level))
site_indices_by_constraint.append(
sorted(list(indices_inside_constraint)))

Expand Down
2 changes: 0 additions & 2 deletions src/NanoParticleTools/differential_kinetics/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def __init__(self,
max_dopants: int = 4,
include_spectra: bool = True,
output_file: str = 'out.h5',
num_workers: int = 1,
max_data_per_group: int = 100000,
**kwargs):
if excitation_wavelength is None:
Expand All @@ -39,7 +38,6 @@ def __init__(self,
self.max_dopants = max_dopants
self.include_spectra = include_spectra
self.output_file = output_file
self.num_workers = num_workers
self.max_data_per_group = max_data_per_group
self.source = None
self.target = None
Expand Down
6 changes: 0 additions & 6 deletions src/NanoParticleTools/differential_kinetics/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ def get_diff_kinetics_parser():
type=str,
default=f'{datetime.now().strftime("%Y%m%d_%H_%M_%S_%f")}.h5')

parser.add_argument('-j',
'--num_workers',
help='Number of concurrent workers',
type=int,
default=1)

parser.add_argument(
'-g',
'--max_data_per_group',
Expand Down
Loading

0 comments on commit f81a604

Please sign in to comment.