test(vv): Added more scripts to verify GridFire behavior
This commit is contained in:
@@ -8,8 +8,13 @@ import sys
|
||||
|
||||
from gridfire.solver import PointSolverTimestepContext
|
||||
from gridfire._gridfire.engine.scratchpads import StateBlob
|
||||
|
||||
from fourdst.composition import Composition
|
||||
import gridfire
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
class LogEntries(Enum):
|
||||
Step = "Step"
|
||||
t = "t"
|
||||
@@ -17,6 +22,7 @@ class LogEntries(Enum):
|
||||
eps = "eps"
|
||||
Composition = "Composition"
|
||||
ReactionContributions = "ReactionContributions"
|
||||
MassFractions = "MassFractions"
|
||||
|
||||
|
||||
class StepLogger:
|
||||
@@ -24,17 +30,40 @@ class StepLogger:
|
||||
self.num_steps : int = 0
|
||||
self.steps : List[Dict[LogEntries, Any]] = []
|
||||
|
||||
# def log_step(self, ctx: PointSolverTimestepContext):
|
||||
# comp_data: Dict[str, SupportsFloat] = {}
|
||||
# for species in ctx.engine.getNetworkSpecies(ctx.state_ctx):
|
||||
# sid = ctx.engine.getSpeciesIndex(ctx.state_ctx, species)
|
||||
# comp_data[species.name()] = ctx.state[sid]
|
||||
# entry : Dict[LogEntries, Any] = {
|
||||
# LogEntries.Step: ctx.num_steps,
|
||||
# LogEntries.t: ctx.t,
|
||||
# LogEntries.dt: ctx.dt,
|
||||
# LogEntries.eps: ctx.state[-1],
|
||||
# LogEntries.Composition: comp_data,
|
||||
# }
|
||||
# self.steps.append(entry)
|
||||
# self.num_steps += 1
|
||||
|
||||
def log_step(self, ctx: PointSolverTimestepContext):
|
||||
comp_data: Dict[str, SupportsFloat] = {}
|
||||
for species in ctx.engine.getNetworkSpecies(ctx.state_ctx):
|
||||
sid = ctx.engine.getSpeciesIndex(ctx.state_ctx, species)
|
||||
comp_data[species.name()] = ctx.state[sid]
|
||||
full_comp = ctx.composition
|
||||
|
||||
comp_data: Dict[str, float] = {}
|
||||
mass_frac: Dict[str, float] = {}
|
||||
for species in full_comp.getRegisteredSpecies():
|
||||
comp_data[species.name()] = full_comp.getMolarAbundance(species)
|
||||
mass_frac[species.name()] = full_comp.getMassFraction(species)
|
||||
|
||||
rhs_calc = ctx.engine.getMostRecentRHSCalculation(ctx.state_ctx)
|
||||
instantaneous_eps = rhs_calc.energy if rhs_calc else 0.0
|
||||
|
||||
entry : Dict[LogEntries, Any] = {
|
||||
LogEntries.Step: ctx.num_steps,
|
||||
LogEntries.t: ctx.t,
|
||||
LogEntries.dt: ctx.dt,
|
||||
LogEntries.eps: ctx.state[-1],
|
||||
LogEntries.eps: instantaneous_eps,
|
||||
LogEntries.Composition: comp_data,
|
||||
LogEntries.MassFractions: mass_frac,
|
||||
}
|
||||
self.steps.append(entry)
|
||||
self.num_steps += 1
|
||||
@@ -47,6 +76,7 @@ class StepLogger:
|
||||
LogEntries.dt.value: step[LogEntries.dt],
|
||||
LogEntries.eps.value: step[LogEntries.eps],
|
||||
LogEntries.Composition.value: step[LogEntries.Composition],
|
||||
LogEntries.MassFractions.value: step[LogEntries.MassFractions],
|
||||
}
|
||||
for step in self.steps
|
||||
]
|
||||
@@ -67,6 +97,32 @@ class StepLogger:
|
||||
with open(filename, 'w') as f:
|
||||
json.dump(out_data, f, indent=4)
|
||||
|
||||
@property
|
||||
def t(self) -> np.ndarray:
|
||||
return np.array([step[LogEntries.t] for step in self.steps])
|
||||
|
||||
@property
|
||||
def df(self) -> pd.DataFrame:
|
||||
if not self.steps:
|
||||
return pd.DataFrame()
|
||||
|
||||
flat_data = []
|
||||
for step in self.steps:
|
||||
row = {
|
||||
"Step": step[LogEntries.Step],
|
||||
"t": step[LogEntries.t],
|
||||
"dt": step[LogEntries.dt],
|
||||
"eps": step[LogEntries.eps],
|
||||
}
|
||||
X_dict = {f"X_{sp}": x for sp, x in step[LogEntries.MassFractions].items()}
|
||||
row.update(step[LogEntries.Composition])
|
||||
row.update(X_dict)
|
||||
flat_data.append(row)
|
||||
|
||||
df = pd.DataFrame(flat_data)
|
||||
|
||||
df = df.ffill().fillna(0.0)
|
||||
return df
|
||||
|
||||
def summary(self) -> Dict[str, Any]:
|
||||
if not self.steps:
|
||||
@@ -78,3 +134,8 @@ class StepLogger:
|
||||
"FinalComposition": final_step[LogEntries.Composition],
|
||||
}
|
||||
return summary_data
|
||||
|
||||
def reset(self):
|
||||
self.num_steps = 0
|
||||
self.steps : List[Dict[LogEntries, Any]] = []
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
savefig.facecolor: auto
|
||||
savefig.edgecolor: auto
|
||||
savefig.format: pdf
|
||||
savefig.bbox: tight
|
||||
|
||||
xtick.minor.visible : True
|
||||
ytick.minor.visible : True
|
||||
|
||||
xtick.direction : in
|
||||
ytick.direction : in
|
||||
|
||||
xtick.top : True
|
||||
ytick.right : True
|
||||
|
||||
xtick.major.size : 8
|
||||
xtick.minor.size : 4
|
||||
|
||||
ytick.major.size : 8
|
||||
ytick.minor.size : 4
|
||||
|
||||
xtick.labelsize : 18
|
||||
ytick.labelsize : 19
|
||||
|
||||
font.size : 20
|
||||
|
||||
font.family : serif
|
||||
text.usetex : True
|
||||
lines.color: C0
|
||||
patch.edgecolor: black
|
||||
text.color: black
|
||||
axes.facecolor: white
|
||||
axes.edgecolor: black
|
||||
axes.labelcolor: black
|
||||
axes.prop_cycle: cycler('color', ['1f77b4', 'ff7f0e', '2ca02c', 'd62728', '9467bd', '8c564b', 'e377c2', '7f7f7f', 'bcbd22', '17becf'])
|
||||
xtick.color: black
|
||||
ytick.color: black
|
||||
grid.color: b0b0b0
|
||||
figure.facecolor: white
|
||||
figure.edgecolor: white
|
||||
savefig.dpi: figure
|
||||
|
||||
Reference in New Issue
Block a user