Skip to content
Draft
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
35 changes: 19 additions & 16 deletions examples/ramses/run_kh.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,12 @@ def rho_map(rmin, rmax):
else:
return rho_1

def rhovel_map(rmin, rmax):
rho = rho_map(rmin, rmax)

def vel_map(rmin, rmax):
x, y, z = rmin

ampl = 0.01
n = 2
pert = np.sin(2 * np.pi * n * x / (xs))

sigma = 0.05 / (2**0.5)
gauss1 = np.exp(-((y - y_interface) ** 2) / (2 * sigma * sigma))
gauss2 = np.exp(-((y + y_interface) ** 2) / (2 * sigma * sigma))
Expand All @@ -186,29 +183,35 @@ def rhovel_map(rmin, rmax):
# Alternative formula (See T. Tricco paper)
# interf_sz = ys/32
# vx = np.arctan(y/interf_sz)/np.pi
# return vx

if y > y_interface:
return vslip / 2, ampl * pert, 0
else:
return -vslip / 2, ampl * pert, 0

def P_map(rmin, rmax):
x, y, z = rmin

vx = 0
if np.abs(y) > y_interface:
vx = vslip / 2
if y > y_interface:
return P_2
else:
vx = -vslip / 2
return P_1

return (vx * rho, ampl * pert * rho, 0)
def rhovel_map(rmin, rmax):
rho = rho_map(rmin, rmax)
vx, vy, vz = vel_map(rmin, rmax)

return (vx * rho, vy * rho, vz * rho)

def rhoetot_map(rmin, rmax):
rho = rho_map(rmin, rmax)
rhovel = rhovel_map(rmin, rmax)
P = P_map(rmin, rmax)

rhovel2 = rhovel[0] * rhovel[0] + rhovel[1] * rhovel[1] + rhovel[2] * rhovel[2]
rhoekin = 0.5 * rhovel2 / rho
Comment on lines 208 to 213
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The rhoetot_map function calls rho_map and then rhovel_map, which in turn calls rho_map again. This redundant call to rho_map can be a performance bottleneck, especially since these functions are called for each cell during initialization.

You can optimize this by calculating the kinetic energy directly from the velocity and density, avoiding the call to rhovel_map and its redundant computation.

Suggested change
rho = rho_map(rmin, rmax)
rhovel = rhovel_map(rmin, rmax)
P = P_map(rmin, rmax)
rhovel2 = rhovel[0] * rhovel[0] + rhovel[1] * rhovel[1] + rhovel[2] * rhovel[2]
rhoekin = 0.5 * rhovel2 / rho
rho = rho_map(rmin, rmax)
vx, vy, vz = vel_map(rmin, rmax)
P = P_map(rmin, rmax)
rhoekin = 0.5 * rho * (vx**2 + vy**2 + vz**2)


x, y, z = rmin

if y > y_interface:
P = P_2
else:
P = P_1

return rhoekin + P / (gamma - 1)

model.set_field_value_lambda_f64("rho", rho_map)
Expand Down