def write_n2(configuration_file_path: str, n2_file_path: str = None, overwrite: bool = False): """ Write the N2 diagram of the problem in file n2.html :param configuration_file_path: :param n2_file_path: :param overwrite: """ if not n2_file_path: n2_file_path = pth.join(pth.dirname(configuration_file_path), "n2.html") n2_file_path = pth.abspath(n2_file_path) if not overwrite and pth.exists(n2_file_path): raise FastFileExistsError( "N2-diagram file %s not written because it already exists. " "Use overwrite=True to bypass." % n2_file_path, n2_file_path, ) make_parent_dir(n2_file_path) problem = FASTOADProblemConfigurator(configuration_file_path).get_problem() problem.setup() problem.final_setup() om.n2(problem, outfile=n2_file_path, show_browser=False) _LOGGER.info("N2 diagram written in %s", n2_file_path)
def test_oad_process(cleanup): """ Test for the overall aircraft design process. """ problem = FASTOADProblemConfigurator(pth.join(INPUT_FOLDER_PATH, "oad_process.toml")).get_problem() problem.model.aicraft.set_input_defaults('data:geometry:horizontal_tail:sweep_25', val=10., units='deg') ref_inputs = pth.join(INPUT_FOLDER_PATH, XML_NAME) get_problem_after_setup(problem).write_needed_inputs(ref_inputs, VariableXmlStandardFormatter()) problem.read_inputs() print('\n') problem.setup(check=True) problem.set_solver_print(level=2) with Timer(name="Mass-performance loop:"): problem.run_model() problem.write_outputs() if not pth.exists(RESULTS_FOLDER_PATH): os.mkdir(RESULTS_FOLDER_PATH) om.view_connections( problem, outfile=pth.join(RESULTS_FOLDER_PATH, "connections.html"), show_browser=False ) om.n2(problem, outfile=pth.join(RESULTS_FOLDER_PATH, "n2.html"), show_browser=False) # Check that weight-performances loop correctly converged assert_allclose( problem["data:weight:aircraft:OWE"], problem["data:weight:airframe:mass"] + problem["data:weight:propulsion:mass"] + problem["data:weight:systems:mass"] + problem["data:weight:furniture:mass"], rtol=5e-2, ) assert_allclose( problem["data:weight:aircraft:MZFW"], problem["data:weight:aircraft:OWE"] + problem["data:weight:aircraft:max_payload"], rtol=5e-2, ) assert_allclose( problem["data:weight:aircraft:MTOW"], problem["data:weight:aircraft:OWE"] + problem["data:weight:aircraft:max_payload"] + problem["data:mission:sizing:fuel"], rtol=5e-2, ) assert_allclose(problem.get_val("data:mission:sizing:fuel", units="kg"), 203.65, atol=1) # noinspection PyTypeChecker assert_allclose(problem.get_val("data:weight:aircraft:max_payload", units="kg"), 400.0, atol=1) # noinspection PyTypeChecker assert_allclose(problem["data:handling_qualities:static_margin"], 0.08515, atol=1e-2) # noinspection PyTypeChecker assert_allclose(problem.get_val("data:weight:aircraft:MTOW", units="kg"), 1650.24, atol=1) # noinspection PyTypeChecker assert_allclose(problem.get_val("data:weight:aircraft:payload", units="kg"), 360., atol=1) # noinspection PyTypeChecker assert_allclose(problem.get_val("data:weight:aircraft:OWE", units="kg"), 1046.61, atol=1) # noinspection PyTypeChecker assert_allclose(problem.get_val("data:mission:sizing:main_route:cruise:fuel", units="kg"), 166.405, atol=1)
def test_oad_process(cleanup): """ Test for the overall aircraft design process. """ problem = FASTOADProblemConfigurator( pth.join(DATA_FOLDER_PATH, "oad_process.toml")).get_problem() ref_inputs = pth.join(DATA_FOLDER_PATH, "beechcraft_76.xml") get_problem_after_setup(problem).write_needed_inputs( ref_inputs, VariableXmlStandardFormatter()) problem.read_inputs() print('\n') problem.setup(check=True) problem.set_solver_print(level=2) problem.run_model() problem.write_outputs() if not pth.exists(RESULTS_FOLDER_PATH): os.mkdir(RESULTS_FOLDER_PATH) om.view_connections(problem, outfile=pth.join(RESULTS_FOLDER_PATH, "connections.html"), show_browser=False) om.n2(problem, outfile=pth.join(RESULTS_FOLDER_PATH, "n2.html"), show_browser=False) # Check that weight-performances loop correctly converged assert_allclose( problem["data:weight:aircraft:OWE"], problem["data:weight:airframe:mass"] + problem["data:weight:propulsion:mass"] + problem["data:weight:systems:mass"] + problem["data:weight:furniture:mass"], rtol=5e-2, ) assert_allclose( problem["data:weight:aircraft:MZFW"], problem["data:weight:aircraft:OWE"] + problem["data:weight:aircraft:max_payload"], rtol=5e-2, ) assert_allclose( problem["data:weight:aircraft:MTOW"], problem["data:weight:aircraft:OWE"] + problem["data:weight:aircraft:max_payload"] + problem["data:mission:sizing:fuel"], rtol=5e-2, )
def _run_problem( configuration_file_path: str, overwrite: bool = False, mode="run_model", auto_scaling: bool = False, ) -> FASTOADProblem: """ Runs problem according to provided file :param configuration_file_path: problem definition :param overwrite: if True, output file will be overwritten :param mode: 'run_model' or 'run_driver' :param auto_scaling: if True, automatic scaling is performed for design variables and constraints :return: the OpenMDAO problem after run """ problem = FASTOADProblemConfigurator(configuration_file_path).get_problem( read_inputs=True, auto_scaling=auto_scaling) outputs_path = pth.normpath(problem.output_file_path) if not overwrite and pth.exists(outputs_path): raise FastFileExistsError( "Problem not run because output file %s already exists. " "Use overwrite=True to bypass." % outputs_path, outputs_path, ) problem.setup() start_time = process_time() if mode == "run_model": problem.run_model() problem.optim_failed = False # Actually, we don't know else: problem.optim_failed = problem.run_driver() end_time = process_time() computation_time = round(end_time - start_time, 2) problem.write_outputs() if problem.optim_failed: _LOGGER.error("Optimization failed after " + str(computation_time) + " seconds") else: _LOGGER.info("Computation finished after " + str(computation_time) + " seconds") _LOGGER.info("Problem outputs written in %s", outputs_path) return problem
def run_non_regression_test( conf_file, legacy_result_file, result_dir, use_xfoil=False, vars_to_check=None, tolerance=5.0e-3, check_weight_perfo_loop=True, ): results_folder_path = pth.join(RESULTS_FOLDER_PATH, result_dir) configuration_file_path = pth.join(results_folder_path, conf_file) # Copy of configuration file and generation of problem instance ------------------ api.generate_configuration_file(configuration_file_path) # just ensure folders are created... shutil.copy(pth.join(DATA_FOLDER_PATH, conf_file), configuration_file_path) problem = FASTOADProblemConfigurator(configuration_file_path).get_problem() # Next trick is needed for overloading option setting from TOML file if use_xfoil and (system() == "Windows" or xfoil_path): problem.model.aerodynamics_landing._OPTIONS["use_xfoil"] = True if system() != "Windows": problem.model.aerodynamics_landing._OPTIONS["xfoil_exe_path"] = xfoil_path # BTW we narrow computed alpha range for sake of CPU time problem.model.aerodynamics_landing._OPTIONS["xfoil_alpha_min"] = 18.0 problem.model.aerodynamics_landing._OPTIONS["xfoil_alpha_max"] = 22.0 # Generation and reading of inputs ---------------------------------------- ref_inputs = pth.join(DATA_FOLDER_PATH, legacy_result_file) get_problem_after_setup(problem).write_needed_inputs(ref_inputs, VariableLegacy1XmlFormatter()) problem.read_inputs() problem.setup() # Run model --------------------------------------------------------------- problem.run_model() problem.write_outputs() om.view_connections( problem, outfile=pth.join(results_folder_path, "connections.html"), show_browser=False ) if check_weight_perfo_loop: # Check that weight-performances loop correctly converged assert_allclose( problem["data:weight:aircraft:OWE"], problem["data:weight:airframe:mass"] + problem["data:weight:propulsion:mass"] + problem["data:weight:systems:mass"] + problem["data:weight:furniture:mass"] + problem["data:weight:crew:mass"], atol=1, ) assert_allclose( problem["data:weight:aircraft:MZFW"], problem["data:weight:aircraft:OWE"] + problem["data:weight:aircraft:max_payload"], atol=1, ) assert_allclose( problem["data:weight:aircraft:MTOW"], problem["data:weight:aircraft:OWE"] + problem["data:weight:aircraft:payload"] + problem["data:mission:sizing:fuel"], atol=1, ) ref_var_list = VariableIO( pth.join(DATA_FOLDER_PATH, legacy_result_file), formatter=VariableLegacy1XmlFormatter(), ).read() row_list = [] for ref_var in ref_var_list: try: value = problem.get_val(ref_var.name, units=ref_var.units)[0] except KeyError: continue row_list.append( { "name": ref_var.name, "units": ref_var.units, "ref_value": ref_var.value[0], "value": value, } ) df = pd.DataFrame(row_list) df["rel_delta"] = (df.value - df.ref_value) / df.ref_value df["rel_delta"][(df.ref_value == 0) & (abs(df.value) <= 1e-10)] = 0.0 df["abs_rel_delta"] = np.abs(df.rel_delta) pd.set_option("display.max_rows", None) pd.set_option("display.max_columns", None) pd.set_option("display.width", 1000) pd.set_option("display.max_colwidth", 120) print(df.sort_values(by=["abs_rel_delta"])) if vars_to_check is not None: for name in vars_to_check: row = df.loc[df.name == name] assert_allclose(row.ref_value, row.value, rtol=tolerance) # assert np.all(df.abs_rel_delta.loc[df.name == name] < tolerance) else: assert np.all(df.abs_rel_delta < tolerance)
def test_oad_process(cleanup): """ Test the overall aircraft design process without and with optimization. """ test = FASTOADProblemConfigurator( pth.join(INPUT_FOLDER_PATH, "oad_process.toml")) problem = FASTOADProblemConfigurator( pth.join(INPUT_FOLDER_PATH, "oad_process.toml")).get_problem() recorder = om.SqliteRecorder("cases.sql") ref_inputs = pth.join(INPUT_FOLDER_PATH, XML_NAME) get_problem_after_setup(problem).write_needed_inputs( ref_inputs, VariableXmlStandardFormatter()) problem.read_inputs() print('\n') problem.setup(check=True) solver = problem.model.nonlinear_solver solver.add_recorder(recorder) problem.run_model() problem.write_outputs() if not pth.exists(RESULTS_FOLDER_PATH): os.mkdir(RESULTS_FOLDER_PATH) om.view_connections(problem, outfile=pth.join(RESULTS_FOLDER_PATH, "connections.html"), show_browser=False) om.n2(problem, outfile=pth.join(RESULTS_FOLDER_PATH, "n2.html"), show_browser=False) # Check that weight-performances loop correctly converged assert_allclose( problem["data:weight:aircraft:OWE"], problem["data:weight:airframe:mass"] + problem["data:weight:propulsion:mass"] + problem["data:weight:systems:mass"] + problem["data:weight:furniture:mass"], rtol=5e-2, ) assert_allclose( problem["data:weight:aircraft:MZFW"], problem["data:weight:aircraft:OWE"] + problem["data:weight:aircraft:max_payload"], rtol=5e-2, ) assert_allclose( problem["data:weight:aircraft:MTOW"], problem["data:weight:aircraft:OWE"] + problem["data:weight:aircraft:payload"] + problem["data:mission:sizing:fuel"], rtol=5e-2, ) if XML_NAME == "cirrus_sr22.xml": assert_allclose(problem.get_val("data:mission:sizing:fuel", units="kg"), 258.831, atol=1) # noinspection PyTypeChecker assert_allclose( problem["data:handling_qualities:stick_fixed_static_margin"], 0.0728, atol=1e-2) # noinspection PyTypeChecker assert_allclose(problem.get_val("data:weight:aircraft:MTOW", units="kg"), 1629.7406025, atol=1) # noinspection PyTypeChecker assert_allclose(problem.get_val("data:weight:aircraft:OWE", units="kg"), 1030.9167, atol=1) else: assert_allclose(problem.get_val("data:mission:sizing:fuel", units="kg"), 228.624, atol=1) # noinspection PyTypeChecker assert_allclose( problem["data:handling_qualities:stick_fixed_static_margin"], 0.0252, atol=1e-2) # noinspection PyTypeChecker assert_allclose(problem.get_val("data:weight:aircraft:MTOW", units="kg"), 1678.863295, atol=1) # noinspection PyTypeChecker assert_allclose(problem.get_val("data:weight:aircraft:OWE", units="kg"), 1090.2524, atol=1)
def write_xdsm( configuration_file_path: str, xdsm_file_path: str = None, overwrite: bool = False, depth: int = 2, wop_server_url=None, api_key=None, ): """ :param configuration_file_path: :param xdsm_file_path: :param overwrite: :param depth: :param wop_server_url: :param api_key: :return: """ if not xdsm_file_path: xdsm_file_path = pth.join(pth.dirname(configuration_file_path), "xdsm.html") xdsm_file_path = pth.abspath(xdsm_file_path) if not overwrite and pth.exists(xdsm_file_path): raise FastFileExistsError( "XDSM-diagram file %s not written because it already exists. " "Use overwrite=True to bypass." % xdsm_file_path, xdsm_file_path, ) make_parent_dir(xdsm_file_path) problem = FASTOADProblemConfigurator(configuration_file_path).get_problem() problem.setup() problem.final_setup() wop = WhatsOpt(url=wop_server_url, login=False) try: ok = wop.login(api_key=api_key, echo=False) except requests.exceptions.ConnectionError: if not wop_server_url and wop.url == PROD_URL: used_url = wop.url # If connection failed while attempting to reach the wop default URL, # that is the internal ONERA server, try with the external server try: wop = WhatsOpt(url=DEFAULT_WOP_URL) ok = wop.login(api_key=api_key, echo=False) except requests.exceptions.ConnectionError: _LOGGER.warning("Failed to connect to %s and %s", used_url, DEFAULT_WOP_URL) return else: _LOGGER.warning("Failed to connect to %s", wop.url) return if ok: xdsm = wop.push_mda(problem, { "--xdsm": True, "--name": None, "--dry-run": False, "--depth": depth }) generate_xdsm_html(xdsm, xdsm_file_path) else: wop.logout() _LOGGER.warning("Could not login to %s", wop.url)