From 0e80fa34bdf3d899a84d78e0e967846d1933c491 Mon Sep 17 00:00:00 2001 From: Caleb Bell Date: Wed, 4 Dec 2024 19:02:12 -0700 Subject: [PATCH] Few more scripts --- dev/check_fluids_can_be_called_from_R.R | 27 +++ dev/check_fluids_can_be_called_from_julia.jl | 201 +++++++++++++++++++ dev/check_fluids_can_be_called_from_octave.m | 125 ++++++++++++ 3 files changed, 353 insertions(+) create mode 100644 dev/check_fluids_can_be_called_from_julia.jl create mode 100644 dev/check_fluids_can_be_called_from_octave.m diff --git a/dev/check_fluids_can_be_called_from_R.R b/dev/check_fluids_can_be_called_from_R.R index 76c4aa4..d9a93f4 100644 --- a/dev/check_fluids_can_be_called_from_R.R +++ b/dev/check_fluids_can_be_called_from_R.R @@ -173,6 +173,32 @@ test_psd <- function() { stop("PSD test suite failed") }) } +benchmark_fluids <- function() { + fluids <- import("fluids") + cat("\nRunning benchmarks:\n") + + # Benchmark friction factor calculation + cat("\nBenchmarking friction_factor:\n") + t1 <- system.time({ + for(i in 1:10000) { + fluids$friction_factor(Re=1e5, eD=0.0001) + } + }) + cat(sprintf("Time for 10000 friction_factor calls: %.6f seconds\n", t1["elapsed"])) + cat(sprintf("Average time per call: %.6f seconds\n", t1["elapsed"]/10000)) + + # Benchmark tank creation + cat("\nBenchmarking TANK creation:\n") + t2 <- system.time({ + for(i in 1:1000) { + fluids$TANK(L=3, D=5, horizontal=FALSE, + sideA="torispherical", sideB="torispherical", + sideA_f=1, sideA_k=0.1, sideB_f=1, sideB_k=0.1) + } + }) + cat(sprintf("Time for 1000 TANK creations: %.6f seconds\n", t2["elapsed"])) + cat(sprintf("Average time per creation: %.6f seconds\n", t2["elapsed"]/1000)) +} # Run the tests test_fluids() @@ -180,3 +206,4 @@ test_atmosphere() test_tank() test_reynolds() test_psd() +benchmark_fluids() diff --git a/dev/check_fluids_can_be_called_from_julia.jl b/dev/check_fluids_can_be_called_from_julia.jl new file mode 100644 index 0000000..3ed963b --- /dev/null +++ b/dev/check_fluids_can_be_called_from_julia.jl @@ -0,0 +1,201 @@ +using PyCall +using Printf +# Import fluids +const fluids = pyimport("fluids") + +function test_fluids() + try + # 1. Test basic module import + println("✓ Successfully imported fluids") + println("✓ Fluids version: ", fluids.__version__) + + # 2. Test basic Reynolds number calculation + Re = fluids.Reynolds(V=2.5, D=0.1, rho=1000, mu=0.001) + println("✓ Reynolds number calculation successful: ", Re) + @assert Re > 0 + + # 3. Test friction factor calculation + fd = fluids.friction_factor(Re=1e5, eD=0.0001) + println("✓ Friction factor calculation successful: ", fd) + @assert 0 < fd < 1 + + println("\nAll basic tests completed successfully!") + + catch e + println("Error occurred: ", e) + rethrow(e) + end +end + +function test_atmosphere() + try + # Test ATMOSPHERE_1976 class + atm = fluids.ATMOSPHERE_1976(Z=5000) + + println("\nTesting atmosphere at 5000m elevation:") + println("✓ Temperature: ", round(atm.T, digits=4)) + println("✓ Pressure: ", round(atm.P, digits=4)) + println("✓ Density: ", round(atm.rho, digits=6)) + + # Test derived properties + println("✓ Gravity: ", round(atm.g, digits=6)) + println("✓ Viscosity: ", @sprintf("%.6e", atm.mu)) + println("✓ Thermal conductivity: ", round(atm.k, digits=6)) + println("✓ Sonic velocity: ", round(atm.v_sonic, digits=4)) + + # Test static methods + g_high = fluids.ATMOSPHERE_1976.gravity(Z=1E5) + println("✓ High altitude gravity: ", round(g_high, digits=6)) + + v_sonic = fluids.ATMOSPHERE_1976.sonic_velocity(T=300) + println("✓ Sonic velocity at 300K: ", round(v_sonic, digits=4)) + + mu_400 = fluids.ATMOSPHERE_1976.viscosity(T=400) + println("✓ Viscosity at 400K: ", @sprintf("%.6e", mu_400)) + + k_400 = fluids.ATMOSPHERE_1976.thermal_conductivity(T=400) + println("✓ Thermal conductivity at 400K: ", round(k_400, digits=6)) + + catch e + println("Error in atmosphere tests: ", e) + rethrow(e) + end +end + +function test_tank() + try + # Test basic tank creation + T1 = fluids.TANK(V=10, L_over_D=0.7, sideB="conical", horizontal=false) + println("\nTesting tank calculations:") + println("✓ Tank length: ", round(T1.L, digits=6)) + println("✓ Tank diameter: ", round(T1.D, digits=6)) + + # Test ellipsoidal tank + tank_ellip = fluids.TANK(D=10, V=500, horizontal=false, + sideA="ellipsoidal", sideB="ellipsoidal", + sideA_a=1, sideB_a=1) + println("✓ Ellipsoidal tank L: ", round(tank_ellip.L, digits=6)) + + # Test torispherical tank + DIN = fluids.TANK(L=3, D=5, horizontal=false, + sideA="torispherical", sideB="torispherical", + sideA_f=1, sideA_k=0.1, sideB_f=1, sideB_k=0.1) + + println("✓ Tank representation: ", string(DIN)) + println("✓ Tank max height: ", round(DIN.h_max, digits=6)) + println("✓ Height at V=40: ", round(DIN.h_from_V(40), digits=6)) + println("✓ Volume at h=4.1: ", round(DIN.V_from_h(4.1), digits=5)) + println("✓ Surface area at h=2.1: ", round(DIN.SA_from_h(2.1), digits=5)) + + catch e + println("Error in tank tests: ", e) + rethrow(e) + end +end + +function test_reynolds() + try + println("\nTesting Reynolds number calculations:") + + # Test with density and viscosity + Re1 = fluids.Reynolds(V=2.5, D=0.25, rho=1.1613, mu=1.9E-5) + println("✓ Re (with rho, mu): ", round(Re1, digits=4)) + @assert abs(Re1 - 38200.6579) < 0.1 + + # Test with kinematic viscosity + Re2 = fluids.Reynolds(V=2.5, D=0.25, nu=1.636e-05) + println("✓ Re (with nu): ", round(Re2, digits=4)) + @assert abs(Re2 - 38202.934) < 0.1 + + catch e + println("Error in Reynolds tests: ", e) + rethrow(e) + end +end + +function test_psd() + try + println("\nTesting particle size distribution functionality:") + + # Create a discrete PSD + ds = [240, 360, 450, 562.5, 703, 878, 1097, 1371, 1713, 2141, 2676, 3345, 4181, 5226, 6532] + numbers = [65, 119, 232, 410, 629, 849, 990, 981, 825, 579, 297, 111, 21, 1] + + psd = fluids.particle_size_distribution.ParticleSizeDistribution( + ds=ds, + fractions=numbers, + order=0 + ) + println("✓ Created discrete PSD") + + # Test mean sizes + d21 = psd.mean_size(2, 1) + println("✓ Size-weighted mean diameter: ", round(d21, digits=4)) + @assert abs(d21 - 1857.788) < 0.1 + + d10 = psd.mean_size(1, 0) + println("✓ Arithmetic mean diameter: ", round(d10, digits=4)) + @assert abs(d10 - 1459.372) < 0.1 + + # Test percentile calculations + d10_percentile = psd.dn(0.1) + d90_percentile = psd.dn(0.9) + println("✓ D10: ", round(d10_percentile, digits=4)) + println("✓ D90: ", round(d90_percentile, digits=4)) + + # Test probability functions + pdf_val = psd.pdf(1000) + cdf_val = psd.cdf(5000) + println("✓ PDF at 1000: ", @sprintf("%.4e", pdf_val)) + println("✓ CDF at 5000: ", round(cdf_val, digits=6)) + + # Test lognormal distribution + psd_log = fluids.particle_size_distribution.PSDLognormal(s=0.5, d_characteristic=5E-6) + println("✓ Created lognormal PSD") + + vssa = psd_log.vssa + println("✓ Volume specific surface area: ", round(vssa, digits=2)) + + span = psd_log.dn(0.9) - psd_log.dn(0.1) + println("✓ Span: ", @sprintf("%.4e", span)) + + ratio_7525 = psd_log.dn(0.75)/psd_log.dn(0.25) + println("✓ D75/D25 ratio: ", round(ratio_7525, digits=6)) + + catch e + println("Error in PSD tests: ", e) + rethrow(e) + end +end +function benchmark_fluids() + println("\nRunning benchmarks:") + + # Benchmark friction factor calculation + println("\nBenchmarking friction_factor:") + t1 = @elapsed for i in 1:1000000 + fluids.friction_factor(Re=1e5, eD=0.0001) + end + println("Time for 1e6 friction_factor calls: ", round(t1, digits=6), " seconds") + println("Average time per call: ", round(t1/1000000, digits=6), " seconds") + + # Benchmark tank creation + println("\nBenchmarking TANK creation:") + t2 = @elapsed for i in 1:1000 + fluids.TANK(L=3, D=5, horizontal=false, + sideA="torispherical", sideB="torispherical", + sideA_f=1, sideA_k=0.1, sideB_f=1, sideB_k=0.1) + end + println("Average time per creation: ", round(t2/1000, digits=6), " seconds") + +end + +# Add this line at the end of your script to run the benchmark +# Run all tests +println("Running fluids tests from Julia...") +test_fluids() +test_atmosphere() +test_tank() +test_reynolds() +test_psd() +benchmark_fluids() +println("\nAll tests completed!") diff --git a/dev/check_fluids_can_be_called_from_octave.m b/dev/check_fluids_can_be_called_from_octave.m new file mode 100644 index 0000000..01d13f4 --- /dev/null +++ b/dev/check_fluids_can_be_called_from_octave.m @@ -0,0 +1,125 @@ +% Test suite for fluids library in Octave +% Requires pythonic package or oct2py + +pkg load pythonic % Load Python interface + +function test_fluids() + try + % Test basic module import + fluids = py.importlib.import_module('fluids'); + printf('✓ Successfully imported fluids\n'); + printf('✓ Fluids version: %s\n', char(fluids.__version__)); + + % Test basic Reynolds number calculation + Re = double(fluids.Reynolds(pyargs('V', 2.5, 'D', 0.1, 'rho', 1000, 'mu', 0.001))); + printf('✓ Reynolds number calculation successful: %f\n', Re); + assert(Re > 0); + + % Test friction factor calculation + fd = double(fluids.friction_factor(pyargs('Re', 1e5, 'eD', 0.0001))); + printf('✓ Friction factor calculation successful: %f\n', fd); + assert(fd > 0 && fd < 1); + + printf('\nAll basic tests completed successfully!\n'); + + catch err + printf('Error occurred: %s\n', err.message); + rethrow(err); + end +end + +function test_atmosphere() + try + fluids = py.importlib.import_module('fluids'); + % Test ATMOSPHERE_1976 class + atm = fluids.ATMOSPHERE_1976(pyargs('Z', 5000)); + + printf('\nTesting atmosphere at 5000m elevation:\n'); + printf('✓ Temperature: %.4f\n', double(atm.T)); + printf('✓ Pressure: %.4f\n', double(atm.P)); + printf('✓ Density: %.6f\n', double(atm.rho)); + + % Test derived properties + printf('✓ Gravity: %.6f\n', double(atm.g)); + printf('✓ Viscosity: %.6e\n', double(atm.mu)); + printf('✓ Thermal conductivity: %.6f\n', double(atm.k)); + printf('✓ Sonic velocity: %.4f\n', double(atm.v_sonic)); + + % Test static methods + g_high = double(fluids.ATMOSPHERE_1976.gravity(pyargs('Z', 1E5))); + printf('✓ High altitude gravity: %.6f\n', g_high); + + v_sonic = double(fluids.ATMOSPHERE_1976.sonic_velocity(pyargs('T', 300))); + printf('✓ Sonic velocity at 300K: %.4f\n', v_sonic); + + catch err + printf('Error in atmosphere tests: %s\n', err.message); + rethrow(err); + end +end + +function test_tank() + try + fluids = py.importlib.import_module('fluids'); + % Test basic tank creation + T1 = fluids.TANK(pyargs('V', 10, 'L_over_D', 0.7, 'sideB', 'conical', 'horizontal', false)); + printf('\nTesting tank calculations:\n'); + printf('✓ Tank length: %.6f\n', double(T1.L)); + printf('✓ Tank diameter: %.6f\n', double(T1.D)); + + % Test ellipsoidal tank + tank_ellip = fluids.TANK(pyargs('D', 10, 'V', 500, 'horizontal', false, ... + 'sideA', 'ellipsoidal', 'sideB', 'ellipsoidal', ... + 'sideA_a', 1, 'sideB_a', 1)); + printf('✓ Ellipsoidal tank L: %.6f\n', double(tank_ellip.L)); + + % Test torispherical tank + DIN = fluids.TANK(pyargs('L', 3, 'D', 5, 'horizontal', false, ... + 'sideA', 'torispherical', 'sideB', 'torispherical', ... + 'sideA_f', 1, 'sideA_k', 0.1, 'sideB_f', 1, 'sideB_k', 0.1)); + + printf('✓ Tank max height: %.6f\n', double(DIN.h_max)); + printf('✓ Height at V=40: %.6f\n', double(DIN.h_from_V(40))); + printf('✓ Volume at h=4.1: %.5f\n', double(DIN.V_from_h(4.1))); + printf('✓ Surface area at h=2.1: %.5f\n', double(DIN.SA_from_h(2.1))); + + catch err + printf('Error in tank tests: %s\n', err.message); + rethrow(err); + end +end + +function benchmark_fluids() + fluids = py.importlib.import_module('fluids'); + printf('\nRunning benchmarks:\n'); + + % Benchmark friction factor calculation + printf('\nBenchmarking friction_factor:\n'); + tic; + for i = 1:1000 + fluids.friction_factor(pyargs('Re', 1e5, 'eD', 0.0001)); + end + t1 = toc; + printf('Time for 1000 friction_factor calls: %.6f seconds\n', t1); + printf('Average time per call: %.6f seconds\n', t1/1000); + + % Benchmark tank creation + printf('\nBenchmarking TANK creation:\n'); + tic; + for i = 1:1000 + fluids.TANK(pyargs('L', 3, 'D', 5, 'horizontal', false, ... + 'sideA', 'torispherical', 'sideB', 'torispherical', ... + 'sideA_f', 1, 'sideA_k', 0.1, 'sideB_f', 1, 'sideB_k', 0.1)); + end + t2 = toc; + printf('Time for 1000 TANK creations: %.6f seconds\n', t2); + printf('Average time per creation: %.6f seconds\n', t2/1000); +end + +% Run all tests +printf('Running fluids tests from Octave...\n'); +test_fluids(); +test_atmosphere(); +test_tank(); +benchmark_fluids(); +printf('\nAll tests completed!\n');