|
1 | 1 | """Split timestepping methods for generically solving terms separately."""
|
2 | 2 |
|
3 | 3 | from firedrake import Projector
|
4 |
| -from firedrake.fml import Label |
| 4 | +from firedrake.fml import Label, drop |
5 | 5 | from pyop2.profiling import timed_stage
|
| 6 | +from gusto.core import TimeLevelFields, StateFields |
6 | 7 | from gusto.core.labels import time_derivative, physics_label
|
7 | 8 | from gusto.time_discretisation.time_discretisation import ExplicitTimeDiscretisation
|
8 |
| -from gusto.timestepping.timestepper import Timestepper |
| 9 | +from gusto.timestepping.timestepper import BaseTimestepper, Timestepper |
| 10 | +from numpy import ones |
9 | 11 |
|
10 |
| -__all__ = ["SplitPhysicsTimestepper", "SplitPrescribedTransport"] |
| 12 | +__all__ = ["SplitTimestepper", "SplitPhysicsTimestepper", "SplitPrescribedTransport"] |
| 13 | + |
| 14 | + |
| 15 | +class SplitTimestepper(BaseTimestepper): |
| 16 | + """ |
| 17 | + Implements a timeloop by applying separate schemes to different terms, e.g, physics |
| 18 | + and individual dynamics components in a user-defined order. This allows a different |
| 19 | + time discretisation to be applied to each defined component. Different terms can be |
| 20 | + substepped by specifying weights; this enables Strang-Splitting to be applied. |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__(self, equation, term_splitting, dynamics_schemes, io, |
| 24 | + weights=None, spatial_methods=None, physics_schemes=None): |
| 25 | + """ |
| 26 | + Args: |
| 27 | + equation (:class:`PrognosticEquation`): the prognostic equation |
| 28 | + term_splitting (list): a list of labels specifying the terms that should |
| 29 | + be solved separately and the order to do so. |
| 30 | + dynamics_schemes: (:class:`TimeDiscretisation`) A list of time |
| 31 | + discretisations for the dynamics schemes. A scheme must be |
| 32 | + provided for each non-physics label that is provided in the |
| 33 | + term_splitting list. |
| 34 | + io (:class:`IO`): the model's object for controlling input/output. |
| 35 | + weights (array, optional): An array of weights for substepping |
| 36 | + of any dynamics or physics scheme. The sum of weights for |
| 37 | + each distinct label in term_splitting must be 1. |
| 38 | + spatial_methods (iter,optional): a list of objects describing the |
| 39 | + methods to use for discretising transport or diffusion terms |
| 40 | + for each transported/diffused variable. Defaults to None, |
| 41 | + in which case the terms follow the original discretisation in |
| 42 | + the equation. |
| 43 | + physics_schemes: (list, optional): a list of tuples of the form |
| 44 | + (:class:`PhysicsParametrisation`, :class:`TimeDiscretisation`), |
| 45 | + pairing physics parametrisations and timestepping schemes to use |
| 46 | + for each. Timestepping schemes for physics must be explicit. |
| 47 | + Defaults to None. |
| 48 | + """ |
| 49 | + |
| 50 | + if spatial_methods is not None: |
| 51 | + self.spatial_methods = spatial_methods |
| 52 | + else: |
| 53 | + self.spatial_methods = [] |
| 54 | + |
| 55 | + # If we have physics schemes, extract these. |
| 56 | + if 'physics' in term_splitting: |
| 57 | + if physics_schemes is None: |
| 58 | + raise ValueError('Physics schemes need to be specified when applying ' |
| 59 | + + 'a physics splitting in the SplitTimestepper') |
| 60 | + else: |
| 61 | + # Check that the weights are correct for physics: |
| 62 | + count = 0 |
| 63 | + if weights is not None: |
| 64 | + for idx, term in enumerate(term_splitting): |
| 65 | + if term == 'physics': |
| 66 | + count += weights[idx] |
| 67 | + if count != 1: |
| 68 | + raise ValueError('Incorrect weights are specified for the ' |
| 69 | + + 'physics schemes in the split timestepper.') |
| 70 | + self.physics_schemes = physics_schemes |
| 71 | + else: |
| 72 | + self.physics_schemes = [] |
| 73 | + |
| 74 | + for parametrisation, phys_scheme in self.physics_schemes: |
| 75 | + # Check that the supplied schemes for physics are valid |
| 76 | + if hasattr(parametrisation, "explicit_only") and parametrisation.explicit_only: |
| 77 | + assert isinstance(phys_scheme, ExplicitTimeDiscretisation), \ |
| 78 | + ("Only explicit time discretisations can be used with " |
| 79 | + + f"physics scheme {parametrisation.label.label}") |
| 80 | + |
| 81 | + self.term_splitting = term_splitting |
| 82 | + self.dynamics_schemes = dynamics_schemes |
| 83 | + |
| 84 | + if weights is not None: |
| 85 | + self.weights = weights |
| 86 | + else: |
| 87 | + self.weights = ones(len(self.term_splitting)) |
| 88 | + |
| 89 | + # Check that each dynamics label in term_splitting has a corresponding |
| 90 | + # dynamics scheme |
| 91 | + for term in self.term_splitting: |
| 92 | + if term != 'physics': |
| 93 | + assert term in self.dynamics_schemes, \ |
| 94 | + f'The {term} terms do not have a specified scheme in the split timestepper' |
| 95 | + |
| 96 | + # Multilevel schemes are currently not supported for the dynamics terms. |
| 97 | + for label, scheme in self.dynamics_schemes.items(): |
| 98 | + assert scheme.nlevels == 1, \ |
| 99 | + "Multilevel schemes are not currently implemented in the split timestepper" |
| 100 | + |
| 101 | + # As we handle physics in separate parametrisations, these are not |
| 102 | + # passed to the super __init__ |
| 103 | + super().__init__(equation, io) |
| 104 | + |
| 105 | + # Check that each dynamics term is specified by a label |
| 106 | + # in the term_splitting list, but also that there are not |
| 107 | + # multiple labels, i.e. there is a single specified time discretisation. |
| 108 | + # When using weights, these should add to 1 for each term. |
| 109 | + terms = self.equation.residual.label_map( |
| 110 | + lambda t: any(t.has_label(time_derivative, physics_label)), map_if_true=drop |
| 111 | + ) |
| 112 | + for term in terms: |
| 113 | + count = 0 |
| 114 | + for idx, label in enumerate(self.term_splitting): |
| 115 | + if term.has_label(Label(label)): |
| 116 | + count += self.weights[idx] |
| 117 | + if count != 1: |
| 118 | + raise ValueError('The term_splitting list does not correctly cover ' |
| 119 | + + 'the dynamics terms in the equation(s).') |
| 120 | + |
| 121 | + # Timesteps for each scheme in the term_splitting list |
| 122 | + self.split_dts = [self.equation.domain.dt*weight for weight in self.weights] |
| 123 | + |
| 124 | + @property |
| 125 | + def transporting_velocity(self): |
| 126 | + return self.fields('u') |
| 127 | + |
| 128 | + def setup_fields(self): |
| 129 | + self.x = TimeLevelFields(self.equation, 1) |
| 130 | + self.fields = StateFields(self.x, self.equation.prescribed_fields, |
| 131 | + *self.io.output.dumplist) |
| 132 | + |
| 133 | + def setup_scheme(self): |
| 134 | + """Sets up transport, diffusion and physics schemes""" |
| 135 | + # TODO: apply_bcs should be False for advection but this means |
| 136 | + # tests with KGOs fail |
| 137 | + self.setup_equation(self.equation) |
| 138 | + |
| 139 | + apply_bcs = True |
| 140 | + for label, scheme in self.dynamics_schemes.items(): |
| 141 | + scheme.setup(self.equation, apply_bcs, Label(label)) |
| 142 | + self.setup_transporting_velocity(scheme) |
| 143 | + if self.io.output.log_courant and label == 'transport': |
| 144 | + scheme.courant_max = self.io.courant_max |
| 145 | + |
| 146 | + apply_bcs = False |
| 147 | + for parametrisation, scheme in self.physics_schemes: |
| 148 | + scheme.setup(self.equation, apply_bcs, parametrisation.label) |
| 149 | + |
| 150 | + def timestep(self): |
| 151 | + |
| 152 | + for idx, term in enumerate(self.term_splitting): |
| 153 | + split_dt = self.split_dts[idx] |
| 154 | + if term == 'physics': |
| 155 | + with timed_stage("Physics"): |
| 156 | + for _, scheme in self.physics_schemes: |
| 157 | + scheme.dt = split_dt |
| 158 | + scheme.apply(self.x.np1(scheme.field_name), self.x.np1(scheme.field_name)) |
| 159 | + else: |
| 160 | + scheme = self.dynamics_schemes[term] |
| 161 | + scheme.dt = split_dt |
| 162 | + scheme.apply(self.x.np1(scheme.field_name), self.x.np1(scheme.field_name)) |
11 | 163 |
|
12 | 164 |
|
13 | 165 | class SplitPhysicsTimestepper(Timestepper):
|
|
0 commit comments