The solver (nsfds3.solver)

The solver package contains the following main objects:

The following example gives the general philosophy to use nsfds3:

from nsfds3.solver import CfgSetup, FDTD
from nsfds3.cpgrid import build_mesh

# Initialize simulation parameter
cfg = CfgSetup()    # or cfg = CfgSetup('path_to_configfile.conf')

# Make the mesh
msh = build_mesh(cfg)

# Create and run simulation
fdtd = FDTD(msh, cfg)
fdtd.run()

# Show result
fdtd.show(view='p', nans=True, buffer=True, grid=False, obstacles=True)
class nsfds3.solver.CfgSetup(cfgfile=None, last=False, verbose=False)[source]

Setup configuration of the solver.

Parameters:
  • cfgfile (str, optional) – If None, CfgSetup takes defaults values. If a valid configuration file is provided, CfgSetup takes the value contained in this file.

  • last (bool, optional) – If cfgfile is not provided, try to load the last configuration used if it exists.

  • verbose (bool, optional) – Verbose mode

Note

Hereafter, one can find the correspondance between entries in the configuration file (on the left) and CfgSetup attributes (on the right) and their default values.

[general]
version                     -> self.version
quiet                       -> self.quiet = False

[files]
directory                   -> self.files.directory = 'data/'
name                        -> self.files.name = 'tmp'
overwrite                   -> self.files.overwrite = False

[thermophysics]
norm                        -> self.tp.norm = False
rho0                        -> self.tp.rho0 = 101325.0
t0                          -> self.tp.T0 - self.tp.Tref = 20.0
gamma                       -> self.tp.gamma = 1.4

[geometry]
force                       -> self.geo.force = False
free                        -> self.geo.free = True
file                        -> self.geo.file = None
name                        -> self.geo.name = None
kwargs                      -> self.geo.kwargs = None
curvname                    -> self.geo.curvname = None
bc                          -> self.geo.bc = 'WWWWWW'
shape                       -> self.geo.shape = (128, 96, 32)
origin                      -> self.geo.origin = None
steps                       -> self.geo.steps = (1., 1., 1.)
flat                        -> self.geo.flat = None
bz grid points              -> self.geo.bz_n = 20
bz filter order             -> self.geo.bz_filter_order = 3
bz stretch order            -> self.geo.bz_stretch_order = 3
bz stretch factor           -> self.geo.bz_stretch_factor = 2

[sources]
on                          -> self.src.on = False
origin                      -> self.src.origin = (),
S0                          -> self.src.S0 = ()
Bx                          -> self.src.Bx = ()
By                          -> self.src.By = ()
Bz                          -> self.src.Bz = ()
kx                          -> self.src.kx = ()
ky                          -> self.src.ky = ()
kz                          -> self.src.kz = ()
k                           -> self.src.k = ()
R                           -> self.src.Rx = ()
evolution                   -> self.src.evolution = ()

[flow]
type                        -> self.flw.ftype = None
components                  -> self.flw.components = (0, 0, 0)

[probes]
variables                   -> self.prb.vars = ()
locations                   -> self.prb.locs = ()

[solver]
mp                          -> self.sol.mp = False
timings                     -> self.sol.timings = False
nan_check                   -> self.sol.nan_check = False
resume                      -> self.sol.resume = False
viscous fluxes              -> self.sol.vsc = True
vorticity                   -> self.sol.vrt = True
shock capture               -> self.sol.cpt = True
selective filter            -> self.sol.flt = True
selective filter n-strength -> self.sol.xnu_n = 0.2
selective filter 0-strength -> self.sol.xnu_0 = 0.01
nt                          -> self.sol.nt = 50
ns                          -> self.sol.ns = 10
cfl                         -> self.sol.cfl = 0.5
save fields                 -> self.sol.save = True

[graphics]
figures                     -> self.gra.fig = True
probes                      -> self.gra.prb = True
bz                          -> self.gra.bz = True
bc                          -> self.gra.bc = True
fps                         -> self.gra.fps = 24
Attributes:
dt

Time step.

frequencies

Calculate the minimum and maximum frequencies for the simulation.

has_grid_backup

Return whether a CartesianGrid or CurvilinearGrid already exist for this configuration or not.

ndim

Dimension of the grid.

version

Version of configuration file.

Methods

check_version()

Check version of the configuration.

get_grid_backup()

Return existing CartesianGrid or CurvilinearGrid instance for this grid configuration if found, else return None.

load([cfgfile, last])

Load configuration file cfgfile.

overwrite()

Overwrite configuration file.

parse_literal(value)

Parse value.

run()

Run the parser.

write(fname)

Write a configuration file with current configuration.

check_version()[source]

Check version of the configuration.

property dt

Time step.

Note

dt is a read only attribute. It is automatically updated if one of geo.steps, sol.cfl, tp.c0 or flw.components attributes is modified.

property frequencies

Calculate the minimum and maximum frequencies for the simulation.

Returns:

A tuple containing the minimum and maximum frequencies. The minimum frequency is calculated as 2 divided by the product of the time step and the number of time steps. The maximum frequency is calculated as the Courant-Friedrichs-Lewy (CFL) condition divided by 10 times the time step.

Return type:

tuple

get_grid_backup()[source]

Return existing CartesianGrid or CurvilinearGrid instance for this grid configuration if found, else return None.

property has_grid_backup

Return whether a CartesianGrid or CurvilinearGrid already exist for this configuration or not.

load(cfgfile=None, last=False)[source]

Load configuration file cfgfile. If file is not found, fallback to default configuration.

Parameters:
  • cfgfile (str, optional) – If None, CfgSetup takes defaults values. If a valid configuration file is provided, CfgSetup takes the value contained in this file.

  • last (bool, optional) – If cfgfile is not provided or not found, try to load the last configuration used if it exists and if last is True.

property ndim

Dimension of the grid.

overwrite()[source]

Overwrite configuration file.

static parse_literal(value)[source]

Parse value.

run()[source]

Run the parser.

property version

Version of configuration file.

write(fname)[source]

Write a configuration file with current configuration.

class nsfds3.solver.CustomInitialConditions(cfg, msh)[source]

Customize initial conditions for acoustic pressure and/or velocities.

Parameters:

Notes

To setup custom initial conditions, one must override the CustomInitialConditions.__post_init__ method. The following attributes can be customized:

  • self.p: acoustic pressure (without static pressure)

  • self.vx: x-component of velocity

  • self.vy: y-component of velocity

  • self.vz: z-component of velocity

Example

class Ring(CustomInitialConditions):

    def __post_init__(self):
        origin = 100, 100
        k = 12
        self.p = self.super_gaussian(origin, k=12)
Attributes:
custom

Report whether initial conditions are provided or not.

has_old_fields

Report whether old fields have been initialized or not.

vars

Return initial conditions that have been provided.

Methods

check()

Check that initial conditions are valid ones.

check_older_run()

Report whether there is an older run or not.

resume()

Setup fields to resume last simulation with the current config.

show()

Show initial conditions graphically.

super_gaussian(origin[, kx, ky, kz, k, Bx, ...])

Super Gaussian distribution.

check()[source]

Check that initial conditions are valid ones.

check_older_run()[source]

Report whether there is an older run or not.

property custom

Report whether initial conditions are provided or not.

property has_old_fields

Report whether old fields have been initialized or not.

resume()[source]

Setup fields to resume last simulation with the current config. Set r, ru, rv[, rw], re instance attributes to the values at the last iteration itmax reached during last simulation.

show()[source]

Show initial conditions graphically.

super_gaussian(origin, kx=2, ky=2, kz=2, k=1, Bx=None, By=None, Bz=None, Rx=0)[source]

Super Gaussian distribution.

\[sg = e^{- (\sqrt{(x - x_0)^2 + (y - y_0)^2} + \sqrt{x_r^2 + y_r^2})^{eta} / (B_x \delta x))^{eta}}\]
Parameters:
x, y, znumpy.ndarray

Axis of the grid.

ix0, iy0, iz0int

Indices of the initial position of the pulse \((x_0, y_0)\).

Bx, By, Bzfloat, optional

Widths \(B_x, B_y, B_z\) of the pulse in m. 0.1 by default.

kx, ky, kz, k: float, optional

Orders \(eta\) of the pulse. Order 2 by default for axis and 1 for global.

Rx: float, optional

Radius following x for annular source

property vars

Return initial conditions that have been provided.

class nsfds3.solver.FDTD(cfg, msh, ics=None, quiet=None, timings=None, overwrite=None)[source]

Solve Navier-Stokes equations using Finite Difference Time Domain (FDTD) technique.

Parameters:
  • cfg (CfgSetup) – Configuration of the simulation

  • msh (CartesianGrid, CurvilinearGrid) – Grid used for the simulation

  • ics (CustomInitialConditions) – Custom Initial conditions

  • quiet (bool) – If True, display informations on the standard output

  • timings (bool) – If True, display complete timings during the simulation

  • overwrite (bool) – If True, automatically overwrite files

  • nan_check (bool) – If True, exit simulation when nan are encountered. This option may degrade computation time.

Notes

When the simulation is complete, one can use the show method to display the desired field at the last iteration, or inspect the fld object that gathers all conservative variables at the last iteration.

Finite differences schemes, Runge-Kutta algorithm and selective filter are applied using the technique described in [1]. The shock capturing procedure is applied using the technique described in [2].

References

[1]

C. Bogey, C. Bailly, “A family of low dispersive and low dissipative explicit schemes for flow and noise computations”, Journal of Computational Physics, Volume 194, Issue 1, 2004, Pages 194-214.

[2]

C. Bogey, N. de Cacqueray, C. Bailly, “A shock-capturing methodology based on adaptative spatial filtering for high-order non-linear computations”, Journal of Computational Physics, Volume 228, Issue 5, 2009, Pages 1447-1465.

Methods

run()

Run simulation.

save_objects()

Save cfg and msh objects.

show([view, vmin, vmax])

Show results.

from_cfg

run()[source]

Run simulation.

save_objects()[source]

Save cfg and msh objects.

show(view='p', vmin=None, vmax=None, **kwargs)[source]

Show results.

class nsfds3.solver.SourceSet(shape, origin, flat=None, **kwargs)[source]

Set of pressure sources in Super Gaussian form.

Super Gaussian [1] can be declared as initial conditions or time evolving pressure fluctuations. To declare time evolving source, the evolution argument must be provided.

\[p_{\text{ring}} = S_0 e^{- (\sqrt{(x - x_0)^2 + (y - y_0)^2} + \sqrt{x_r^2 + y_r^2})^{\beta} / (B_x \delta x))^{\beta}}\]

References

[1]

S. Kang et al. « A Physics-Based Approach to Oversample Multi-Satellite, Multi-Species Observations to a Common Grid ». Preprint. Gases/Remote Sensing/Data Processing and Information Retrieval, 23 août 2018. https://doi.org/10.5194/amt-2018-253.

Parameters:
  • shape (tuple) – Shape of the computational domain.

  • origin (tuple or (tuple, )) – Coordinates of the center. Can be a tuple or (tuple, ). If a tuple of tuples is provided, all tuples must have the same length.

  • flat (tuple or None) – Whether geometry is flattened or not. If a tuple is provided, it must provide (flat axis, flat index).

  • S0 (float or (float, ). Optional.) – Amplitude \(S_0\) of the pulse in Pa. 1 by default. Parameter amplitude is 1 by default for each source. Can be float for a single source or tuple of floats for multiple sources.

  • Bx (float or (float, ). Optional.) – Width \(B_x, B_y, B_z\) of the sources. Parameter width is 0.1 by default for each source. Can be positive int for a single source or tuple of positive ints for multiple sources.

  • By (float or (float, ). Optional.) – Width \(B_x, B_y, B_z\) of the sources. Parameter width is 0.1 by default for each source. Can be positive int for a single source or tuple of positive ints for multiple sources.

  • Bz (float or (float, ). Optional.) – Width \(B_x, B_y, B_z\) of the sources. Parameter width is 0.1 by default for each source. Can be positive int for a single source or tuple of positive ints for multiple sources.

  • k1 (int or (int, ). Optional.) – Order of the sources. Must be positive (if not, absolute is taken). Parameter order is 2 by default for each source. Can be int for a single source or tuple of positive ints for multiple sources.

  • k2 (int or (int, ). Optional.) – Order of the sources. Must be positive (if not, absolute is taken). Parameter order is 2 by default for each source. Can be int for a single source or tuple of positive ints for multiple sources.

  • k3 (int or (int, ). Optional.) – Order of the sources. Must be positive (if not, absolute is taken). Parameter order is 2 by default for each source. Can be int for a single source or tuple of positive ints for multiple sources.

  • k (int or (int, ). Optional.) – Order of the sources. Must be positive (if not, absolute is taken). Parameter order is 2 by default for each source. Can be int for a single source or tuple of positive ints for multiple sources.

  • Rx (float or (float, ). Optional.) – Radius for the case of annular source. 0 by default. Can be float for a single source or tuple of floats for multiple sources.

  • on (bool or (bool, ). Optional) – Whether source is on or not. Parameter on is False by default for each source. Can be bool for a single source or tuple of bool for multiple sources.

  • evolution (float/func, or (float/func, ). Optional.) –

    Time evolution of the source. Parameter evolution is None by default for each source. If evolution is None, the source will be an initial condition. If evolution is a float, it will describe the frequency of a sinusoidal time evolution. If evolution is a function, the time evolution of the source will be the result of evolution(t) where t is the time axis calculated as follows:

    import numpy as np
    t = np.linspace(0, nt * dt, nt + 1)
    

    where nt and dt are the number of time step and the time step setup in the configuration, respectively.

Example

# Declare 2 initial conditions, the first one located at (100, 100) with an amplitude of 10 # and a x-width of 0.1 grid points. The second source is located at (150, 150) with the same # amplitude and a x-width of 0.2 grid points s = SourceSet(origin=((100, 100), (150, 150)), S0=10, Bx=(0.1, 0.2))

Attributes:
Bx

x-widths of the source. Will be converted to positive integer if not.

By

y-widths of the source. Will be converted to positive integer if not.

Bz

z-widths of the source. Will be converted to positive integer if not.

Rx

Radius in the case of annular source.

S0

Amplitudes of the source.

evolution

Time evolution of the sources.

k

Global order of the source.

kwargs

Return a list of dictionnaries providing keyword arguments of the sources.

kx

Order of the source following x.

ky

Order of the source following y.

kz

Order of the source following z.

on

Report whether sources must be activated or not.

origin

Methods

update()

Update parameters.

property Bx

x-widths of the source. Will be converted to positive integer if not.

property By

y-widths of the source. Will be converted to positive integer if not.

property Bz

z-widths of the source. Will be converted to positive integer if not.

property Rx

Radius in the case of annular source.

property S0

Amplitudes of the source.

property evolution

Time evolution of the sources.

property k

Global order of the source. Will be converted to positive integer if not.

property kwargs

Return a list of dictionnaries providing keyword arguments of the sources.

property kx

Order of the source following x. Will be converted to positive integer if not.

property ky

Order of the source following y. Will be converted to positive integer if not.

property kz

Order of the source following z. Will be converted to positive integer if not.

property on

Report whether sources must be activated or not.

update()[source]

Update parameters.