def test_model_obj(): """ Test the object behavior for the `Model` object Test the instantiation and attribute data for the `Model` object of the `single_bubble_model` module. Notes ----- This test function only tests instantiation from a netCDF file of ambient CTD data and does not test any of the object methods. Instantiation from simulation data and testing of the object methods is done in the remaining test functions. See Also -------- test_simulation """ # Get the ambient profile data profile = get_profile() # Initialize a Model object sbm = single_bubble_model.Model(profile) # Check the model attributes assert_approx_equal(sbm.p.rho_r, 1031.035855535142, significant=6) (T, S, P) = profile.get_values(1000., ['temperature', 'salinity', 'pressure']) (Tp, Sp, Pp) = sbm.profile.get_values(1000., ['temperature', 'salinity', 'pressure']) assert Tp == T assert Sp == S assert Pp == P
from tamoc import dbm from tamoc import seawater from tamoc import single_bubble_model from tamoc import dispersed_phases import numpy as np if __name__ == '__main__': # Open an ambient profile object from the netCDF dataset nc = '../../test/output/test_bm54.nc' bm54 = ambient.Profile(nc, chem_names='all') bm54.close_nc() # Initialize a single_bubble_model.Model object with this data sbm = single_bubble_model.Model(bm54) # Create a light gas bubble to track composition = ['methane', 'ethane', 'propane', 'oxygen'] bub = dbm.FluidParticle(composition, fp_type=0.) # Set the mole fractions of each component at release. mol_frac = np.array([0.95, 0.03, 0.02, 0.]) # Specify the remaining particle initial conditions de = 0.005 z0 = 1000. T0 = 273.15 + 30. fdis = 1.e-15 # Also, use the hydrate model from Jun et al. (2015) to set the
def test_simulation(): """ Test the output from the `Model.simulate` method Test the output of the `Model.simulate` method of the `single_bubble_model` module. These tests include a single component bubble, multi-component bubbles and drops, and multi-component fluid particles with gas stripping from the ambient dissolved chemicals. """ # Get the ambient profile data profile = get_profile() # Initialize a Model object sbm = single_bubble_model.Model(profile) # Set up the initial conditions composition = ['methane', 'ethane', 'propane', 'oxygen'] bub = dbm.FluidParticle(composition) mol_frac = np.array([0.90, 0.07, 0.03, 0.0]) de = 0.005 z0 = 1000. T0 = 273.15 + 30. # Run the simulation sbm.simulate(bub, np.array([0., 0., z0]), de, mol_frac, T0, K_T=1, fdis=1e-8, delta_t=10.) # Check the solution assert sbm.y.shape[0] == 1117 assert sbm.y.shape[1] == 8 assert sbm.t.shape[0] == 1117 assert_approx_equal(sbm.t[-1], 11016.038751523512, significant=6) assert_array_almost_equal(sbm.y[-1, :], np.array([ 0.00000000e+00, 0.00000000e+00, 3.36934635e+02, 4.31711152e-14, 6.66318106e-15, -2.91389824e-13, -1.08618680e-15, -1.37972400e-07 ]), decimal=6) # Write the output files sbm.save_sim('./output/sbm_data.nc', './test_BM54.nc', 'Results of ./test_sbm.py script') sbm.save_txt('./output/sbm_data', './test_BM54.nc', 'Results of ./test_sbm.py script') # Reload the simulation sbm_f = single_bubble_model.Model(simfile='./output/sbm_data.nc') # Check that the attributes are loaded correctly assert sbm_f.y[0, 0] == sbm.y[0, 0] # x0 assert sbm_f.y[0, 1] == sbm.y[0, 1] # y0 assert sbm_f.y[0, 2] == sbm.y[0, 2] # z0 assert_array_almost_equal(sbm_f.particle.m0, sbm.particle.m0, decimal=6) assert sbm_f.particle.T0 == sbm.particle.T0 print sbm_f.particle.K_T, sbm.particle.K_T assert sbm_f.particle.K == sbm.particle.K assert sbm_f.particle.K_T == sbm.particle.K_T assert sbm_f.particle.fdis == sbm.particle.fdis assert sbm_f.K_T0 == sbm.K_T0 assert sbm_f.delta_t == sbm.delta_t (T, S, P) = profile.get_values(1000., ['temperature', 'salinity', 'pressure']) (Tp, Sp, Pp) = sbm_f.profile.get_values(1000., ['temperature', 'salinity', 'pressure']) assert Tp == T assert Sp == S assert Pp == P # Check that the results are still correct assert sbm.y.shape[0] == 1117 assert sbm.y.shape[1] == 8 assert sbm.t.shape[0] == 1117 assert_approx_equal(sbm.t[-1], 11016.038751523512, significant=6) assert_array_almost_equal(sbm.y[-1, :], np.array([ 0.00000000e+00, 0.00000000e+00, 3.36934635e+02, 4.31711152e-14, 6.66318106e-15, -2.91389824e-13, -1.08618680e-15, -1.37972400e-07 ]), decimal=6) # Load the data in the txt file and check the solution data = np.loadtxt('./output/sbm_data.txt') assert sbm.y.shape[0] == 1117 assert sbm.y.shape[1] == 8 assert sbm.t.shape[0] == 1117 assert_approx_equal(sbm.t[-1], 11016.038751523512, significant=6) assert_array_almost_equal(sbm.y[-1, :], np.array([ 0.00000000e+00, 0.00000000e+00, 3.36934635e+02, 4.31711152e-14, 6.66318106e-15, -2.91389824e-13, -1.08618680e-15, -1.37972400e-07 ]), decimal=6) # Create an inert particle that is compressible oil = dbm.InsolubleParticle(True, True, rho_p=840.) mol_frac = np.array([1.]) # Specify the remaining particle initial conditions de = 0.03 z0 = 1000. T0 = 273.15 + 30. # Simulate the trajectory through the water column and plot the results sbm.simulate(oil, np.array([0., 0., z0]), de, mol_frac, T0, K_T=1, delta_t=10.) ans = np.array([ 0.00000000e+00, 0.00000000e+00, 1.16067764e-01, 1.26136097e-02, 7.57374681e+03 ]) for i in range(5): assert_approx_equal(sbm.y[-1, i], ans[i], significant=6)
from tamoc import ambient from tamoc import dbm from tamoc import seawater from tamoc import single_bubble_model import numpy as np if __name__ == '__main__': # Create a single_bubble_model.Model object from the bubble.py results # stored on the hard drive. Note that the netCDF file is self-documenting # and actually points to the ambient CTD profile data. If that file # does not exist, the post_processing method cannot be applied as it # reads from the ambient data. sbm = single_bubble_model.Model(simfile='./bubble.nc') # Echo the results to the screen: print 'The results of ./bin/bubble.py have been loaded into memory' print ' len(t) : %d' % sbm.t.shape[0] print ' shape(y) : %d, %d' % (sbm.y.shape[0], sbm.y.shape[1]) print ' composition : %s, ' % sbm.particle.composition # You can re-run the simulation with different parameters by calling # the simulate method. print '\nRe-running simulation with de = 0.01 m:' z0 = sbm.y[0,2] yk = sbm.particle.particle.mol_frac(sbm.particle.m0) T0 = sbm.particle.T0 K = sbm.particle.K fdis = sbm.particle.fdis