-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatlon.py
59 lines (52 loc) · 2.4 KB
/
latlon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from firedrake import *
import numpy as np
def get_latlon_mesh(mesh):
coords_orig = mesh.coordinates
coords_fs = coords_orig.function_space()
if coords_fs.extruded:
cell = mesh._base_mesh.ufl_cell().cellname()
DG1_hori_elt = FiniteElement("DG", cell, 1, variant="equispaced")
DG1_vert_elt = FiniteElement("DG", interval, 1, variant="equispaced")
DG1_elt = TensorProductElement(DG1_hori_elt, DG1_vert_elt)
else:
cell = mesh.ufl_cell().cellname()
DG1_elt = FiniteElement("DG", cell, 1, variant="equispaced")
vec_DG1 = VectorFunctionSpace(mesh, DG1_elt)
coords_dg = Function(vec_DG1).interpolate(coords_orig)
coords_latlon = Function(vec_DG1)
shapes = {"nDOFs": vec_DG1.finat_element.space_dimension(), 'dim': 3}
radius = np.min(np.sqrt(coords_dg.dat.data[:, 0]**2 + coords_dg.dat.data[:, 1]**2 + coords_dg.dat.data[:, 2]**2))
# lat-lon 'x' = atan2(y, x)
coords_latlon.dat.data[:, 0] = np.arctan2(coords_dg.dat.data[:, 1], coords_dg.dat.data[:, 0])
# lat-lon 'y' = asin(z/sqrt(x^2 + y^2 + z^2))
coords_latlon.dat.data[:, 1] = np.arcsin(coords_dg.dat.data[:, 2]/np.sqrt(coords_dg.dat.data[:, 0]**2 + coords_dg.dat.data[:, 1]**2 + coords_dg.dat.data[:, 2]**2))
# our vertical coordinate is radius - the minimum radius
coords_latlon.dat.data[:, 2] = np.sqrt(coords_dg.dat.data[:, 0]**2 + coords_dg.dat.data[:, 1]**2 + coords_dg.dat.data[:, 2]**2) - radius
# We need to ensure that all points in a cell are on the same side of the branch cut in longitude coords
# This kernel amends the longitude coords so that all longitudes in one cell are close together
kernel = op2.Kernel("""
#define PI 3.141592653589793
#define TWO_PI 6.283185307179586
void splat_coords(double *coords) {{
double max_diff = 0.0;
double diff = 0.0;
for (int i=0; i<{nDOFs}; i++) {{
for (int j=0; j<{nDOFs}; j++) {{
diff = coords[i*{dim}] - coords[j*{dim}];
if (fabs(diff) > max_diff) {{
max_diff = diff;
}}
}}
}}
if (max_diff > PI) {{
for (int i=0; i<{nDOFs}; i++) {{
if (coords[i*{dim}] < 0) {{
coords[i*{dim}] += TWO_PI;
}}
}}
}}
}}
""".format(**shapes), "splat_coords")
op2.par_loop(kernel, coords_latlon.cell_set,
coords_latlon.dat(op2.RW, coords_latlon.cell_node_map()))
return Mesh(coords_latlon)