Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions tests/python/physics/lgrngn_cond_substepping.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from libcloudphxx import lgrngn
from libcloudphxx import common

import pandas as pd
import csv

# wrapper for timing excecution time
def wrapper(func, opts, th, rv, rhod):
Expand Down Expand Up @@ -284,13 +284,29 @@ def test(RH_formula, step_count, substep_count, exact_substep, constp, mixing, a
results['sstp_cond_adapt_drw2_max'] = opts_init.sstp_cond_adapt_drw2_max
records.append(results)

def _write_csv(path, rows):
os.makedirs(os.path.dirname(path), exist_ok=True)
# Collect fieldnames deterministically (preserve insertion order from first row, then add any missing keys)
fieldnames = []
seen = set()
for r in rows:
for k in r.keys():
if k not in seen:
fieldnames.append(k)
seen.add(k)
with open(path, "w", newline="") as f:
w = csv.DictWriter(f, fieldnames=fieldnames)
w.writeheader()
for r in rows:
w.writerow(r)

# save results to a CSV file for refdata comparison and for plotting
df = pd.DataFrame(records)
df['sd_conc'] = opts_init.sd_conc # Add the column to all rows at once
df['RH_max'] = opts_init.RH_max
df['dt'] = 1
os.makedirs("test_results", exist_ok=True)
df.to_csv("test_results/lgrngn_cond_substepping_results.csv", index=False)
for r in records:
r['sd_conc'] = opts_init.sd_conc
r['RH_max'] = opts_init.RH_max
r['dt'] = 1

_write_csv("test_results/lgrngn_cond_substepping_results.csv", records)

# Optionally save as reference data
if '--save-ref' in sys.argv:
Expand All @@ -300,8 +316,8 @@ def test(RH_formula, step_count, substep_count, exact_substep, constp, mixing, a
refdata_dir = os.path.join(script_dir, "refdata")
os.makedirs(refdata_dir, exist_ok=True)
refdata_file = os.path.join(refdata_dir, "lgrngn_cond_substepping_refdata.csv")
df.to_csv(refdata_file, index=False)
print("Reference data saved to: test_results/lgrngn_cond_substepping_refdata.csv")
_write_csv(refdata_file, records)
print(f"Reference data saved to: {refdata_file}")
print("Future runs can be compared against this reference using:")
print(" python lgrngn_cond_substepping_test.py")

68 changes: 57 additions & 11 deletions tests/python/physics/lgrngn_cond_substepping_plot.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
import pandas as pd
import csv
import matplotlib.pyplot as plt
import os


def _read_csv_rows(path):
with open(path, newline="") as f:
return list(csv.DictReader(f))


def _to_bool(v):
s = str(v).strip().lower()
if s in ("true", "1", "t", "yes", "y"):
return True
if s in ("false", "0", "f", "no", "n"):
return False
raise ValueError(f"Cannot parse boolean from: {v!r}")


def _to_float(v):
return float(str(v).strip())


def _to_int(v):
return int(float(str(v).strip()))


# Load the results
df = pd.read_csv("test_results/lgrngn_cond_substepping_results.csv")
rows = _read_csv_rows("test_results/lgrngn_cond_substepping_results.csv")

variables = [
('ss', 'Supersaturation [%]'),
Expand All @@ -16,25 +39,48 @@
('exectime', 'Execution time [s]'),
]

RH_formula_names = {
0 : "pv_cc", 1: "rv_cc", 2 :"pv_tet", 3 : "rv_tet"
}
# RH_formula_names = {
# 0 : "pv_cc", 1: "rv_cc", 2 :"pv_tet", 3 : "rv_tet"
# }

group_cols = ['mixing', 'constp', 'exact_sstp', 'adaptive', 'sstp_cond_act']
RH_formulas = sorted(df['RH_formula'].unique())
# RH_formulas = sorted({ _to_int(r['RH_formula']) for r in rows })
RH_formulas = sorted({ r['RH_formula'] for r in rows })

os.makedirs("test_results/plots", exist_ok=True)

def _group_key(row):
return (
_to_bool(row['mixing']),
_to_bool(row['constp']),
_to_bool(row['exact_sstp']),
_to_bool(row['adaptive']),
_to_int(row['sstp_cond_act']),
)


for var, ylabel in variables:
fig, axs = plt.subplots(2, 2, figsize=(14, 10), sharex=True)
axs = axs.flatten()
for i, RH_formula in enumerate(RH_formulas):
ax = axs[i]
subdf = df[df['RH_formula'] == RH_formula]
for name, group in subdf.groupby(group_cols):
label = ', '.join(f"{col}={val}" for col, val in zip(group_cols, name))
ax.plot(group['sstp_cond'], group[var], marker='o', label=label, linestyle='--')
ax.set_title(f'RH_formula={RH_formula_names[RH_formula]}')
subrows = [r for r in rows if r['RH_formula'] == RH_formula]

grouped = {}
for r in subrows:
k = _group_key(r)
grouped.setdefault(k, []).append(r)

for key, group in grouped.items():
label = ', '.join(
f"{col}={val}" for col, val in zip(group_cols, key)
)
group_sorted = sorted(group, key=lambda r: _to_int(r['sstp_cond']))
x = [_to_int(r['sstp_cond']) for r in group_sorted]
y = [_to_float(r[var]) for r in group_sorted]
ax.plot(x, y, marker='o', label=label, linestyle='--')
# ax.set_title(f'RH_formula={RH_formula_names[RH_formula]}')
ax.set_title(f'RH_formula={RH_formula}')
ax.set_xlabel('Number of substeps')
ax.set_ylabel(ylabel)
ax.set_xscale('log')
Expand Down
Loading
Loading