CFDSolver.setAeroProblem(ap) cfdPoints = CFDSolver.getInitialSurfaceCoordinates(CFDSolver.allWallsGroup) # print "cfdPoints", cfdPoints # exit() if 0: setVolumeGrid(structU, ntimeintervalsspectral, b, xRot, CFDSolver, cfdPoints, complex_flag, False) CFDSolver(ap) setVolumeGrid(structU, ntimeintervalsspectral, b, xRot, CFDSolver, cfdPoints, complex_flag, True) index_r = 3881 - 1 # rho residual1 = CFDSolver.getResidual(ap) print("res rho", residual1[index_r]) print("res ivx", residual1[index_r + 1]) print("res ivy", residual1[index_r + 2]) print("res ivz", residual1[index_r + 3]) if 1: setVolumeGrid(structU, ntimeintervalsspectral, b, xRot, CFDSolver, cfdPoints, complex_flag, False) CFDSolver(ap) # surgery room 1 # local section # inviscidcentralflux:
class TestCmplxStep(reg_test_classes.CmplxRegTest): """ Tests that sensitives calculated from solving an adjoint are correct. and jacobian vector products are accurate. based on old regression tests_cs 12, and 14 """ N_PROCS = 2 h = 1e-40 def setUp(self): if not hasattr(self, "name"): # return immediately when the setup method is being called on the based class and NOT the # classes created using parametrized # this will happen when training, but will hopefully be fixed down the line return super().setUp() options = copy.copy(adflowDefOpts) options["outputdirectory"] = os.path.join(baseDir, options["outputdirectory"]) options.update(self.options) self.ffdFile = os.path.join(baseDir, "../../input_files/mdo_tutorial_ffd.fmt") mesh_options = copy.copy(IDWarpDefOpts) mesh_options.update({"gridFile": options["gridfile"]}) self.ap = copy.deepcopy(self.aero_prob) # Setup aeroproblem self.ap.evalFuncs = self.evalFuncs # add the default dvs to the problem for dv in defaultAeroDVs: self.ap.addDV(dv) self.CFDSolver = ADFLOW_C(options=options, debug=True) self.CFDSolver.setMesh(USMesh_C(options=mesh_options)) self.CFDSolver.setDVGeo(setDVGeo(self.ffdFile, cmplx=True)) # propagates the values from the restart file throughout the code self.CFDSolver.getResidual(self.ap) def cmplx_test_aero_dvs(self): if not hasattr(self, "name"): # return immediately when the setup method is being called on the based class and NOT the # classes created using parametrized # this will happen when training, but will hopefully be fixed down the line return for dv in ["alpha", "mach"]: # defaultAeroDVs: funcsSens = defaultdict(lambda: {}) setattr(self.ap, dv, getattr(self.ap, dv) + self.h * 1j) self.CFDSolver.resetFlow(self.ap) self.CFDSolver(self.ap, writeSolution=False) self.assert_solution_failure() funcs = {} self.CFDSolver.evalFunctions(self.ap, funcs) setattr(self.ap, dv, getattr(self.ap, dv) - self.h * 1j) for f in self.ap.evalFuncs: key = self.ap.name + "_" + f dv_key = dv + "_" + self.ap.name funcsSens[key][dv_key] = numpy.imag(funcs[key]) / self.h if MPI.COMM_WORLD.rank == 0: print("====================================") print(self.ap.alpha) print(self.ap.mach) print(self.name, funcsSens) print("====================================") self.handler.root_add_dict("Eval Functions Sens:", funcsSens, rtol=1e-8, atol=5e-10) def cmplx_test_geom_dvs(self): if not hasattr(self, "name"): # return immediately when the setup method is being called on the based class and NOT the # classes created using parametrized # this will happen when training, but will hopefully be fixed down the line return # redo the setup for a cmplx test funcsSens = defaultdict(lambda: {}) xRef = {"twist": [0.0] * 6, "span": [0.0], "shape": numpy.zeros(72, dtype="D")} for dv in ["span", "twist", "shape"]: xRef[dv][0] += self.h * 1j self.CFDSolver.resetFlow(self.ap) self.CFDSolver.DVGeo.setDesignVars(xRef) self.CFDSolver(self.ap, writeSolution=False) self.assert_solution_failure() funcs = {} self.CFDSolver.evalFunctions(self.ap, funcs) xRef[dv][0] -= self.h * 1j for f in self.ap.evalFuncs: key = self.ap.name + "_" + f dv_key = dv funcsSens[key][dv_key] = numpy.imag(funcs[key]) / self.h err_msg = "Failed value for: {}".format(key + " " + dv_key) ref_val = self.handler.db["Eval Functions Sens:"][key][dv_key] ref_val = ref_val.flatten()[0] numpy.testing.assert_allclose(funcsSens[key][dv_key], ref_val, atol=5e-9, rtol=5e-9, err_msg=err_msg) if MPI.COMM_WORLD.rank == 0: print("====================================") print(self.name, funcsSens) print("====================================")
class TestJacVecFwdCS(reg_test_classes.CmplxRegTest): """ Tests jacobian vector products against CS. """ N_PROCS = 2 h = 1e-40 def setUp(self): if not hasattr(self, "name"): # return immediately when the setup method is being called on the based class and NOT the # classes created using parametrized # this will happen when training, but will hopefully be fixed down the line return super().setUp() options = copy.copy(adflowDefOpts) options["outputdirectory"] = os.path.join(baseDir, options["outputdirectory"]) options.update(self.options) self.ap = copy.deepcopy(self.aero_prob) # Setup aeroproblem self.ap = copy.deepcopy(self.aero_prob) # add the default dvs to the problem for dv in defaultAeroDVs: self.ap.addDV(dv) # add the default dvs to the problem for dv in defaultAeroDVs: self.ap.addDV(dv) self.CFDSolver = ADFLOW_C(options=options, debug=True) # propagates the values from the restart file throughout the code self.CFDSolver.getResidual(self.ap) # ------------------- Derivative routine checks ---------------------------- def cmplx_test_wDot(self): if not hasattr(self, "name"): # return immediately when the setup method is being called on the based class and NOT the # classes created using parametrized # this will happen when training, but will hopefully be fixed down the line return # perturb each input and check that the outputs match the FD to with in reason wDot = self.CFDSolver.getStatePerturbation(314) resDot_CS, funcsDot_CS, fDot_CS = self.CFDSolver.computeJacobianVectorProductFwd( wDot=wDot, residualDeriv=True, funcDeriv=True, fDeriv=True, mode="CS", h=self.h) rtol, atol = getTol(tol=1e-9) self.handler.root_print("||dR/dw * wDot||") self.handler.par_add_norm("||dR/dw * wDot||", resDot_CS, rtol=rtol, atol=atol) self.handler.root_print("dFuncs/dw * wDot") self.handler.root_add_dict("dFuncs/dw * wDot", funcsDot_CS, rtol=rtol, atol=atol) self.handler.root_print("||dF/dw * wDot||") self.handler.par_add_norm("||dF/dw * wDot||", fDot_CS, rtol=rtol, atol=atol) def cmplx_test_xVDot(self): if not hasattr(self, "name"): # return immediately when the setup method is being called on the based class and NOT the # classes created using parametrized # this will happen when training, but will hopefully be fixed down the line return # perturb each input and check that the outputs match the FD to with in reason xVDot = self.CFDSolver.getSpatialPerturbation(314) rtol, atol = getTol(tol=1e-10) resDot_cs, funcsDot_cs, fDot_cs = self.CFDSolver.computeJacobianVectorProductFwd( xVDot=xVDot, residualDeriv=True, funcDeriv=True, fDeriv=True, mode="CS", h=self.h) self.handler.root_print("||dR/dXv * xVDot||") self.handler.par_add_norm("||dR/dXv * xVDot||", resDot_cs, rtol=rtol, atol=atol) # These can be finiky sometimes so a bigger tolerance. self.handler.root_print("dFuncs/dXv * xVDot") self.handler.root_add_dict("dFuncs/dXv * xVDot", funcsDot_cs, rtol=rtol * 10, atol=atol * 10) self.handler.root_print("||dF/dXv * xVDot||") self.handler.par_add_norm("||dF/dXv * xVDot||", fDot_cs, rtol=rtol, atol=atol) def cmplx_test_xDvDot(self): if not hasattr(self, "name"): # return immediately when the setup method is being called on the based class and NOT the # classes created using parametrized # this will happen when training, but will hopefully be fixed down the line return rtol, atol = getTol(tol=1e-10) # perturb each input and check that the outputs match the FD to with in reason for aeroDV in self.ap.DVs.values(): key = aeroDV.key self.handler.root_print(" -> %s" % key) xDvDot = {key: 1.0} resDot_cs, funcsDot_cs, fDot_cs = self.CFDSolver.computeJacobianVectorProductFwd( xDvDot=xDvDot, residualDeriv=True, funcDeriv=True, fDeriv=True, mode="CS", h=self.h) self.handler.root_print("||dR/d%s||" % key) self.handler.par_add_norm("||dR/d%s||" % key, resDot_cs, rtol=rtol, atol=atol) self.handler.root_print("dFuncs/d%s" % key) self.handler.root_add_dict("dFuncs/d%s" % key, funcsDot_cs, rtol=rtol, atol=atol) self.handler.root_print("||dF/d%s||" % key) self.handler.par_add_norm("||dF/d%s||" % key, fDot_cs, rtol=rtol, atol=atol)