def test_topography(): """ Regression test that checks whether the mean topography is still the same as at time of creation, also generates a vtk file which shows topography. :return: """ # Generate Grid rad = 6371.0 fib_grid = FibonacciGrid() radii = np.array([rad]) resolution = np.ones_like(radii) * 500 fib_grid.set_global_sphere(radii, resolution) grid_data = GridData(*fib_grid.get_coordinates()) top = Topography() top.read() # 1000 topo = top.eval(grid_data.df['c'], grid_data.df['l'], topo_smooth_factor=0.) x, y, z = grid_data.get_coordinates().T elements = triangulate(x, y, z) coords = np.array((x, y, z)).T # Test if topography does not change mean_topo_test = np.array([-2.44594261115]) mean_topo = np.mean(topo) np.testing.assert_almost_equal(mean_topo, mean_topo_test, decimal=DECIMAL_CLOSE) write_vtk(os.path.join(VTK_DIR, 'topography.vtk'), coords, elements, topo, 'topography')
def test_topo(): """ Test to ensure that a vtk of the topography is written succesfully. :return: """ topo = Topography() topo.read() x, y, z = skl.fibonacci_sphere(300) c, l, _ = cart2sph(x, y, z) vals = topo.eval(c, l) elements = triangulate(x, y, z) pts = np.array((x, y, z)).T write_vtk(os.path.join(VTK_DIR, 'topo.vtk'), pts, elements, vals, 'topo') north_pole = np.array([-4.228]) south_pole = np.array([-0.056]) random_point = np.array([0.097533]) np.testing.assert_almost_equal(topo.eval(0, 0), north_pole, decimal=DECIMAL_CLOSE) np.testing.assert_almost_equal(topo.eval(np.pi, 0), south_pole, decimal=DECIMAL_CLOSE) np.testing.assert_almost_equal(topo.eval(np.radians(90 - 53.833333), np.radians(76.500000)), random_point, decimal=DECIMAL_CLOSE)
def test_fibonacci_grid(): """ Test to check whether Fibonacci_grid works :return: """ fib_grid = FibonacciGrid() # Set global background grid radii = np.linspace(6371.0, 0.0, 5) resolution = np.ones_like(radii) * (6371.0 / 5) fib_grid.set_global_sphere(radii, resolution) # refinement region coarse europe c_min = np.radians(50) c_max = np.radians(120) l_min = np.radians(-30) l_max = np.radians(60) radii_regional = np.linspace(6371.0, 5571.0, 10) resolution_regional = np.ones_like(radii_regional) * (6371.0 / 20) fib_grid.add_refinement_region(c_min, c_max, l_min, l_max, radii_regional, resolution_regional, angle=np.radians(-57.5), rot_y=1) # refinement region fine Japan c_min = np.radians(30) c_max = np.radians(75) l_min = np.radians(115) l_max = np.radians(160) radii_regional = np.linspace(6371.0, 5571.0, 4) resolution_regional = np.ones_like(radii_regional) * 200 fib_grid.add_refinement_region(c_min, c_max, l_min, l_max, radii_regional, resolution_regional) x, y, z = fib_grid.get_coordinates() x, y, z = fib_grid.get_coordinates(is_normalised=True) r, c, l = fib_grid.get_coordinates(type='spherical') r, c, l = fib_grid.get_coordinates(type='spherical', is_normalised=True) vals = np.ones_like(x) elements = triangulate(x, y, z) pts = np.array((x, y, z)).T np.testing.assert_allclose(np.sum(x**2 + y**2 + z**2), 5469.0929941834, rtol=.02, atol=0) write_vtk(os.path.join(VTK_DIR, 'test_fib_grid.vtk'), pts, elements, vals, 'ones')
def test_crust(): """ Regression test that checks whether the mean crustal depths and velocity is still the same as at time of creation, also generates vtk files :return: """ # Generate Grid rad = 6351.0 fib_grid = FibonacciGrid() radii = np.array(np.linspace(rad, rad, 1)) resolution = np.ones_like(radii) * 500 fib_grid.set_global_sphere(radii, resolution) grid_data = GridData(*fib_grid.get_coordinates()) grid_data.add_one_d() crust = Crust() crust_dep = crust.interpolate(grid_data.df['c'], grid_data.df['l'], param='crust_dep', smooth_fac=crust.crust_dep_smooth_fac) crust_vs = crust.interpolate(grid_data.df['c'], grid_data.df['l'], param='crust_vs', smooth_fac=crust.crust_dep_smooth_fac) #Test if mean crustal depth and velocity remain the same mean_crust_dep_test = np.array([18.9934922621]) mean_crust_vs_test = np.array([3.42334914127]) mean_crust_dep = np.mean(crust_dep) mean_crust_vs = np.mean(crust_vs) np.testing.assert_almost_equal(mean_crust_dep, mean_crust_dep_test, decimal=DECIMAL_CLOSE) np.testing.assert_almost_equal(mean_crust_vs, mean_crust_vs_test, decimal=DECIMAL_CLOSE) # Write vtk's x, y, z = grid_data.get_coordinates().T elements = triangulate(x, y, z) coords = np.array((x, y, z)).T write_vtk(os.path.join(VTK_DIR, 'crust_dep.vtk'), coords, elements, crust_dep, 'crust_dep') write_vtk(os.path.join(VTK_DIR, 'crust_vs.vtk'), coords, elements, crust_vs, 'crust_vs')
def test_s20rts_vtk_single_sphere(): """ Test to ensure that a vtk of a single sphere of s20rts is written succesfully. :return: """ s20mod = s20.S20rts() rad = s20mod.layers[0] rel_rad = rad / s20mod.r_earth x, y, z = skl.fibonacci_sphere(500) c, l, r = cart2sph(x, y, z) vals = s20mod.eval(c, l, r) elements = triangulate(x, y, z) pts = np.array((x, y, z)).T * rel_rad write_vtk(os.path.join(VTK_DIR, 'test_s20rts.vtk'), pts, elements, vals, 'vs')
""" Read a list of Cartesian grid points, evaluate the CSEM, compute a Delauney triangulation, and turn into a vtk file. """ import os import numpy as np from csemlib.csem.evaluate_csem import evaluate_csem from csemlib.io.readers import read_from_grid from csemlib.models.model import write_vtk, triangulate depths = [100] for depth in depths: # Read some grid points. --------------------------------------------------- x, y, z = read_from_grid('../../grids/OUTPUT/fib_'+str(depth)+'.dat') grid_data = evaluate_csem(x,y,z) # Generate output. --------------------------------------------------------- # Make vtk file. elements = triangulate(x, y, z) points = np.array((x, y, z)).T filename = os.path.join('./', str(depth)+'.vtk') write_vtk(filename, points, elements, grid_data.df['vsv'])