Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
malihass committed Jan 15, 2025
1 parent d72a152 commit 7b9ea54
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 145 deletions.
69 changes: 44 additions & 25 deletions papers/tutorial/calibration_bsd/data/check_real.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import numpy as np
from prettyPlot.plotting import *
from matplotlib.cm import ScalarMappable
from matplotlib.colors import Normalize
from prettyPlot.plotting import *

target=np.load("target.npz")
A=np.load("dataset.npz")
nsim = A['y'].shape[0]
target = np.load("target.npz")
A = np.load("dataset.npz")
nsim = A["y"].shape[0]
print(f"nsim = {nsim}")
ceff = []
beff_fact = []
ceff_fail = []
beff_fact_fail = []
for i in range(nsim):
if abs(np.mean(A['y'][i,:])-1) > 1e-6:
print(f"beff_fact : {A['beff_fact'][i]:.2f}, ceff : {A['ceff'][i]:.2f}")
ceff.append(A['ceff'][i])
beff_fact.append(A['beff_fact'][i])
if abs(np.mean(A["y"][i, :]) - 1) > 1e-6:
print(
f"beff_fact : {A['beff_fact'][i]:.2f}, ceff : {A['ceff'][i]:.2f}"
)
ceff.append(A["ceff"][i])
beff_fact.append(A["beff_fact"][i])
else:
ceff_fail.append(A['ceff'][i])
beff_fact_fail.append(A['beff_fact'][i])
fig=plt.figure()
plt.plot(beff_fact, ceff, 'o', color='k')
plt.plot(beff_fact_fail, ceff_fail, 'o', color='r')
ceff_fail.append(A["ceff"][i])
beff_fact_fail.append(A["beff_fact"][i])
fig = plt.figure()
plt.plot(beff_fact, ceff, "o", color="k")
plt.plot(beff_fact_fail, ceff_fail, "o", color="r")
pretty_labels("beff_fact", "ceff")


Expand All @@ -32,13 +34,24 @@
cmap = plt.cm.viridis # Choose a colormap
color_map = ScalarMappable(norm=norm, cmap=cmap)

fig=plt.figure()
fig = plt.figure()
icount = 0
for i in range(nsim):
if abs(np.mean(A['y'][i,:])-1) > 1e-6:
plt.plot(A["x"],A["y"][i,:], color=color_map.to_rgba(beff_fact_arr[icount]))
if abs(np.mean(A["y"][i, :]) - 1) > 1e-6:
plt.plot(
A["x"],
A["y"][i, :],
color=color_map.to_rgba(beff_fact_arr[icount]),
)
icount += 1
plt.plot(target["x"],target["y"], '^', markersize=10, markeredgecolor='k', markerfacecolor='r')
plt.plot(
target["x"],
target["y"],
"^",
markersize=10,
markeredgecolor="k",
markerfacecolor="r",
)
pretty_labels("x", "y", 14, title="beff_fact")


Expand All @@ -47,17 +60,23 @@
cmap = plt.cm.viridis # Choose a colormap
color_map = ScalarMappable(norm=norm, cmap=cmap)

fig=plt.figure()
fig = plt.figure()
icount = 0
for i in range(nsim):
if abs(np.mean(A['y'][i,:])-1) > 1e-6:
plt.plot(A["x"],A["y"][i,:], color=color_map.to_rgba(ceff_arr[icount]))
icount +=1
plt.plot(target["x"],target["y"], '^', markersize=10, markeredgecolor='k', markerfacecolor='r')
if abs(np.mean(A["y"][i, :]) - 1) > 1e-6:
plt.plot(
A["x"], A["y"][i, :], color=color_map.to_rgba(ceff_arr[icount])
)
icount += 1
plt.plot(
target["x"],
target["y"],
"^",
markersize=10,
markeredgecolor="k",
markerfacecolor="r",
)
pretty_labels("x", "y", 14, title="ceff")



plt.show()


2 changes: 1 addition & 1 deletion papers/tutorial/calibration_bsd/make_dataset/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from prettyPlot.plotting import *
from utils import check_vol_cons, diam2vol, vol2diam


class Bubbles:
def __init__(
self,
Expand Down Expand Up @@ -45,4 +46,3 @@ def breakup(self, id_break: int, new_diam_list: list[float]) -> None:
# Reset the breakup rate factor
# This factor is useful for very small timesteps (this is not the efficiency factor)
self.breakup_rate_factor = 1.0

78 changes: 51 additions & 27 deletions papers/tutorial/calibration_bsd/make_dataset/generate_dataset.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import argparse
import os

import numpy as np
from flow import Bubbles
from models import (
Expand All @@ -7,10 +10,10 @@
)
from prettyPlot.plotting import *
from simulation import Simulation
import os
import argparse

parser = argparse.ArgumentParser(description="Generate data for training the surrogate")
parser = argparse.ArgumentParser(
description="Generate data for training the surrogate"
)
parser.add_argument(
"-filesave",
"--filesave",
Expand All @@ -21,32 +24,32 @@
default="dataset.npz",
)
args, unknown = parser.parse_known_args()
data_folder = os.path.join("..","data")
target = np.load(os.path.join(data_folder,"target.npz"))
data_folder = os.path.join("..", "data")
target = np.load(os.path.join(data_folder, "target.npz"))
x_pdf_target = target["x"]
beff_fact_data = []
ceff_data = []
x_pdf_data = []
y_pdf_data = []
x_pdf_data = []
y_pdf_data = []

try:
os.remove(args.filesave)
os.remove(args.filesave)
except FileNotFoundError:
pass

dt = 0.01
nt = 15000
isim=0
nsim=100
while isim<nsim:
isim = 0
nsim = 100
while isim < nsim:
isim += 1
ceff = np.random.uniform(0.02, 2)
beff_fact = np.random.uniform(0.8, 1.1)

# Initialize
bubbles = Bubbles(nbubbles=2000, diam=1e-3)
breakup_kwargs = {
"breakup_rate": ceff*beff_fact,
"breakup_rate": ceff * beff_fact,
"dt": dt,
"n_break": 2, # if n_break=2, this is binary breakup
"min_break_diam": 1e-6, # forbid breakup that lead to diameter < min_break_diam
Expand All @@ -66,42 +69,63 @@
breakup_kwargs=breakup_kwargs,
coalescence_kwargs=coalescence_kwargs,
)
result = sim.run(window_size=200,kill_failed_sim=True, x_pdf=x_pdf_target)

result = sim.run(window_size=200, kill_failed_sim=True, x_pdf=x_pdf_target)
if "x_pdf" in result:
mean_diameter_history = result["mean_diameter_history"]
y_pdf = result["y_pdf"]
x_pdf = result["x_pdf"]
start_ave_time = result["start_ave_time"]
start_ave_time = result["start_ave_time"]

# Plot results
time = np.arange(0, sim.nt * sim.dt, sim.dt)
plt.figure(figsize=(10, 6))
plt.plot(time, mean_diameter_history)
plt.plot(np.ones(10)*start_ave_time, np.linspace(np.amin(mean_diameter_history), np.amax(mean_diameter_history), 10), '--', color='k')
pretty_labels("Time [s]", "Mean bubble diameter [m]", 14, fontname="Times")
plt.plot(
np.ones(10) * start_ave_time,
np.linspace(
np.amin(mean_diameter_history),
np.amax(mean_diameter_history),
10,
),
"--",
color="k",
)
pretty_labels(
"Time [s]", "Mean bubble diameter [m]", 14, fontname="Times"
)
plt.close()

# Plot equilibrium BSD
plt.figure(figsize=(10, 6))
mean_bsd = np.mean(y_pdf, axis=0)
std_bsd = np.std(y_pdf, axis=0)
plt.plot(x_pdf, mean_bsd, color="k", linewidth=3)
plt.plot(x_pdf, mean_bsd+std_bsd, "--", color="k", linewidth=3)
plt.plot(x_pdf, mean_bsd-std_bsd, "--", color="k", linewidth=3)
plt.plot(x_pdf, mean_bsd + std_bsd, "--", color="k", linewidth=3)
plt.plot(x_pdf, mean_bsd - std_bsd, "--", color="k", linewidth=3)
pretty_labels("diameter [m]", "Bin count", 14, fontname="Times")
plt.close()
#plt.show()
# plt.show()
print(f"\nSUCCESS beff_fact = {beff_fact} ceff = {ceff}\n")
beff_fact_data.append(beff_fact)
ceff_data.append(ceff)
y_pdf_data.append(mean_bsd)
np.savez(os.path.join(data_folder,args.filesave), x=x_pdf_target, y=np.array(y_pdf_data), beff_fact=np.array(beff_fact_data), ceff=np.array(ceff_data))
np.savez(
os.path.join(data_folder, args.filesave),
x=x_pdf_target,
y=np.array(y_pdf_data),
beff_fact=np.array(beff_fact_data),
ceff=np.array(ceff_data),
)
else:
print(f"\nFAILED beff_fact = {beff_fact} ceff = {ceff}\n")
beff_fact_data.append(beff_fact)
ceff_data.append(ceff)
y_pdf_data.append(np.ones(len(x_pdf_target)))
np.savez(os.path.join(data_folder,args.filesave), x=x_pdf_target, y=np.array(y_pdf_data), beff_fact=np.array(beff_fact_data), ceff=np.array(ceff_data))


np.savez(
os.path.join(data_folder, args.filesave),
x=x_pdf_target,
y=np.array(y_pdf_data),
beff_fact=np.array(beff_fact_data),
ceff=np.array(ceff_data),
)
13 changes: 8 additions & 5 deletions papers/tutorial/calibration_bsd/make_dataset/generate_target.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import numpy as np
from flow import Bubbles
from models import (
Expand All @@ -7,7 +9,6 @@
)
from prettyPlot.plotting import *
from simulation import Simulation
import os

# Initialize
bubbles = Bubbles(nbubbles=2000, diam=1e-3)
Expand Down Expand Up @@ -53,13 +54,15 @@
mean_bsd = np.mean(y_pdf, axis=0)
std_bsd = np.std(y_pdf, axis=0)
plt.plot(x_pdf, mean_bsd, color="k", linewidth=3)
plt.plot(x_pdf, mean_bsd+std_bsd, "--", color="k", linewidth=3)
plt.plot(x_pdf, mean_bsd-std_bsd, "--", color="k", linewidth=3)
plt.plot(x_pdf, mean_bsd + std_bsd, "--", color="k", linewidth=3)
plt.plot(x_pdf, mean_bsd - std_bsd, "--", color="k", linewidth=3)
pretty_labels("diameter [m]", "Bin count", 14, fontname="Times")

dataFolder = os.path.join("..","data")
dataFolder = os.path.join("..", "data")
os.makedirs(dataFolder, exist_ok=True)

np.savez(os.path.join(dataFolder,"target.npz"), x=x_pdf, y=mean_bsd, sigma=std_bsd)
np.savez(
os.path.join(dataFolder, "target.npz"), x=x_pdf, y=mean_bsd, sigma=std_bsd
)

plt.show()
19 changes: 11 additions & 8 deletions papers/tutorial/calibration_bsd/make_dataset/main_target.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import numpy as np
from flow import Bubbles
from models import (
Expand All @@ -7,7 +9,6 @@
)
from prettyPlot.plotting import *
from simulation import Simulation
import os

# Initialize
bubbles = Bubbles(nbubbles=2000, diam=1e-3)
Expand Down Expand Up @@ -53,18 +54,20 @@
mean_bsd = np.mean(y_pdf, axis=0)
std_bsd = np.std(y_pdf, axis=0)
plt.plot(x_pdf, mean_bsd, color="k", linewidth=3)
plt.plot(x_pdf, mean_bsd+std_bsd, "--", color="k", linewidth=3)
plt.plot(x_pdf, mean_bsd-std_bsd, "--", color="k", linewidth=3)
plt.plot(x_pdf, mean_bsd + std_bsd, "--", color="k", linewidth=3)
plt.plot(x_pdf, mean_bsd - std_bsd, "--", color="k", linewidth=3)
pretty_labels("diameter [m]", "Bin count", 14, fontname="Times")

dataFolder = os.path.join("..","data")
dataFolder = os.path.join("..", "data")
os.makedirs(dataFolder, exist_ok=True)

np.savez(os.path.join(dataFolder,"target.npz"), x=x_pdf, y=mean_bsd, sigma=std_bsd)
np.savez(
os.path.join(dataFolder, "target.npz"), x=x_pdf, y=mean_bsd, sigma=std_bsd
)

## Plot equilibrium BSD
#plt.figure(figsize=(10, 6))
#plt.hist(sim.bubbles.diameters, bins=25)
#pretty_labels("diameter [m]", "Bin count", 14, fontname="Times")
# plt.figure(figsize=(10, 6))
# plt.hist(sim.bubbles.diameters, bins=25)
# pretty_labels("diameter [m]", "Bin count", 14, fontname="Times")

plt.show()
2 changes: 0 additions & 2 deletions papers/tutorial/calibration_bsd/make_dataset/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,3 @@ def simple_nary_breakup(bubbles: Bubbles, kwargs: dict) -> None:
# Check that we are not creating too small of bubbles
if np.amin(new_diameters) > min_break_diam:
bubbles.breakup(id_break=id1, new_diam_list=list(new_diameters))


Loading

0 comments on commit 7b9ea54

Please sign in to comment.