|
| 1 | +# An idealized baroclinic instability test case |
| 2 | +# |
| 3 | +# Note that this libelixir is based on the original baroclinic instability elixir by |
| 4 | +# Erik Faulhaber for Trixi.jl |
| 5 | +# Source: https://github.com/trixi-framework/Trixi.jl/blob/main/examples/p4est_3d_dgsem/elixir_euler_baroclinic_instability.jl |
| 6 | +# |
| 7 | +# References: |
| 8 | +# - Paul A. Ullrich, Thomas Melvin, Christiane Jablonowski, Andrew Staniforth (2013) |
| 9 | +# A proposed baroclinic wave test case for deep- and shallow-atmosphere dynamical cores |
| 10 | +# https://doi.org/10.1002/qj.2241 |
| 11 | + |
| 12 | +using OrdinaryDiffEq |
| 13 | +using Trixi |
| 14 | +using LinearAlgebra |
| 15 | +using LibTrixi |
| 16 | + |
| 17 | + |
| 18 | +# Initial condition for an idealized baroclinic instability test |
| 19 | +# https://doi.org/10.1002/qj.2241, Section 3.2 and Appendix A |
| 20 | +function initial_condition_baroclinic_instability(x, t, |
| 21 | + equations::CompressibleEulerEquations3D) |
| 22 | + lon, lat, r = cartesian_to_sphere(x) |
| 23 | + radius_earth = 6.371229e6 |
| 24 | + # Make sure that the r is not smaller than radius_earth |
| 25 | + z = max(r - radius_earth, 0.0) |
| 26 | + |
| 27 | + # Unperturbed basic state |
| 28 | + rho, u, p = basic_state_baroclinic_instability_longitudinal_velocity(lon, lat, z) |
| 29 | + |
| 30 | + # Stream function type perturbation |
| 31 | + u_perturbation, v_perturbation = perturbation_stream_function(lon, lat, z) |
| 32 | + |
| 33 | + u += u_perturbation |
| 34 | + v = v_perturbation |
| 35 | + |
| 36 | + # Convert spherical velocity to Cartesian |
| 37 | + v1 = -sin(lon) * u - sin(lat) * cos(lon) * v |
| 38 | + v2 = cos(lon) * u - sin(lat) * sin(lon) * v |
| 39 | + v3 = cos(lat) * v |
| 40 | + |
| 41 | + return prim2cons(SVector(rho, v1, v2, v3, p), equations) |
| 42 | +end |
| 43 | + |
| 44 | +function cartesian_to_sphere(x) |
| 45 | + r = norm(x) |
| 46 | + lambda = atan(x[2], x[1]) |
| 47 | + if lambda < 0 |
| 48 | + lambda += 2 * pi |
| 49 | + end |
| 50 | + phi = asin(x[3] / r) |
| 51 | + |
| 52 | + return lambda, phi, r |
| 53 | +end |
| 54 | + |
| 55 | +# Unperturbed balanced steady-state. |
| 56 | +# Returns primitive variables with only the velocity in longitudinal direction (rho, u, p). |
| 57 | +# The other velocity components are zero. |
| 58 | +function basic_state_baroclinic_instability_longitudinal_velocity(lon, lat, z) |
| 59 | + # Parameters from Table 1 in the paper |
| 60 | + # Corresponding names in the paper are commented |
| 61 | + radius_earth = 6.371229e6 # a |
| 62 | + half_width_parameter = 2 # b |
| 63 | + gravitational_acceleration = 9.80616 # g |
| 64 | + k = 3 # k |
| 65 | + surface_pressure = 1e5 # p₀ |
| 66 | + gas_constant = 287 # R |
| 67 | + surface_equatorial_temperature = 310.0 # T₀ᴱ |
| 68 | + surface_polar_temperature = 240.0 # T₀ᴾ |
| 69 | + lapse_rate = 0.005 # Γ |
| 70 | + angular_velocity = 7.29212e-5 # Ω |
| 71 | + |
| 72 | + # Distance to the center of the Earth |
| 73 | + r = z + radius_earth |
| 74 | + |
| 75 | + # In the paper: T₀ |
| 76 | + temperature0 = 0.5 * (surface_equatorial_temperature + surface_polar_temperature) |
| 77 | + # In the paper: A, B, C, H |
| 78 | + const_a = 1 / lapse_rate |
| 79 | + const_b = (temperature0 - surface_polar_temperature) / |
| 80 | + (temperature0 * surface_polar_temperature) |
| 81 | + const_c = 0.5 * (k + 2) * (surface_equatorial_temperature - surface_polar_temperature) / |
| 82 | + (surface_equatorial_temperature * surface_polar_temperature) |
| 83 | + const_h = gas_constant * temperature0 / gravitational_acceleration |
| 84 | + |
| 85 | + # In the paper: (r - a) / bH |
| 86 | + scaled_z = z / (half_width_parameter * const_h) |
| 87 | + |
| 88 | + # Temporary variables |
| 89 | + temp1 = exp(lapse_rate / temperature0 * z) |
| 90 | + temp2 = exp(-scaled_z^2) |
| 91 | + |
| 92 | + # In the paper: ̃τ₁, ̃τ₂ |
| 93 | + tau1 = const_a * lapse_rate / temperature0 * temp1 + |
| 94 | + const_b * (1 - 2 * scaled_z^2) * temp2 |
| 95 | + tau2 = const_c * (1 - 2 * scaled_z^2) * temp2 |
| 96 | + |
| 97 | + # In the paper: ∫τ₁(r') dr', ∫τ₂(r') dr' |
| 98 | + inttau1 = const_a * (temp1 - 1) + const_b * z * temp2 |
| 99 | + inttau2 = const_c * z * temp2 |
| 100 | + |
| 101 | + # Temporary variables |
| 102 | + temp3 = r / radius_earth * cos(lat) |
| 103 | + temp4 = temp3^k - k / (k + 2) * temp3^(k + 2) |
| 104 | + |
| 105 | + # In the paper: T |
| 106 | + temperature = 1 / ((r / radius_earth)^2 * (tau1 - tau2 * temp4)) |
| 107 | + |
| 108 | + # In the paper: U, u (zonal wind, first component of spherical velocity) |
| 109 | + big_u = gravitational_acceleration / radius_earth * k * temperature * inttau2 * |
| 110 | + (temp3^(k - 1) - temp3^(k + 1)) |
| 111 | + temp5 = radius_earth * cos(lat) |
| 112 | + u = -angular_velocity * temp5 + sqrt(angular_velocity^2 * temp5^2 + temp5 * big_u) |
| 113 | + |
| 114 | + # Hydrostatic pressure |
| 115 | + p = surface_pressure * |
| 116 | + exp(-gravitational_acceleration / gas_constant * (inttau1 - inttau2 * temp4)) |
| 117 | + |
| 118 | + # Density (via ideal gas law) |
| 119 | + rho = p / (gas_constant * temperature) |
| 120 | + |
| 121 | + return rho, u, p |
| 122 | +end |
| 123 | + |
| 124 | +# Perturbation as in Equations 25 and 26 of the paper (analytical derivative) |
| 125 | +function perturbation_stream_function(lon, lat, z) |
| 126 | + # Parameters from Table 1 in the paper |
| 127 | + # Corresponding names in the paper are commented |
| 128 | + perturbation_radius = 1 / 6 # d₀ / a |
| 129 | + perturbed_wind_amplitude = 1.0 # Vₚ |
| 130 | + perturbation_lon = pi / 9 # Longitude of perturbation location |
| 131 | + perturbation_lat = 2 * pi / 9 # Latitude of perturbation location |
| 132 | + pertz = 15000 # Perturbation height cap |
| 133 | + |
| 134 | + # Great circle distance (d in the paper) divided by a (radius of the Earth) |
| 135 | + # because we never actually need d without dividing by a |
| 136 | + great_circle_distance_by_a = acos(sin(perturbation_lat) * sin(lat) + |
| 137 | + cos(perturbation_lat) * cos(lat) * |
| 138 | + cos(lon - perturbation_lon)) |
| 139 | + |
| 140 | + # In the first case, the vertical taper function is per definition zero. |
| 141 | + # In the second case, the stream function is per definition zero. |
| 142 | + if z > pertz || great_circle_distance_by_a > perturbation_radius |
| 143 | + return 0.0, 0.0 |
| 144 | + end |
| 145 | + |
| 146 | + # Vertical tapering of stream function |
| 147 | + perttaper = 1.0 - 3 * z^2 / pertz^2 + 2 * z^3 / pertz^3 |
| 148 | + |
| 149 | + # sin/cos(pi * d / (2 * d_0)) in the paper |
| 150 | + sin_, cos_ = sincos(0.5 * pi * great_circle_distance_by_a / perturbation_radius) |
| 151 | + |
| 152 | + # Common factor for both u and v |
| 153 | + factor = 16 / (3 * sqrt(3)) * perturbed_wind_amplitude * perttaper * cos_^3 * sin_ |
| 154 | + |
| 155 | + u_perturbation = -factor * (-sin(perturbation_lat) * cos(lat) + |
| 156 | + cos(perturbation_lat) * sin(lat) * cos(lon - perturbation_lon)) / |
| 157 | + sin(great_circle_distance_by_a) |
| 158 | + |
| 159 | + v_perturbation = factor * cos(perturbation_lat) * sin(lon - perturbation_lon) / |
| 160 | + sin(great_circle_distance_by_a) |
| 161 | + |
| 162 | + return u_perturbation, v_perturbation |
| 163 | +end |
| 164 | + |
| 165 | +@inline function source_terms_baroclinic_instability(u, x, t, |
| 166 | + equations::CompressibleEulerEquations3D) |
| 167 | + radius_earth = 6.371229e6 # a |
| 168 | + gravitational_acceleration = 9.80616 # g |
| 169 | + angular_velocity = 7.29212e-5 # Ω |
| 170 | + |
| 171 | + r = norm(x) |
| 172 | + # Make sure that r is not smaller than radius_earth |
| 173 | + z = max(r - radius_earth, 0.0) |
| 174 | + r = z + radius_earth |
| 175 | + |
| 176 | + du1 = zero(eltype(u)) |
| 177 | + |
| 178 | + # Gravity term |
| 179 | + temp = -gravitational_acceleration * radius_earth^2 / r^3 |
| 180 | + du2 = temp * u[1] * x[1] |
| 181 | + du3 = temp * u[1] * x[2] |
| 182 | + du4 = temp * u[1] * x[3] |
| 183 | + du5 = temp * (u[2] * x[1] + u[3] * x[2] + u[4] * x[3]) |
| 184 | + |
| 185 | + # Coriolis term, -2Ω × ρv = -2 * angular_velocity * (0, 0, 1) × u[2:4] |
| 186 | + du2 -= -2 * angular_velocity * u[3] |
| 187 | + du3 -= 2 * angular_velocity * u[2] |
| 188 | + |
| 189 | + return SVector(du1, du2, du3, du4, du5) |
| 190 | +end |
| 191 | + |
| 192 | + |
| 193 | +# The function to create the simulation state needs to be named `init_simstate` |
| 194 | +function init_simstate() |
| 195 | + |
| 196 | + ############################################################################### |
| 197 | + # Setup for the baroclinic instability test |
| 198 | + gamma = 1.4 |
| 199 | + equations = CompressibleEulerEquations3D(gamma) |
| 200 | + |
| 201 | + ############################################################################### |
| 202 | + # semidiscretization of the problem |
| 203 | + |
| 204 | + initial_condition = initial_condition_baroclinic_instability |
| 205 | + |
| 206 | + boundary_conditions = Dict(:inside => boundary_condition_slip_wall, |
| 207 | + :outside => boundary_condition_slip_wall) |
| 208 | + |
| 209 | + # This is a good estimate for the speed of sound in this example. |
| 210 | + # Other values between 300 and 400 should work as well. |
| 211 | + surface_flux = FluxLMARS(340) |
| 212 | + volume_flux = flux_kennedy_gruber |
| 213 | + solver = DGSEM(polydeg = 5, surface_flux = surface_flux, |
| 214 | + volume_integral = VolumeIntegralFluxDifferencing(volume_flux)) |
| 215 | + |
| 216 | + trees_per_cube_face = (16, 8) |
| 217 | + mesh = Trixi.P4estMeshCubedSphere(trees_per_cube_face..., 6.371229e6, 30000.0, |
| 218 | + polydeg = 5, initial_refinement_level = 0) |
| 219 | + |
| 220 | + semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver, |
| 221 | + source_terms = source_terms_baroclinic_instability, |
| 222 | + boundary_conditions = boundary_conditions) |
| 223 | + |
| 224 | + ############################################################################### |
| 225 | + # ODE solvers, callbacks etc. |
| 226 | + |
| 227 | + tspan = (0.0, 12 * 24 * 60 * 60.0) # time in seconds for 12 days# |
| 228 | + |
| 229 | + ode = semidiscretize(semi, tspan) |
| 230 | + |
| 231 | + summary_callback = SummaryCallback() |
| 232 | + |
| 233 | + analysis_interval = 5000 |
| 234 | + analysis_callback = AnalysisCallback(semi, interval = analysis_interval) |
| 235 | + |
| 236 | + alive_callback = AliveCallback(analysis_interval = analysis_interval) |
| 237 | + |
| 238 | + save_solution = SaveSolutionCallback(interval = 5000, |
| 239 | + save_initial_solution = true, |
| 240 | + save_final_solution = true, |
| 241 | + solution_variables = cons2prim, |
| 242 | + output_directory = "out_baroclinic",) |
| 243 | + |
| 244 | + callbacks = CallbackSet(summary_callback, |
| 245 | + analysis_callback, |
| 246 | + alive_callback, |
| 247 | + save_solution) |
| 248 | + |
| 249 | + |
| 250 | + ############################################################################### |
| 251 | + # create the time integrator |
| 252 | + |
| 253 | + # OrdinaryDiffEq's `integrator` |
| 254 | + # Use a Runge-Kutta method with automatic (error based) time step size control |
| 255 | + integrator = init(ode, RDPK3SpFSAL49(); |
| 256 | + abstol = 1.0e-6, reltol = 1.0e-6, |
| 257 | + ode_default_options()..., |
| 258 | + callback = callbacks, |
| 259 | + maxiters=5e5); |
| 260 | + |
| 261 | + ############################################################################### |
| 262 | + # Create simulation state |
| 263 | + |
| 264 | + simstate = SimulationState(semi, integrator) |
| 265 | + |
| 266 | + return simstate |
| 267 | +end |
0 commit comments