Skip to content

Commit

Permalink
Added example on plotting stiffness coefficients.
Browse files Browse the repository at this point in the history
  • Loading branch information
hprats committed Dec 20, 2024
1 parent 0d0ccce commit 52109b4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
50 changes: 50 additions & 0 deletions docs/source/plotting_results.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ plt.show()

<div style="text-align: center;"> <img src="https://github.com/hprats/ZacrosTools/blob/main/examples/DRM_on_PtHfC/coverage_per_sitetype.png?raw=true" alt="Coverage per site type" width="700"/> </div>

---

### Plotting the number of molecules produced

This example demonstrates how to plot the number of molecules produced over time for different gas-phase species in a single KMC simulation.
Expand Down Expand Up @@ -104,6 +106,54 @@ plt.show()

---

## Plotting stiffness scaling coefficients

In simulations where stiffness scaling is employed, it's often useful to visualize how the stiffness coefficients evolve over time. Stiffness coefficients may remain constant for a while and then abruptly change at specific times. Using a step plot makes it easy to highlight these jumps:

```python
import numpy as np
import matplotlib.pyplot as plt
from zacrostools.kmc_output import parse_general_output_file

data = parse_general_output_file(
output_file="simulation_results/CH4_5.179e-04#CO2_6.105e-04/general_output.txt",
parse_stiffness_coefficients=True)

stiffness_df = data['stiffness_scaling_coefficients']

# Identify columns corresponding to stiffness steps
step_columns = [col for col in stiffness_df.columns if col not in ['time', 'nevents']]

# Filter out steps where all coefficients are 1.0 (no change)
filtered_steps = [col for col in step_columns if not np.all(np.isclose(stiffness_df[col], 1.0))]

plt.figure(figsize=(8, 6))
for step in filtered_steps:
plt.step(
stiffness_df['time'],
stiffness_df[step],
where='post',
label=step,
linewidth=1.5
)

plt.yscale('log')
plt.xlabel('Simulated time (s)', fontsize=14)
plt.ylabel('Stiffness coefficient', fontsize=14)
plt.legend(fontsize=12, title_fontsize=12)
plt.grid(True, which="both", ls="--", linewidth=0.5)

plt.tight_layout()
plt.savefig('stiffness_coefficients.png', dpi=300, bbox_inches='tight', transparent=False)
plt.show()
```

The `parse_general_output_file` function extracts stiffness scaling coefficients from the general output file of a simulation. After filtering out steps that do not change (remain at a factor of 1.0), the code creates a step plot showing how the coefficients evolve over time.

<div style="text-align: center;"> <img src="https://github.com/hprats/ZacrosTools/blob/main/examples/DRM_on_PtHfC/stiffness_coefficients.png?raw=true" alt="Stiffness coefficients" width="600"/> </div>

---

## Creating heatmaps from a set of KMC simulations

When running a set of KMC simulations at various operating conditions, 2D heatmaps can be created using the `plot_heatmap` function from the `zacrostools` library.
Expand Down
Binary file modified examples/DRM_on_PtHfC/stiffness_coefficients.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions examples/DRM_on_PtHfC/stiffness_coefficients.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from zacrostools.kmc_output import parse_general_output_file

data = parse_general_output_file(
output_file="simulation_results/CH4_3.728e-01#CO2_4.394e-01/general_output.txt",
output_file="simulation_results/CH4_5.179e-04#CO2_6.105e-04/general_output.txt",
parse_stiffness_coefficients=True)

stiffness_df = data['stiffness_scaling_coefficients']
Expand All @@ -14,12 +14,11 @@

plt.figure(figsize=(8, 6))
for step in filtered_steps:
plt.plot(
plt.step(
stiffness_df['time'],
stiffness_df[step],
where='post',
label=step,
marker='o',
linestyle='-',
linewidth=1.5
)

Expand Down

0 comments on commit 52109b4

Please sign in to comment.