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("====================================")
print('shape[13] Derivatives:') reg_write(funcsSens['mdo_tutorial_cl']['shape'][0][13], 1e-10,1e-10) reg_write(funcsSens['mdo_tutorial_cmz']['shape'][0][13], 1e-10,1e-10) reg_write(funcsSens['mdo_tutorial_drag']['shape'][0][13], 1e-10,1e-10) else: # For the complex....we just do successive perturbation for ii in range(3): xRef = {'twist':[0.0, 0.0], 'span':[0.0], 'shape':numpy.zeros(72, dtype='D')} if ii == 0: xRef['twist'][0] += h*1j elif ii == 1: xRef['span'][0] += h*1j else: xRef['shape'][13] += h*1j CFDSolver.resetFlow(ap) DVGeo.setDesignVars(xRef) CFDSolver(ap, writeSolution=False) funcs = {} CFDSolver.evalFunctions(ap, funcs) if MPI.COMM_WORLD.rank == 0: if ii == 0: for key in ['cl','cmz','drag']: print('funcs[%s]:'%key) reg_write(numpy.real(funcs['mdo_tutorial_%s'%key]),1e-10,1e-10) if ii == 0: print('Twist[0] Derivatives:') elif ii == 1: print('Span Derivatives:')