Skip to content

Commit afed2e9

Browse files
authored
Merge pull request #139 from NREL/postfix
Fix compute fitted kla
2 parents 0fe313d + 7755850 commit afed2e9

5 files changed

Lines changed: 73 additions & 9 deletions

File tree

bird/postprocess/kla_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,14 @@ def check_data_size(time_obs: np.ndarray, concentration_obs: np.ndarray):
147147
def compute_kla(
148148
data_t: np.ndarray,
149149
data_c: np.ndarray,
150+
num_warmup: int = 4000,
151+
num_samples: int = 1000,
150152
max_chop: int | None = None,
151153
bootstrap: bool = True,
152154
) -> dict:
153155
"""
154156
Main loop to compute kla
155157
"""
156-
num_warmup = 4000
157-
num_samples = 1000
158158
rng_key = random.PRNGKey(0)
159159
rng_key, rng_key_ = random.split(rng_key)
160160

bird/postprocess/post_quantities.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,8 @@ def compute_fitted_kla(
11171117
species_names: str | list[str],
11181118
n_cells: int | None = None,
11191119
volume_time: str | None = None,
1120+
num_warmup: int = 4000,
1121+
num_samples: int = 1000,
11201122
field_dict: dict | None = None,
11211123
) -> tuple[dict, dict, dict]:
11221124
r"""
@@ -1149,6 +1151,12 @@ def compute_fitted_kla(
11491151
volume_time : str | None
11501152
Time folder to read to get the cell volumes.
11511153
If None, finds volume time automatically
1154+
num_warmup: int
1155+
Number of MCMC samples in the warmup phase
1156+
Defaults to 4000
1157+
num_samples: int
1158+
Number of posterior MCMC samples generated
1159+
Defaults to 1000
11521160
field_dict : dict
11531161
Dictionary of fields used to avoid rereading the same fields to calculate different quantities
11541162
@@ -1228,16 +1236,18 @@ def compute_fitted_kla(
12281236
)
12291237
c_history[species_name][itime] = c_liq
12301238

1231-
breakpoint()
1232-
12331239
logger.info("Doing kla fit")
12341240
# Compute kla
12351241
kla_spec = {}
12361242
cstar_spec = {}
12371243
for species_name in species_names:
12381244
kla_res = compute_kla(
1239-
np.array(time_float_sorted), c_history[species_name]
1245+
np.array(time_float_sorted),
1246+
c_history[species_name],
1247+
num_warmup=num_warmup,
1248+
num_samples=num_samples,
12401249
)
1250+
# Convert to h-1
12411251
kla_spec[species_name] = {
12421252
"mean": kla_res["kla"] * 3600,
12431253
"std": kla_res["kla_err"] * 3600,

bird/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Bio reactor design version"""
22

3-
__version__ = "0.0.43"
3+
__version__ = "0.0.45"

docs/source/python_interface_tut.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ If we want to read the gas volume fraction ``alpha.gas``, the mass fraction of C
5050
print("cell alpha gas shape = ", alpha_gas.shape)
5151
print("cell u liq shape = ", u_liq.shape)
5252
53-
The function ``read_field`` automatically detects whether the field is a vector or a scalar field.
53+
The function ``read_field`` automatically detects whether the field is a vector or a scalar field. The first returned results in (N,) for a scalar field and (N,3) for a vector field.
5454

5555

5656

tests/postprocess/test_post_quantities.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,62 @@ def test_instantaneous_kla():
315315
)
316316

317317

318+
def test_fitted_kla():
319+
"""
320+
Test for fitted kla calculation
321+
"""
322+
case_folder = os.path.join(
323+
Path(__file__).parent,
324+
"..",
325+
"..",
326+
"bird",
327+
"postprocess",
328+
"data_conditional_mean",
329+
)
330+
field_dict = {}
331+
# Check that assertion error is sent if too few snapshots
332+
try:
333+
kla_spec1, cstar_spec1, _ = compute_fitted_kla(
334+
species_names=["CO2"],
335+
case_folder=case_folder,
336+
)
337+
except AssertionError:
338+
pass
339+
340+
# Create dummy time folders
341+
for time_folder in [str(entry) for entry in range(81, 89)]:
342+
shutil.copytree(
343+
os.path.join(case_folder, "80"),
344+
os.path.join(case_folder, time_folder),
345+
)
346+
kla_spec1, cstar_spec1, _ = compute_fitted_kla(
347+
species_names=["CO2"],
348+
case_folder=case_folder,
349+
num_warmup=100,
350+
num_samples=100,
351+
)
352+
for time_folder in [str(entry) for entry in range(81, 89)]:
353+
shutil.rmtree(os.path.join(case_folder, time_folder))
354+
355+
# Create dummy time folders to trigger bootstrapping
356+
for time_folder in [str(entry) for entry in range(81, 91)]:
357+
shutil.copytree(
358+
os.path.join(case_folder, "80"),
359+
os.path.join(case_folder, time_folder),
360+
)
361+
kla_spec1, cstar_spec1, _ = compute_fitted_kla(
362+
species_names=["CO2"],
363+
case_folder=case_folder,
364+
num_warmup=100,
365+
num_samples=100,
366+
)
367+
for time_folder in [str(entry) for entry in range(81, 91)]:
368+
shutil.rmtree(os.path.join(case_folder, time_folder))
369+
370+
318371
if __name__ == "__main__":
319-
test_compute_superficial_gas_velocity()
320-
test_compute_gh()
372+
# test_compute_superficial_gas_velocity()
373+
# test_compute_gh()
321374
# test_ave_y_liq()
322375
# test_ave_conc_liq()
376+
test_fitted_kla()

0 commit comments

Comments
 (0)