|
1 | 1 | import pytest |
2 | 2 | import numpy as np |
3 | 3 | from math import pi, isclose |
4 | | -from numbacs.utils import (unravel_index, ravel_index, curl_vel, curl_func_tspan, |
5 | | - composite_simpsons, composite_simpsons_38_irregular, dist_2d, |
6 | | - dist_tol, shoelace, max_in_radius, gen_circ, gen_filled_circ, |
7 | | - gen_filled_circ_radius, arclength, arclength_along_arc, |
8 | | - interp_curve, wn_pt_in_poly, pts_in_poly, pts_in_poly_mask, |
9 | | - cart_prod) |
| 4 | +from numbacs.utils import ( |
| 5 | + gradF_stencil_2D, |
| 6 | + gradF_aux_stencil_2D, |
| 7 | + gradF_main_stencil_2D, |
| 8 | + gradUV_stencil_2D, |
| 9 | + eigvalsh_max_2D, |
| 10 | + inv_2D, |
| 11 | + vec_dot_2D, |
| 12 | + vec_dot_3D, |
| 13 | + unravel_index, |
| 14 | + ravel_index, |
| 15 | + curl_vel, |
| 16 | + curl_func_tspan, |
| 17 | + composite_simpsons, |
| 18 | + composite_simpsons_38_irregular, |
| 19 | + dist_2d, |
| 20 | + dist_tol, |
| 21 | + shoelace, |
| 22 | + max_in_radius, |
| 23 | + gen_circ, |
| 24 | + gen_filled_circ, |
| 25 | + gen_filled_circ_radius, |
| 26 | + arclength, |
| 27 | + arclength_along_arc, |
| 28 | + interp_curve, |
| 29 | + wn_pt_in_poly, |
| 30 | + pts_in_poly, |
| 31 | + pts_in_poly_mask, |
| 32 | + cart_prod, |
| 33 | + lonlat2xyz, |
| 34 | + local_basis_S2 |
| 35 | +) |
10 | 36 |
|
11 | 37 |
|
12 | 38 | @pytest.fixture |
@@ -37,6 +63,118 @@ def polygon_tpoints(): |
37 | 63 |
|
38 | 64 | return pts |
39 | 65 |
|
| 66 | +@pytest.fixture |
| 67 | +def lonlat_coords(): |
| 68 | + |
| 69 | + lon = np.linspace(-180.0, 180.0, 5) |
| 70 | + lat = np.linspace(-90, 90, 3) |
| 71 | + |
| 72 | + return np.meshgrid(lon, lat, indexing='ij') |
| 73 | + |
| 74 | +def test_gradF_stencil_2D(coords_dg, fm_data): |
| 75 | + |
| 76 | + x, y = coords_dg |
| 77 | + dx = x[1] |
| 78 | + dy = y[1] |
| 79 | + |
| 80 | + gradF_expected = ( |
| 81 | + -5.976700186729431, |
| 82 | + -5.0307804346084595, |
| 83 | + 0.331292524933815, |
| 84 | + 1.0545916110277176 |
| 85 | + ) |
| 86 | + gradF = gradF_stencil_2D(fm_data, 5, 5, dx, dy) |
| 87 | + |
| 88 | + assert np.allclose(gradF, gradF_expected) |
| 89 | + |
| 90 | + |
| 91 | +def test_gradF_aux_stencil_2D(coords_dg, fm_aux_data): |
| 92 | + |
| 93 | + gradF_aux_expected = ( |
| 94 | + -7.270276546478271, |
| 95 | + -9.016692638397217, |
| 96 | + 0.5433335900306702, |
| 97 | + 0.5362555384635925 |
| 98 | + ) |
| 99 | + gradF_aux = gradF_aux_stencil_2D(fm_aux_data, 5, 5, 1e-5) |
| 100 | + |
| 101 | + assert np.allclose(gradF_aux, gradF_aux_expected) |
| 102 | + |
| 103 | + |
| 104 | +def test_gradF_main_stencil_2D(coords_dg, fm_aux_data): |
| 105 | + |
| 106 | + x, y = coords_dg |
| 107 | + dx = x[1] |
| 108 | + dy = y[1] |
| 109 | + |
| 110 | + gradF_main_expected = ( |
| 111 | + -5.976700186729431, |
| 112 | + -5.0307804346084595, |
| 113 | + 0.331292524933815, |
| 114 | + 1.0545916110277176 |
| 115 | + ) |
| 116 | + gradF_main = gradF_main_stencil_2D(fm_aux_data, 5, 5, dx, dy) |
| 117 | + |
| 118 | + assert np.allclose(gradF_main, gradF_main_expected) |
| 119 | + |
| 120 | + |
| 121 | +def test_gradUV_stencil_2D(coords_dg, vel_data): |
| 122 | + |
| 123 | + x, y = coords_dg |
| 124 | + dx = x[1] |
| 125 | + dy = y[1] |
| 126 | + |
| 127 | + gradUV_expected = (0.0, 0.9708055193627335, -0.9708055193627335, 0.0) |
| 128 | + u, v = vel_data |
| 129 | + gradUV = gradUV_stencil_2D(u, v, 5, 5, dx, dy) |
| 130 | + |
| 131 | + assert np.allclose(gradUV, gradUV_expected) |
| 132 | + |
| 133 | +def test_eigvalsh_max_2D(): |
| 134 | + |
| 135 | + B = np.array([[1.3, -2.4], [0.7, 5.2]]) |
| 136 | + A = B.T @ B |
| 137 | + |
| 138 | + eigval_expected = 32.8088282841737 |
| 139 | + eigval = eigvalsh_max_2D(A) |
| 140 | + |
| 141 | + assert np.allclose(eigval, eigval_expected) |
| 142 | + |
| 143 | +def test_inv_2D(): |
| 144 | + |
| 145 | + B = np.array([[1.3, -2.4], [0.7, 5.2]]) |
| 146 | + B_singular = B.copy() |
| 147 | + B_singular[:, 0] = 0.0 |
| 148 | + |
| 149 | + Binv_expected = np.array([[0.61611374, 0.28436019], [-0.08293839, 0.15402844]]) |
| 150 | + Bsinv_expected = np.array([[0.0, 0.0], [0.0, 0.0]]) |
| 151 | + |
| 152 | + Binv = inv_2D(B) |
| 153 | + Bsinv = inv_2D(B_singular) |
| 154 | + |
| 155 | + assert np.allclose(Binv, Binv_expected) and np.allclose(Bsinv, Bsinv_expected) |
| 156 | + |
| 157 | +def test_vec_dot_2D(): |
| 158 | + |
| 159 | + v1 = np.array([1.3, -2.4]) |
| 160 | + v2 = np.array([0.7, 5.2]) |
| 161 | + |
| 162 | + dot_expected = -11.57 |
| 163 | + dot = vec_dot_2D(v1, v2) |
| 164 | + |
| 165 | + assert np.allclose(dot, dot_expected) |
| 166 | + |
| 167 | + |
| 168 | +def test_vec_dot_3D(): |
| 169 | + |
| 170 | + v1 = np.array([1.3, -2.4, 3.2]) |
| 171 | + v2 = np.array([0.7, 5.2, -2.1]) |
| 172 | + |
| 173 | + dot_expected = -18.29 |
| 174 | + dot = vec_dot_3D(v1, v2) |
| 175 | + |
| 176 | + assert np.allclose(dot, dot_expected) |
| 177 | + |
40 | 178 |
|
41 | 179 | def test_unravel_index(): |
42 | 180 |
|
@@ -334,3 +472,85 @@ def test_cart_prod(): |
334 | 472 | cprod = cart_prod((t,x,y)) |
335 | 473 |
|
336 | 474 | assert np.all(cprod == cprod_expected) |
| 475 | + |
| 476 | + |
| 477 | +def test_lonlat2xyz(lonlat_coords): |
| 478 | + |
| 479 | + Lon, Lat = lonlat_coords |
| 480 | + |
| 481 | + xyz_points_expected = np.array([ |
| 482 | + [[-1.22464680e-16, -1.49975978e-32, -2.00000000e+00], |
| 483 | + [-2.00000000e+00, -2.44929360e-16, 0.00000000e+00], |
| 484 | + [-1.22464680e-16, -1.49975978e-32, 2.00000000e+00]], |
| 485 | + |
| 486 | + [[ 7.49879891e-33, -1.22464680e-16, -2.00000000e+00], |
| 487 | + [ 1.22464680e-16, -2.00000000e+00, 0.00000000e+00], |
| 488 | + [ 7.49879891e-33, -1.22464680e-16, 2.00000000e+00]], |
| 489 | + |
| 490 | + [[ 1.22464680e-16, 0.00000000e+00, -2.00000000e+00], |
| 491 | + [ 2.00000000e+00, 0.00000000e+00, 0.00000000e+00], |
| 492 | + [ 1.22464680e-16, 0.00000000e+00, 2.00000000e+00]], |
| 493 | + |
| 494 | + [[ 7.49879891e-33, 1.22464680e-16, -2.00000000e+00], |
| 495 | + [ 1.22464680e-16, 2.00000000e+00, 0.00000000e+00], |
| 496 | + [ 7.49879891e-33, 1.22464680e-16, 2.00000000e+00]], |
| 497 | + |
| 498 | + [[-1.22464680e-16, 1.49975978e-32, -2.00000000e+00], |
| 499 | + [-2.00000000e+00, 2.44929360e-16, 0.00000000e+00], |
| 500 | + [-1.22464680e-16, 1.49975978e-32, 2.00000000e+00]] |
| 501 | + ] |
| 502 | + ) |
| 503 | + |
| 504 | + xyz_points = lonlat2xyz(Lon, Lat, 2.0, deg2rad=True, return_array=True) |
| 505 | + |
| 506 | + assert np.allclose(xyz_points, xyz_points_expected) |
| 507 | + |
| 508 | + |
| 509 | +def test_local_basis_S2(lonlat_coords): |
| 510 | + |
| 511 | + Lon, Lat = lonlat_coords |
| 512 | + |
| 513 | + basis_expected = ( |
| 514 | + np.array([[[ 1.2246468e-16, -1.0000000e+00, 0.0000000e+00], |
| 515 | + [ 1.2246468e-16, -1.0000000e+00, 0.0000000e+00], |
| 516 | + [ 1.2246468e-16, -1.0000000e+00, 0.0000000e+00]], |
| 517 | + |
| 518 | + [[ 1.0000000e+00, 6.1232340e-17, 0.0000000e+00], |
| 519 | + [ 1.0000000e+00, 6.1232340e-17, 0.0000000e+00], |
| 520 | + [ 1.0000000e+00, 6.1232340e-17, 0.0000000e+00]], |
| 521 | + |
| 522 | + [[-0.0000000e+00, 1.0000000e+00, 0.0000000e+00], |
| 523 | + [-0.0000000e+00, 1.0000000e+00, 0.0000000e+00], |
| 524 | + [-0.0000000e+00, 1.0000000e+00, 0.0000000e+00]], |
| 525 | + |
| 526 | + [[-1.0000000e+00, 6.1232340e-17, 0.0000000e+00], |
| 527 | + [-1.0000000e+00, 6.1232340e-17, 0.0000000e+00], |
| 528 | + [-1.0000000e+00, 6.1232340e-17, 0.0000000e+00]], |
| 529 | + |
| 530 | + [[-1.2246468e-16, -1.0000000e+00, 0.0000000e+00], |
| 531 | + [-1.2246468e-16, -1.0000000e+00, 0.0000000e+00], |
| 532 | + [-1.2246468e-16, -1.0000000e+00, 0.0000000e+00]]]), |
| 533 | + np.array([[[-1.0000000e+00, -1.2246468e-16, 6.1232340e-17], |
| 534 | + [ 0.0000000e+00, 0.0000000e+00, 1.0000000e+00], |
| 535 | + [ 1.0000000e+00, 1.2246468e-16, 6.1232340e-17]], |
| 536 | + |
| 537 | + [[ 6.1232340e-17, -1.0000000e+00, 6.1232340e-17], |
| 538 | + [-0.0000000e+00, 0.0000000e+00, 1.0000000e+00], |
| 539 | + [-6.1232340e-17, 1.0000000e+00, 6.1232340e-17]], |
| 540 | + |
| 541 | + [[ 1.0000000e+00, 0.0000000e+00, 6.1232340e-17], |
| 542 | + [-0.0000000e+00, -0.0000000e+00, 1.0000000e+00], |
| 543 | + [-1.0000000e+00, -0.0000000e+00, 6.1232340e-17]], |
| 544 | + |
| 545 | + [[ 6.1232340e-17, 1.0000000e+00, 6.1232340e-17], |
| 546 | + [-0.0000000e+00, -0.0000000e+00, 1.0000000e+00], |
| 547 | + [-6.1232340e-17, -1.0000000e+00, 6.1232340e-17]], |
| 548 | + |
| 549 | + [[-1.0000000e+00, 1.2246468e-16, 6.1232340e-17], |
| 550 | + [ 0.0000000e+00, -0.0000000e+00, 1.0000000e+00], |
| 551 | + [ 1.0000000e+00, -1.2246468e-16, 6.1232340e-17]]]) |
| 552 | + ) |
| 553 | + |
| 554 | + basis = local_basis_S2(Lon, Lat, deg2rad=True) |
| 555 | + |
| 556 | + assert np.allclose(basis, basis_expected) |
0 commit comments