Skip to content

Commit

Permalink
allow full fun3d residuals list to be extracted
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-engelstad committed Oct 31, 2023
1 parent 669faad commit d81cc99
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
13 changes: 13 additions & 0 deletions funtofem/driver/oneway_aero_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,19 @@ def _setup_grid_filepaths(self):
grid_filepaths.append(filepath)
# set the grid filepaths into the fun3d aim
self.flow_aim.grid_filepaths = grid_filepaths

# also setup the mapbc files
mapbc_filepaths = []
for scenario in self.model.scenarios:
filepath = os.path.join(
fun3d_dir,
scenario.name,
"Flow",
f"{scenario.fun3d_project_name}.mapbc",
)
mapbc_filepaths.append(filepath)
# set the mapbc filepaths into the fun3d aim
self.flow_aim.mapbc_filepaths = mapbc_filepaths
return

@property
Expand Down
43 changes: 28 additions & 15 deletions funtofem/interface/fun3d_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,13 +629,15 @@ def post(self, scenario, bodies):
"""

# report warning if flow residual too large
resid = self.get_forward_residual(step=scenario.steps) # step=scenario.steps
resid = self.get_forward_residual(step=scenario.steps, all=True) # step=scenario.steps
if self.comm.rank == 0:
print(f"Forward residuals = {resid}")
self._forward_done = True
self._forward_resid = resid
if abs(resid.real) > 1.0e-10:
if abs(np.linalg.norm(resid).real) > self.forward_tolerance:
if self.comm.rank == 0:
print(
f"Warning: fun3d forward flow residual = {resid} > 1.0e-10, is rather large..."
f"\tWarning: fun3d forward flow residual = {resid} > {self.forward_tolerance:.2e}, is rather large..."
)

self.fun3d_flow.post()
Expand Down Expand Up @@ -964,21 +966,23 @@ def post_adjoint(self, scenario, bodies):
"""

# report warning if flow residual too large
resid = self.get_adjoint_residual(step=scenario.steps)
resid = self.get_adjoint_residual(step=scenario.steps, all=True)
if self.comm.rank == 0:
print(f"Adjoint residuals = {resid}")
self._adjoint_done = True
self._adjoint_resid = resid
if abs(resid.real) > 1.0e-10:
if abs(np.linalg.norm(resid).real) > self.adjoint_tolerance:
if self.comm.rank == 0:
print(
f"Warning fun3d adjoint residual = {resid} > 1.0e-10, is rather large..."
f"\tWarning fun3d adjoint residual = {resid} > {self.adjoint_tolerance:.2e}, is rather large..."
)

# solve the initial condition adjoint
self.fun3d_adjoint.post()
os.chdir(self.root_dir)
return

def get_forward_residual(self, step=0):
def get_forward_residual(self, step=0, all=False):
"""
Returns L2 norm of scalar residual norms for each flow state
L2norm([R1,...,R6])
Expand All @@ -987,31 +991,40 @@ def get_forward_residual(self, step=0):
----------
step: int
the time step number
all: bool
whether to return a list of all residuals or just a scalar
"""
if not self._forward_done:
residuals = self.fun3d_flow.get_flow_rms_residual(step)
if self.comm.rank == 0:
print(f"Forward residuals = {residuals}")
return np.linalg.norm(residuals)
else:
return self._forward_resid
residuals = self._forward_resid

if all:
return residuals
else:
return np.linalg.norm(residuals)

def get_adjoint_residual(self, step=0):
def get_adjoint_residual(self, step=0, all=False):
"""
Returns L2 norm of list of scalar adjoint residuals L2norm([R1,...,R6])
Parameters
----------
step: int
the time step number
all: bool
whether to return a list of all residuals or a scalar
"""
if not self._adjoint_done:
residuals = self.fun3d_adjoint.get_flow_rms_residual(step)
if self.comm.rank == 0:
print(f"Adjoint residuals = {residuals}")
return np.linalg.norm(residuals)
else:
return self._adjoint_resid
residuals = self._adjoint_resid

if all:
return residuals
else:
return np.linalg.norm(residuals)

def set_states(self, scenario, bodies, step):
"""
Expand Down

0 comments on commit d81cc99

Please sign in to comment.