|
| 1 | +import unittest, importlib, numpy as np, os, sys |
| 2 | +from funtofem import * |
| 3 | +from mpi4py import MPI |
| 4 | + |
| 5 | +np.random.seed(1234567) |
| 6 | +comm = MPI.COMM_WORLD |
| 7 | + |
| 8 | +tacs_loader = importlib.util.find_spec("tacs") |
| 9 | +caps_loader = importlib.util.find_spec("pyCAPS") |
| 10 | + |
| 11 | +base_dir = os.path.dirname(os.path.abspath(__file__)) |
| 12 | +csm_path = os.path.join(base_dir, "geometry", "ssw.csm") |
| 13 | +results_folder, _ = make_test_directories(comm, base_dir) |
| 14 | + |
| 15 | +if tacs_loader is not None and caps_loader is not None: |
| 16 | + from tacs import caps2tacs |
| 17 | + |
| 18 | +# check if we're in github to run only online vs offline tests |
| 19 | +in_github_workflow = bool(os.getenv("GITHUB_ACTIONS")) |
| 20 | +# in_github_workflow = True |
| 21 | +optional = True # whether to run optional tests |
| 22 | + |
| 23 | + |
| 24 | +@unittest.skipIf( |
| 25 | + tacs_loader is None or caps_loader is None, |
| 26 | + "skipping test using caps2tacs if caps or tacs are unavailable", |
| 27 | +) |
| 28 | +class TestTacsSteadyShapeDriver(unittest.TestCase): |
| 29 | + N_PROCS = 2 |
| 30 | + FILENAME = "tacs_steady_shape_driver.txt" |
| 31 | + FILEPATH = os.path.join(results_folder, FILENAME) |
| 32 | + |
| 33 | + @unittest.skipIf(in_github_workflow, "only run this test offline") |
| 34 | + def test_shape_steady_aeroelastic(self): |
| 35 | + # make the funtofem and tacs model |
| 36 | + f2f_model = FUNtoFEMmodel("wing") |
| 37 | + tacs_model = caps2tacs.TacsModel.build(csm_file=csm_path, comm=comm) |
| 38 | + tacs_model.mesh_aim.set_mesh( # need a refined-enough mesh for the derivative test to pass |
| 39 | + edge_pt_min=5, |
| 40 | + edge_pt_max=10, |
| 41 | + global_mesh_size=0.1, |
| 42 | + max_surf_offset=0.01, |
| 43 | + max_dihedral_angle=5, |
| 44 | + ).register_to( |
| 45 | + tacs_model |
| 46 | + ) |
| 47 | + f2f_model.structural = tacs_model |
| 48 | + |
| 49 | + # build a body which we will register variables to |
| 50 | + wing = Body.aeroelastic("wing") |
| 51 | + |
| 52 | + # setup the material and shell properties |
| 53 | + aluminum = caps2tacs.Isotropic.aluminum().register_to(tacs_model) |
| 54 | + |
| 55 | + nribs = int(tacs_model.get_config_parameter("nribs")) |
| 56 | + nspars = int(tacs_model.get_config_parameter("nspars")) |
| 57 | + nOML = nribs - 1 |
| 58 | + |
| 59 | + for irib in range(1, nribs + 1): |
| 60 | + caps2tacs.ShellProperty( |
| 61 | + caps_group=f"rib{irib}", material=aluminum, membrane_thickness=0.05 |
| 62 | + ).register_to(tacs_model) |
| 63 | + for ispar in range(1, nspars + 1): |
| 64 | + caps2tacs.ShellProperty( |
| 65 | + caps_group=f"spar{ispar}", material=aluminum, membrane_thickness=0.05 |
| 66 | + ).register_to(tacs_model) |
| 67 | + for iOML in range(1, nOML + 1): |
| 68 | + caps2tacs.ShellProperty( |
| 69 | + caps_group=f"OML{iOML}", material=aluminum, membrane_thickness=0.03 |
| 70 | + ).register_to(tacs_model) |
| 71 | + for prefix in ["LE", "TE"]: |
| 72 | + caps2tacs.ShellProperty( |
| 73 | + caps_group=f"{prefix}spar", material=aluminum, membrane_thickness=0.03 |
| 74 | + ).register_to(tacs_model) |
| 75 | + |
| 76 | + # register any shape variables to the wing which are auto-registered to tacs model |
| 77 | + for itc in range(2, 4): |
| 78 | + Variable.shape(name=f"tc{itc}").set_bounds( |
| 79 | + lower=0.4, value=0.2, upper=1.6 |
| 80 | + ).register_to(wing) |
| 81 | + |
| 82 | + # register the wing body to the model |
| 83 | + wing.register_to(f2f_model) |
| 84 | + |
| 85 | + # add remaining information to tacs model |
| 86 | + caps2tacs.PinConstraint("root").register_to(tacs_model) |
| 87 | + |
| 88 | + # make a funtofem scenario |
| 89 | + test_scenario = Scenario.steady("test", steps=10) |
| 90 | + Function.ksfailure().register_to(test_scenario) |
| 91 | + Function.mass().register_to(test_scenario) |
| 92 | + test_scenario.register_to(f2f_model) |
| 93 | + |
| 94 | + solvers = SolverManager(comm) |
| 95 | + solvers.flow = TestAerodynamicSolver(comm, f2f_model) |
| 96 | + aero_driver = TestAeroOnewayDriver(solvers, f2f_model) |
| 97 | + transfer_settings = TransferSettings(npts=200, beta=0.5) |
| 98 | + |
| 99 | + # setup the tacs model |
| 100 | + tacs_model.setup() |
| 101 | + |
| 102 | + tacs_driver = OnewayStructDriver.prime_loads( |
| 103 | + aero_driver, transfer_settings=transfer_settings, nprocs=2 |
| 104 | + ) |
| 105 | + |
| 106 | + max_rel_error = TestResult.derivative_test( |
| 107 | + "testaero=>tacs_shape_steady-aeroelastic", |
| 108 | + f2f_model, |
| 109 | + tacs_driver, |
| 110 | + TestTacsSteadyShapeDriver.FILEPATH, |
| 111 | + complex_mode=False, |
| 112 | + epsilon=1e-4, |
| 113 | + ) |
| 114 | + rtol = 1e-4 |
| 115 | + self.assertTrue(max_rel_error < rtol) |
| 116 | + return |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + if tacs_loader is not None and caps_loader is not None: |
| 121 | + if comm.rank == 0: |
| 122 | + open(TestTacsSteadyShapeDriver.FILEPATH, "w").close() |
| 123 | + |
| 124 | + unittest.main() |
0 commit comments