From 92be05f351dab8e980bb5565b814cb88884483e5 Mon Sep 17 00:00:00 2001 From: acrovato <a.crovato@uliege.be> Date: Sun, 26 May 2024 14:20:37 +0200 Subject: [PATCH] Refactor default values in APIs --- dart/api/core.py | 49 +++++++++++++------------------------- dart/api/cupydo.py | 5 +--- dart/api/internal/polar.py | 12 ++++------ dart/api/internal/trim.py | 18 ++++++-------- 4 files changed, 28 insertions(+), 56 deletions(-) diff --git a/dart/api/core.py b/dart/api/core.py index 332baa1..63af776 100644 --- a/dart/api/core.py +++ b/dart/api/core.py @@ -71,27 +71,20 @@ def init_dart(cfg, scenario='aerodynamic', task='analysis', viscous=False): raise RuntimeError('Problem dimension should be 2 or 3, but ' + cfg['Dim'] + ' was given!\n') else: _dim = cfg['Dim'] - # aoa and aos - if 'AoA' in cfg: - alpha = cfg['AoA'] * math.pi / 180 - else: - alpha = 0 + # angles of attack and side slip, and freestream Mach number + alpha = cfg.get('AoA', 0.) * math.pi / 180 + beta = cfg.get('AoS', 0.) * math.pi / 180 + minf = cfg.get('M_inf', 0.) if _dim == 2: - if 'AoS' in cfg and cfg['AoS'] != 0: + if beta != 0: raise RuntimeError('Angle of sideslip (AoS) should be zero for 2D problems!\n') - else: - beta = 0 if 'Symmetry' in cfg: raise RuntimeError('Symmetry boundary condition cannot be used for 2D problems!\n') else: - if 'AoS' in cfg: - if cfg['AoS'] != 0 and 'Symmetry' in cfg: - raise RuntimeError('Symmetry boundary condition cannot be used with nonzero angle of sideslip (AoS)!\n') - beta = cfg['AoS'] * math.pi / 180 - else: - beta = 0 + if beta != 0 and 'Symmetry' in cfg: + raise RuntimeError('Symmetry boundary condition cannot be used with nonzero angle of sideslip (AoS)!\n') # save format - if cfg['Format'] == 'vtk': + if cfg.get('Format', 'vtk') == 'vtk': try: import tboxVtk Writer = tboxVtk.VtkExport @@ -102,16 +95,9 @@ def init_dart(cfg, scenario='aerodynamic', task='analysis', viscous=False): else: Writer = tbox.GmshExport print('Results will be saved in gmsh format.\n') - # number of threads - if 'Threads' in cfg: - nthrd = cfg['Threads'] - else: - nthrd = 1 - # verbosity - if 'Verb' in cfg: - verb = cfg['Verb'] - else: - verb = 1 + # number of threads and verbosity + nthrd = cfg.get('Threads', 1) + verb = cfg.get('Verb', 1) # scenario and task type if scenario != 'aerodynamic' and scenario != 'aerostructural': raise RuntimeError('Scenario should be aerodynamic or aerostructural, but "' + scenario + '" was given!\n') @@ -124,7 +110,7 @@ def init_dart(cfg, scenario='aerodynamic', task='analysis', viscous=False): _qinf = None # Mesh creation - pars = {} if 'Pars' not in cfg else cfg['Pars'] + pars = cfg.get('Pars', {}) _msh = gmsh.MeshLoader(cfg['File']).execute(**pars) # add the wake if _dim == 2: @@ -166,7 +152,7 @@ def init_dart(cfg, scenario='aerodynamic', task='analysis', viscous=False): else: if 'Fuselage' in cfg: _mrf.addFixed(cfg['Fuselage']) - if 'Symmetry' in cfg: + if 'Symmetry' in cfg: _mrf.setSymmetry(cfg['Symmetry'], 1) for i in range(len(cfg['Wings'])): if i == 0: @@ -179,9 +165,9 @@ def init_dart(cfg, scenario='aerodynamic', task='analysis', viscous=False): _mrf = None # Problem creation - _pbl = dart.Problem(_msh, _dim, alpha, beta, cfg['M_inf'], cfg['S_ref'], cfg['c_ref'], cfg['x_ref'], cfg['y_ref'], cfg['z_ref']) + _pbl = dart.Problem(_msh, _dim, alpha, beta, minf, cfg['S_ref'], cfg['c_ref'], cfg['x_ref'], cfg['y_ref'], cfg['z_ref']) # add the field - _pbl.set(dart.Fluid(_msh, cfg['Fluid'], cfg['M_inf'], _dim, alpha, beta)) + _pbl.set(dart.Fluid(_msh, cfg['Fluid'], minf, _dim, alpha, beta)) # add the initial condition _pbl.set(dart.Initial(_msh, cfg['Fluid'], _dim, alpha, beta)) # add farfield boundary conditions (Neumann only, a DOF will be pinned automatically) @@ -236,10 +222,7 @@ def init_dart(cfg, scenario='aerodynamic', task='analysis', viscous=False): elif cfg['LSolver'] == 'MUMPS': linsol = tbox.Mumps() elif cfg['LSolver'] == 'GMRES': - gfil = cfg['G_fill'] if 'G_fill' in cfg else 2 - grst = cfg['G_restart'] if 'G_restart' in cfg else 50 - gtol = cfg['G_tol'] if 'G_tol' in cfg else 1e-5 - linsol = tbox.Gmres(gfil, 1e-6, grst, gtol, 200) + linsol = tbox.Gmres(cfg.get('G_fill', 2), 1e-6, cfg.get('G_restart', 50), cfg.get('G_tol', 1e-5), 200) else: raise RuntimeError('Available linear solvers: PARDISO, MUMPS or GMRES, but ' + cfg['LSolver'] + ' was given!\n') # initialize the nonlinear (outer) solver diff --git a/dart/api/cupydo.py b/dart/api/cupydo.py index 07646e9..a50922c 100644 --- a/dart/api/cupydo.py +++ b/dart/api/cupydo.py @@ -40,10 +40,7 @@ class Dart(FluidSolver): self.nodalInitPosX, self.nodalInitPosY, self.nodalInitPosZ = self.getNodalInitialPositions() # init save frequency (fsi) - if 'SaveFreq' in params: - self.saveFreq = params['SaveFreq'] - else: - self.saveFreq = sys.maxsize + self.saveFreq = params.get('SaveFreq', sys.maxsize) # generic init FluidSolver.__init__(self) diff --git a/dart/api/internal/polar.py b/dart/api/internal/polar.py index 1922425..198b57e 100644 --- a/dart/api/internal/polar.py +++ b/dart/api/internal/polar.py @@ -68,14 +68,10 @@ class Polar: _dart = init_dart(p, scenario='aerodynamic', task='analysis') self.dim, self.msh, self.wrt, self.pbl, self.bnd, self.sol = (_dart.get(key) for key in ['dim', 'msh', 'wrt', 'pbl', 'bnd', 'sol']) # save some parameters for later use - self.format = p['Format'] - try: - self.slice = p['Slice'] - self.tag = p['TagId'] - except: - self.slice = None - self.tag = None - self.mach = p['M_inf'] + self.format = p.get('Format', 'vtk') + self.slice = p.get('Slice') + self.tag = p.get('TagId') + self.mach = self.pbl.M_inf self.alphas = tU.myrange(p['AoA_begin'], p['AoA_end'], p['AoA_step']) def run(self): diff --git a/dart/api/internal/trim.py b/dart/api/internal/trim.py index 6fbe7e7..31c1028 100644 --- a/dart/api/internal/trim.py +++ b/dart/api/internal/trim.py @@ -49,9 +49,9 @@ class Trim: clTgt : float target lift coefficient alpha : float - initial angle of attack + current angle of attack dCl : float - initial (estimated) slope of lift curve + current slope of lift curve """ def __init__(self, p): """Setup and configure @@ -67,16 +67,12 @@ class Trim: _dart = init_dart(p, scenario='aerodynamic', task='analysis') self.dim, self.msh, self.wrt, self.pbl, self.bnd, self.sol = (_dart.get(key) for key in ['dim', 'msh', 'wrt', 'pbl', 'bnd', 'sol']) # save some parameters for later use - self.format = p['Format'] - try: - self.slice = p['Slice'] - self.tag = p['TagId'] - except: - self.slice = None - self.tag = None - self.mach = p['M_inf'] + self.format = p.get('Format', 'vtk') + self.slice = p.get('Slice') + self.tag = p.get('TagId') + self.mach = self.pbl.M_inf + self.alpha = self.pbl.alpha self.clTgt = p['CL'] - self.alpha = p['AoA']*math.pi/180 self.dCl = p['dCL'] def run(self): -- GitLab