Skip to content

Utility

utility

This module provides several classes and functions to perform a number of pre-, syn-, and post-processing tasks. Users incorporate utility as required in their code, depending on what they would like to achieve.

TimestepAdaptor(dt_const, u, V, target_cfl=1.0, increase_tolerance=1.5, maximum_timestep=None)

Computes timestep based on CFL condition for provided velocity field

Parameters:

Name Type Description Default
dt_const

Constant whose value will be updated by the timestep adaptor

required
u

Velocity to base CFL condition on

required
V

FunctionSpace for reference velocity, usually velocity space

required
target_cfl

CFL number to target with chosen timestep

1.0
increase_tolerance

Maximum tolerance timestep is allowed to change by

1.5
maximum_timestep

Maximum allowable timestep

None
Source code in g-adopt/gadopt/utility.py
78
79
80
81
82
83
84
85
86
87
88
89
def __init__(self, dt_const, u, V, target_cfl=1.0, increase_tolerance=1.5, maximum_timestep=None):
    self.dt_const = dt_const
    self.u = u
    self.target_cfl = target_cfl
    self.increase_tolerance = increase_tolerance
    self.maximum_timestep = maximum_timestep
    self.mesh = V.mesh()

    # J^-1 u is a discontinuous expression, using op3.MAX_RW it takes the maximum value
    # in all adjacent elements when interpolating it to a continuous function space
    # We do need to ensure we reset ref_vel to zero, as it also takes the max with any previous values
    self.ref_vel_interpolate = interpolate(abs(dot(JacobianInverse(self.mesh), self.u)), V, access=op3.MAX_RW)

CombinedSurfaceMeasure(domain, degree)

Bases: Measure

A surface measure that combines ds_v, the integral over vertical boundary facets, and ds_t and ds_b, the integral over horizontal top and bottom facets. The vertical boundary facets are identified with the same surface ids as ds_v. The top and bottom surfaces are identified via the "top" and "bottom" ids.

Source code in g-adopt/gadopt/utility.py
198
199
200
201
def __init__(self, domain, degree):
    self.ds_v = ds_v(domain=domain, degree=degree)
    self.ds_t = ds_t(domain=domain, degree=degree)
    self.ds_b = ds_b(domain=domain, degree=degree)

__rmul__(other)

This is to handle terms to be integrated over all surfaces in the form of other*ds. Here the CombinedSurfaceMeasure ds is not called, instead we just split it up as below.

Source code in g-adopt/gadopt/utility.py
211
212
213
214
def __rmul__(self, other):
    """This is to handle terms to be integrated over all surfaces in the form of other*ds.
    Here the CombinedSurfaceMeasure ds is not called, instead we just split it up as below."""
    return other*self.ds_v + other*self.ds_t + other*self.ds_b

ExtrudedFunction(*args, mesh_3d=None, **kwargs)

Bases: Function

A 2D Function that provides a 3D view on the extruded domain.

The 3D function can be accessed as ExtrudedFunction.view_3d. The 3D function resides in V x R function space, where V is the function space of the source function. The 3D function shares the data of the 2D function.

Parameters:

Name Type Description Default
mesh_3d

Extruded 3D mesh where the function will be extended to.

None
Source code in g-adopt/gadopt/utility.py
350
351
352
353
354
def __init__(self, *args, mesh_3d=None, **kwargs):
    # create the 2d function
    super().__init__(*args, **kwargs)
    if mesh_3d is not None:
        self.view_3d = extend_function_to_3d(self, mesh_3d)

LayerAveraging(mesh, r1d=None, quad_degree=None)

A manager for computing a vertical profile of horizontal layer averages.

Parameters:

Name Type Description Default
mesh

The mesh over which to compute averages

required
r1d

An array of either depth coordinates or radii, at which to compute layer averages. If not provided, and mesh is extruded, it uses the same layer heights. If mesh is not extruded, r1d is required.

None
Source code in g-adopt/gadopt/utility.py
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
def __init__(self, mesh, r1d=None, quad_degree=None):
    self.mesh = mesh
    XYZ = SpatialCoordinate(mesh)

    if is_cartesian(mesh):
        self.r = XYZ[len(XYZ)-1]
    else:
        self.r = sqrt(dot(XYZ, XYZ))

    self.dx = dx
    if quad_degree is not None:
        self.dx = dx(degree=quad_degree)

    if r1d is not None:
        self.r1d = r1d
    else:
        try:
            nlayers = mesh.layers
        except AttributeError:
            raise ValueError("For non-extruded mesh need to specify depths array r1d.")
        CG1 = FunctionSpace(mesh, "CG", 1)
        r_func = Function(CG1).interpolate(self.r)
        self.r1d = r_func.dat.data[:nlayers]

    self.mass = np.zeros((2, len(self.r1d)))
    self.rhs = np.zeros(len(self.r1d))
    self._assemble_mass()

get_layer_average(T)

Compute the layer averages of Function T at the predefined depths.

Returns:

Type Description

A numpy array containing the averages.

Source code in g-adopt/gadopt/utility.py
488
489
490
491
492
493
494
495
496
def get_layer_average(self, T):
    """Compute the layer averages of `Function` T at the predefined depths.

    Returns:
      A numpy array containing the averages.
    """

    self._assemble_rhs(T)
    return solveh_banded(self.mass, self.rhs, lower=True)

extrapolate_layer_average(u, avg)

Given an array of layer averages avg, extrapolate to Function u

Source code in g-adopt/gadopt/utility.py
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
def extrapolate_layer_average(self, u, avg):
    """Given an array of layer averages avg, extrapolate to `Function` u
    """

    r = self.r
    rc = Constant(self.r1d[0])
    rn = Constant(self.r1d[1])
    rp = Constant(0.)

    u.assign(0.0)

    phi = max_value(min_value((r - rp) / (rc - rp), (rn - r) / (rn - rc)), 0)
    val = Constant(0.)

    for a, rin in zip(avg[:-1], self.r1d[1:]):
        val.assign(a)
        rn.assign(rin)
        # reconstruct this layer according to the basis function
        u.interpolate(u + val * phi)

        rp.assign(rc)
        rc.assign(rn)

    phi = max_value(min_value(1, (r - rp) / (rn - rp)), 0)
    val.assign(avg[-1])
    u.interpolate(u + val * phi)

InteriorBC

Bases: DirichletBC

DirichletBC applied to anywhere that is not on the specified boundary

log(*args, level=None)

Log output to stdout from root processor only.

If level is provided, the message is only printed when that level is enabled by the GADOPT_LOGLEVEL environment variable.

Source code in g-adopt/gadopt/utility.py
38
39
40
41
42
43
44
45
46
def log(*args, level=None):
    """Log output to stdout from root processor only.

    If level is provided, the message is only printed when that level is
    enabled by the GADOPT_LOGLEVEL environment variable.
    """
    if level is not None and level < log_level:
        return
    PETSc.Sys.Print(*args)

horizontal_components(u)

Returns expression for vector u with vertical component removed, i.e. returns the vector UFL expression orthogonal to gravity.

Source code in g-adopt/gadopt/utility.py
137
138
139
140
def horizontal_components(u):
    """Returns expression for vector u with vertical component removed, i.e. returns the vector UFL expression orthogonal to gravity."""
    mesh = extract_unique_domain(u)
    return u - vertical_component(u) * upward_normal(mesh)

get_device_type(gpu_options)

Determine the available GPU device type.

Checks for a GPU device in the following order of priority: 1. The GADOPT_DEVICE_OVERRIDE environment variable. 2. The device_type key in the gpu_options dictionary. 3. Firedrake's get_device_type() function.

Returns:

Type Description
str | None

The detected device type (e.g., CUDA, HIP). Returns None if the HOST

str | None

device is detected (i.e. no GPU) or if the device set in gpu_options or

str | None

returned by Firedrake's get_device_type() is None.

Raises:

Type Description
ValueError

If GADOPT_DEVICE_OVERRIDE or gpu_options["device_type"]

Source code in g-adopt/gadopt/utility.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
def get_device_type(gpu_options: dict | None) -> str | None:
    """Determine the available GPU device type.

    Checks for a GPU device in the following order of priority:
    1. The `GADOPT_DEVICE_OVERRIDE` environment variable.
    2. The `device_type` key in the `gpu_options` dictionary.
    3. Firedrake's `get_device_type()` function.

    Returns:
        The detected device type (e.g., `CUDA`, `HIP`). Returns `None` if the `HOST`
        device is detected (i.e. no GPU) or if the device set in `gpu_options` or
        returned by Firedrake's `get_device_type()` is `None`.

    Raises:
        ValueError: If `GADOPT_DEVICE_OVERRIDE` or `gpu_options["device_type"]`
        specifies a device type not in `valid_devices`.
    """
    if "GADOPT_DEVICE_OVERRIDE" in os.environ:
        device_type = os.environ["GADOPT_DEVICE_OVERRIDE"]
        if device_type not in valid_devices:
            raise ValueError(
                f"Invalid device specified in GADOPT_DEVICE_OVERRIDE. Valid values are {','.join(valid_devices)}"
            )
    elif gpu_options is not None and "device_type" in gpu_options:
        device_type = gpu_options["device_type"]
        if device_type is not None and device_type not in valid_devices:
            raise ValueError(
                f"Invalid device specified in 'device_type' key. Valid values are {','.join(valid_devices)} or None"
            )
    else:
        # Set the PETSc error handler so that Firedrake can correctly intercept the
        # exception from PETSc if a GPU-enabled build calls this on a non-GPU enabled
        # system - add this to Firedrake. This function returns None for a GPU-enabled
        # build or HOST on a non-GPU enabled build when GPUs are not available.
        PETSc.Sys.pushErrorHandler("python")
        device_type = fd_get_device_type()
        PETSc.Sys.popErrorHandler()
    if device_type == "HOST":
        return None
    return device_type

depends_on(ufl_expr, terminal)

Does ufl_expr depend on terminal (Function/Constant/...)?

Source code in g-adopt/gadopt/utility.py
241
242
243
def depends_on(ufl_expr, terminal):
    """Does ufl_expr depend on terminal (Function/Constant/...)?"""
    return terminal in traverse_unique_terminals(ufl_expr)

tensor_jump(v, n)

Jump term for vector functions based on the tensor product

\["jump"(bb u, bb n) = (bb u^+ bb n^+) + (bb u^- bb n^-)\]

This is the discrete equivalent of grad(u) as opposed to the vectorial UFL jump operator ufl.jump which represents div(u). The equivalent of nabla_grad(u) is given by tensor_jump(n, u).

Source code in g-adopt/gadopt/utility.py
264
265
266
267
268
269
270
271
272
273
274
def tensor_jump(v, n):
    r"""
    Jump term for vector functions based on the tensor product

    $$"jump"(bb u, bb n) = (bb u^+ bb n^+) + (bb u^- bb n^-)$$

    This is the discrete equivalent of grad(u) as opposed to the
    vectorial UFL jump operator `ufl.jump` which represents div(u).
    The equivalent of nabla_grad(u) is given by tensor_jump(n, u).
    """
    return outer(v('+'), n('+')) + outer(v('-'), n('-'))

extend_function_to_3d(func, mesh_extruded)

Returns a view of a Function on the base mesh as a Function on the extruded mesh.

The extended function lives in the V x R space, where V is the space of func. Its horizontal element is the element of func, and it shares its data with func: a change to one shows in the other. The source must be a scalar or vector continuous (H1) function on the base mesh of mesh_extruded. Mixed spaces, their subspaces and restricted spaces are not supported.

Parameters:

Name Type Description Default
func

Source Function on the base mesh.

required
mesh_extruded

Mesh extruded from the mesh of func.

required

Returns:

Type Description

The extended Function. Its source attribute is func.

Raises:

Type Description
ValueError

If func does not satisfy the conditions above.

Source code in g-adopt/gadopt/utility.py
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
def extend_function_to_3d(func, mesh_extruded):
    """Returns a view of a `Function` on the base mesh as a `Function` on the extruded mesh.

    The extended function lives in the V x R space, where V is the space of
    `func`. Its horizontal element is the element of `func`, and it shares its
    data with `func`: a change to one shows in the other. The source must be a
    scalar or vector continuous (H1) function on the base mesh of `mesh_extruded`.
    Mixed spaces, their subspaces and restricted spaces are not supported.

    Arguments:
      func: Source `Function` on the base mesh.
      mesh_extruded: Mesh extruded from the mesh of `func`.

    Returns:
      The extended `Function`. Its `source` attribute is `func`.

    Raises:
      ValueError: If `func` does not satisfy the conditions above.
    """
    fs = func.function_space()
    ufl_elem = fs.ufl_element()
    is_vector = isinstance(ufl_elem, VectorElement)
    scalar_elem = ufl_elem.sub_elements[0] if is_vector else ufl_elem

    # These conditions depend only on the function space, so they are the same on every rank.
    # A subspace of a mixed space shares the buffer of the whole mixed function, and a
    # restricted space numbers its nodes differently from the unrestricted V x R space.
    if len(fs) != 1 or fs.parent is not None or fs.boundary_set:
        raise ValueError("extend_function_to_3d: the source must not be a mixed, sub or restricted space")
    if fs.mesh().topology is not mesh_extruded.topology._base_mesh:
        raise ValueError("extend_function_to_3d: the source mesh must be the base mesh of mesh_extruded")
    # The V x R space numbers its nodes in the same order as V only for continuous elements.
    if ufl_elem.sobolev_space != ufl.H1:
        raise ValueError(f"extend_function_to_3d: unsupported element {ufl_elem}; use a continuous element")

    # Use the source element itself in the horizontal, so that both spaces have the same dofs.
    r0 = FiniteElement("R", mesh_extruded.ufl_cell().sub_cells[1], 0)
    elem = TensorProductElement(scalar_elem, r0)
    if is_vector:
        fs_extended = VectorFunctionSpace(mesh_extruded, elem, dim=ufl_elem.num_sub_elements)
    else:
        fs_extended = FunctionSpace(mesh_extruded, elem)

    # The buffer of the source must have the size and the halo exchange pattern (star forest) of
    # the extended space. Both can differ between ranks, so all ranks agree before any raises.
    # The size test comes first, and np.array_equal is used, because StarForest.__eq__ fails on
    # arrays of different lengths.
    sf, sf_extended = func.dat.buffer.sf, fs_extended.axes.unindexed.sf
    ok = func.dat.buffer.size == fs_extended.axes.unindexed.local_max_size and type(sf) is type(sf_extended)
    if ok and isinstance(sf, op3.sf.StarForest) and sf is not sf_extended:
        ok = np.array_equal(sf.ilocal, sf_extended.ilocal) and np.array_equal(sf.iremote, sf_extended.iremote)
    if not mesh_extruded.comm.allreduce(ok, op=MPI.LAND):
        raise ValueError("extend_function_to_3d: the source and extended spaces have different data layouts")

    # A view with the axes of the extended space on the buffer of the source.
    func_extended = Function(fs_extended, name=func.name(), val=func.dat.with_axes(fs_extended.axes))
    func_extended.source = func
    return func_extended

absv(u)

Component-wise absolute value of vector for SU stabilisation

Source code in g-adopt/gadopt/utility.py
547
548
549
def absv(u):
    """Component-wise absolute value of vector for SU stabilisation"""
    return as_vector([abs(ui) for ui in u])

node_coordinates(function)

Interpolates mesh coordinates at each node of the provided function.

Parameters:

Name Type Description Default
function Function

A Firedrake function

required

Returns:

Type Description
Function

A Firedrake function for the interpolated mesh coordinates

Source code in g-adopt/gadopt/utility.py
561
562
563
564
565
566
567
568
569
570
571
572
def node_coordinates(function: Function) -> Function:
    """Interpolates mesh coordinates at each node of the provided function.

    Args:
      function: A Firedrake function

    Returns:
      A Firedrake function for the interpolated mesh coordinates
    """
    vec_space = VectorFunctionSpace(function.ufl_domain(), function.ufl_element())

    return Function(vec_space).interpolate(SpatialCoordinate(function))

interpolate_1d_profile(function, one_d_filename)

Assign a one-dimensional profile to a Function function from a file.

The function reads a one-dimensional profile (e.g., viscosity) together with the radius/height input for the profile, from a file, broadcasts the two arrays to all processes, and then interpolates this array onto the function space of function.

Parameters:

Name Type Description Default
function Function

The function onto which the 1D profile will be assigned

required
one_d_filename str

The path to the file containing the 1D radial profile

required
Note
  • This is designed to read a file with one process and distribute in parallel with MPI.
  • The input file should contain an array of radius/height and an array of values, separated by a comma.
Source code in g-adopt/gadopt/utility.py
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
def interpolate_1d_profile(function: Function, one_d_filename: str):
    """
    Assign a one-dimensional profile to a Function `function` from a file.

    The function reads a one-dimensional profile (e.g., viscosity) together with the
    radius/height input for the profile, from a file, broadcasts the two arrays to all
    processes, and then interpolates this array onto the function space of `function`.

    Args:
        function: The function onto which the 1D profile will be assigned
        one_d_filename: The path to the file containing the 1D radial profile

    Note:
        - This is designed to read a file with one process and distribute in parallel with MPI.
        - The input file should contain an array of radius/height and an array of values, separated by a comma.
    """
    mesh = extract_unique_domain(function)

    if mesh.comm.rank == 0:
        rshl, one_d_data = np.loadtxt(one_d_filename, unpack=True, delimiter=",")
        if rshl[1] < rshl[0]:
            rshl = rshl[::-1]
            one_d_data = one_d_data[::-1]
        if not np.all(np.diff(rshl) > 0):
            raise ValueError("Data must be strictly monotonous.")
    else:
        one_d_data = None
        rshl = None

    one_d_data = mesh.comm.bcast(one_d_data, root=0)
    rshl = mesh.comm.bcast(rshl, root=0)

    X = SpatialCoordinate(mesh)

    upward_coord = vertical_component(X)

    rad = Function(function.function_space()).interpolate(upward_coord)

    averager = LayerAveraging(mesh, rshl if mesh.layers is None else None)
    interpolated_visc = np.interp(averager.get_layer_average(rad), rshl, one_d_data)
    averager.extrapolate_layer_average(function, interpolated_visc)

extruded_layer_heights(DG0_layers, radii)

Calculates layer heights for extruded mesh using rheological boundary

Parameters:

Name Type Description Default
DG0_layers int | list[int]

Number of layers per rheological layer

required
radii list[float]

Radii of rheological boundaries to match when extruding the mesh. Assumes sequence starts with outer radii -> inner radii.

required

Returns:

Type Description
list[float]

list of layer heights

Source code in g-adopt/gadopt/utility.py
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
def extruded_layer_heights(
        DG0_layers: int | list[int], radii: list[float]) -> list[float]:

    """Calculates layer heights for extruded mesh using rheological boundary

    Args:
      DG0_layers:
        Number of layers per rheological layer
      radii:
        Radii of rheological boundaries to match when extruding the mesh.
        Assumes sequence starts with outer radii -> inner radii.

    Returns:
        list of layer heights
        """

    layer_heights = []

    nz_layers = [DG0_layers] * len(radii) if isinstance(DG0_layers, int) else DG0_layers
    assert len(nz_layers) == len(radii)

    # starting at the bottom radius, work outwards
    for i in range(len(radii) - 2, -1, -1):
        dz = (radii[i] - radii[i + 1]) / nz_layers[i]
        for _ in range(nz_layers[i]):
            layer_heights.append(dz)
    return layer_heights

initialise_background_field(f, background_values, X, radii, shift=0.0)

Initialises discontinuous field with sharp jumps at rheological boundaries

Parameters:

Name Type Description Default
f Function

Function to interpolate background values into. N.b. f is modified in place.

required
background_values list[float]

Discrete values to interpolate into f

required
X SpatialCoordinate

Spatial coordinates associated with function f

required
radii list[float]

Radii of rheological discontinuities. N.b. length of background_values must be one less than length of radii.

required
shift float

Shift vertical radii by a constant, e.g. when mesh is offset from radii values.

0.0
Source code in g-adopt/gadopt/utility.py
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
def initialise_background_field(
        f: Function,
        background_values: list[float],
        X: ufl.geometry.SpatialCoordinate,
        radii: list[float],
        shift: float = 0.0,):
    """Initialises discontinuous field with sharp jumps at rheological boundaries

    Args:
      f:
        Function to interpolate background values into. N.b. `f` is modified in place.
      background_values:
        Discrete values to interpolate into `f`
      X:
        Spatial coordinates associated with function `f`
      radii:
        Radii of rheological discontinuities. N.b. length of `background_values` must be one less
        than length of `radii`.
      shift:
        Shift vertical radii by a constant, e.g. when mesh is offset from `radii` values.
    """

    assert len(background_values) == len(radii)-1
    for i in range(len(background_values)):
        f.interpolate(
            conditional(vertical_component(X) + shift > radii[i+1],
                        conditional(vertical_component(X) + shift <= radii[i],
                                    background_values[i], f), f))