Skip to content

Commit b850e3c

Browse files
committed
Update computation of flow quantities in parallel
1 parent 18e8a21 commit b850e3c

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

src/oasismove/problems/__init__.py

+44-25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import subprocess
55
from collections import defaultdict
66
from os import getpid, path
7+
78
import numpy as np
89
from dolfin import *
910

@@ -126,27 +127,34 @@ def QC(u):
126127
def compute_flow_quantities(u, L, nu, mesh, t, tstep, dt, h, outlet_area=1, boundary=None, outlet_ids=[], inlet_ids=[],
127128
id_wall=0, period=1.0, newfolder=None, dynamic_mesh=False, write_to_file=False):
128129
"""
129-
Compute max and mean Reynolds number using velocities U_max & U_mean,
130-
kinematic viscosity (nu) and characteristic length (L)
131-
Also computes the CFL number, and fluxes through boundaries
130+
Compute max and mean Reynolds numbers, CFL numbers, and fluxes through boundaries.
131+
132+
Args:
133+
u (Function): Velocity field.
134+
L (float): Characteristic length.
135+
nu (float): Kinematic viscosity of fluid.
136+
mesh (Mesh): Computational mesh.
137+
t (float): Current time.
138+
tstep (int): Current time step.
139+
dt (float): Time step size.
140+
h (float): Mesh element size.
141+
outlet_area (float): Area of the outlet, default is 1.
142+
boundary (MeshFunction): Boundary mesh function, default is None.
143+
outlet_ids (list of int): List of outlet boundary IDs, default is empty list.
144+
inlet_ids (list of int): List of inlet boundary IDs, default is empty list.
145+
id_wall (int): Wall boundary ID, default is 0.
146+
period (float): Period of oscillation, default is 1.0.
147+
newfolder (str): Directory for output files, default is None.
148+
dynamic_mesh (bool): Flag for dynamic mesh, default is False.
149+
write_to_file (bool): Flag to write results to file, default is False.
132150
"""
133151

134-
# Compute and printCFL number
135-
DG = FunctionSpace(mesh, "DG", 0)
136-
U = project(sqrt(inner(u, u)), DG)
137-
138-
U_mean = U.vector().get_local().mean()
139-
U_max = U.vector().get_local().max()
140-
141-
re_mean = U_mean * L / nu
142-
re_max = U_max * L / nu
152+
# Compute boundary flow rates
143153
flux_in = []
144154
flux_out = []
145155
flux_wall = []
146156
re_outlet = 0
147-
148157
if boundary is not None:
149-
# Compute Reynolds number at outlet
150158
ds = Measure("ds", subdomain_data=boundary)
151159
n = FacetNormal(mesh)
152160
u_dot_n = dot(u, n)
@@ -163,27 +171,38 @@ def compute_flow_quantities(u, L, nu, mesh, t, tstep, dt, h, outlet_area=1, boun
163171
# Compute Womersley number
164172
omega = 2 * np.pi / period
165173
D_outlet = np.sqrt(4 * outlet_area / np.pi)
166-
167-
womersley_number_outlet = D_outlet * np.sqrt(omega / nu)
174+
womersley_number = D_outlet * np.sqrt(omega / nu)
168175

169176
# Recompute edge length if mesh has moved
170177
if dynamic_mesh:
171178
h = mesh.hmin()
172179

173-
cfl = U.vector().get_local() * dt / h
174-
max_cfl = cfl.max()
175-
mean_cfl = cfl.mean()
180+
# Compute velocity used to estimate max and mean velocity, CFL and Reynolds number
181+
DG = FunctionSpace(mesh, "DG", 0)
182+
U = project(sqrt(inner(u, u)), DG)
183+
local_U = U.vector().get_local()
184+
U_array = MPI.comm_world.gather(local_U, 0)
176185

177186
if MPI.rank(MPI.comm_world) == 0:
187+
# Gather velocity arrays and compute max and mean velocity, CFL and Reynolds number
188+
U_gathered = np.concatenate(U_array)
189+
U_mean = np.mean(U_gathered)
190+
U_max = np.max(U_gathered)
191+
192+
cfl_mean = U_mean * dt / h
193+
cfl_max = U_max * dt / h
194+
195+
re_mean = U_mean * L / nu
196+
re_max = U_max * L / nu
197+
178198
info_green(
179199
'Time = {0:2.4e}, timestep = {1:6d}, max Reynolds number={2:2.3f}, mean Reynolds number={3:2.3f}, outlet Reynolds number={4:2.3f}, Womersley number={5:2.3f}, max velocity={6:2.3f}, mean velocity={7:2.3f}, max CFL={8:2.3f}, mean CFL={9:2.3f}'
180-
.format(t, tstep, re_max, re_mean, re_outlet, womersley_number_outlet, U_max, U_mean, max_cfl,
181-
mean_cfl))
200+
.format(t, tstep, re_max, re_mean, re_outlet, womersley_number, U_max, U_mean, cfl_max, cfl_mean))
182201

183-
if write_to_file:
184-
data = [t, tstep, re_max, re_mean, re_outlet, womersley_number_outlet, U_max, U_mean, max_cfl,
185-
mean_cfl] + flux_in + flux_out + flux_wall
186-
write_data_to_file(newfolder, data, "flow_metrics.txt")
202+
if write_to_file:
203+
data = [t, tstep, re_max, re_mean, re_outlet, womersley_number, U_max, U_mean, cfl_max,
204+
cfl_mean] + flux_in + flux_out + flux_wall
205+
write_data_to_file(newfolder, data, "flow_metrics.txt")
187206

188207

189208
def write_data_to_file(save_path, data, filename):

0 commit comments

Comments
 (0)