julia Integration

This example demonstrates how to use fluids from julia.

Source Code

  1using PyCall
  2using Printf
  3const fluids = pyimport("fluids")
  4
  5function test_fluids()
  6    try
  7        # 1. Test basic module import
  8        println("✓ Successfully imported fluids")
  9        println("✓ Fluids version: ", fluids.__version__)
 10        
 11        # 2. Test basic Reynolds number calculation
 12        Re = fluids.Reynolds(V=2.5, D=0.1, rho=1000, mu=0.001)
 13        println("✓ Reynolds number calculation successful: ", Re)
 14        @assert Re > 0
 15        
 16        # 3. Test friction factor calculation
 17        fd = fluids.friction_factor(Re=1e5, eD=0.0001)
 18        println("✓ Friction factor calculation successful: ", fd)
 19        @assert 0 < fd < 1
 20                
 21        println("\nAll basic tests completed successfully!")
 22        
 23    catch e
 24        println("Error occurred: ", e)
 25        rethrow(e)
 26    end
 27end
 28
 29function test_atmosphere()
 30    try
 31        # Test ATMOSPHERE_1976 class
 32        atm = fluids.ATMOSPHERE_1976(Z=5000)
 33        
 34        println("\nTesting atmosphere at 5000m elevation:")
 35        println("✓ Temperature: ", round(atm.T, digits=4))
 36        println("✓ Pressure: ", round(atm.P, digits=4))
 37        println("✓ Density: ", round(atm.rho, digits=6))
 38        
 39        # Test derived properties
 40        println("✓ Gravity: ", round(atm.g, digits=6))
 41        println("✓ Viscosity: ", @sprintf("%.6e", atm.mu))
 42        println("✓ Thermal conductivity: ", round(atm.k, digits=6))
 43        println("✓ Sonic velocity: ", round(atm.v_sonic, digits=4))
 44        
 45        # Test static methods
 46        g_high = fluids.ATMOSPHERE_1976.gravity(Z=1E5)
 47        println("✓ High altitude gravity: ", round(g_high, digits=6))
 48        
 49        v_sonic = fluids.ATMOSPHERE_1976.sonic_velocity(T=300)
 50        println("✓ Sonic velocity at 300K: ", round(v_sonic, digits=4))
 51        
 52        mu_400 = fluids.ATMOSPHERE_1976.viscosity(T=400)
 53        println("✓ Viscosity at 400K: ", @sprintf("%.6e", mu_400))
 54        
 55        k_400 = fluids.ATMOSPHERE_1976.thermal_conductivity(T=400)
 56        println("✓ Thermal conductivity at 400K: ", round(k_400, digits=6))
 57        
 58    catch e
 59        println("Error in atmosphere tests: ", e)
 60        rethrow(e)
 61    end
 62end
 63
 64function test_tank()
 65    try
 66        # Test basic tank creation
 67        T1 = fluids.TANK(V=10, L_over_D=0.7, sideB="conical", horizontal=false)
 68        println("\nTesting tank calculations:")
 69        println("✓ Tank length: ", round(T1.L, digits=6))
 70        println("✓ Tank diameter: ", round(T1.D, digits=6))
 71        
 72        # Test ellipsoidal tank
 73        tank_ellip = fluids.TANK(D=10, V=500, horizontal=false,
 74                                sideA="ellipsoidal", sideB="ellipsoidal",
 75                                sideA_a=1, sideB_a=1)
 76        println("✓ Ellipsoidal tank L: ", round(tank_ellip.L, digits=6))
 77        
 78        # Test torispherical tank
 79        DIN = fluids.TANK(L=3, D=5, horizontal=false,
 80                         sideA="torispherical", sideB="torispherical",
 81                         sideA_f=1, sideA_k=0.1, sideB_f=1, sideB_k=0.1)
 82        
 83        println("✓ Tank representation: ", string(DIN))
 84        println("✓ Tank max height: ", round(DIN.h_max, digits=6))
 85        println("✓ Height at V=40: ", round(DIN.h_from_V(40), digits=6))
 86        println("✓ Volume at h=4.1: ", round(DIN.V_from_h(4.1), digits=5))
 87        println("✓ Surface area at h=2.1: ", round(DIN.SA_from_h(2.1), digits=5))
 88        
 89    catch e
 90        println("Error in tank tests: ", e)
 91        rethrow(e)
 92    end
 93end
 94
 95function test_reynolds()
 96    try
 97        println("\nTesting Reynolds number calculations:")
 98        
 99        # Test with density and viscosity
100        Re1 = fluids.Reynolds(V=2.5, D=0.25, rho=1.1613, mu=1.9E-5)
101        println("✓ Re (with rho, mu): ", round(Re1, digits=4))
102        @assert abs(Re1 - 38200.6579) < 0.1
103        
104        # Test with kinematic viscosity
105        Re2 = fluids.Reynolds(V=2.5, D=0.25, nu=1.636e-05)
106        println("✓ Re (with nu): ", round(Re2, digits=4))
107        @assert abs(Re2 - 38202.934) < 0.1
108        
109    catch e
110        println("Error in Reynolds tests: ", e)
111        rethrow(e)
112    end
113end
114
115function test_psd()
116    try
117        println("\nTesting particle size distribution functionality:")
118        
119        # Create a discrete PSD
120        ds = [240, 360, 450, 562.5, 703, 878, 1097, 1371, 1713, 2141, 2676, 3345, 4181, 5226, 6532]
121        numbers = [65, 119, 232, 410, 629, 849, 990, 981, 825, 579, 297, 111, 21, 1]
122        
123        psd = fluids.particle_size_distribution.ParticleSizeDistribution(
124            ds=ds,
125            fractions=numbers,
126            order=0
127        )
128        println("✓ Created discrete PSD")
129        
130        # Test mean sizes
131        d21 = psd.mean_size(2, 1)
132        println("✓ Size-weighted mean diameter: ", round(d21, digits=4))
133        @assert abs(d21 - 1857.788) < 0.1
134        
135        d10 = psd.mean_size(1, 0)
136        println("✓ Arithmetic mean diameter: ", round(d10, digits=4))
137        @assert abs(d10 - 1459.372) < 0.1
138        
139        # Test percentile calculations
140        d10_percentile = psd.dn(0.1)
141        d90_percentile = psd.dn(0.9)
142        println("✓ D10: ", round(d10_percentile, digits=4))
143        println("✓ D90: ", round(d90_percentile, digits=4))
144        
145        # Test probability functions
146        pdf_val = psd.pdf(1000)
147        cdf_val = psd.cdf(5000)
148        println("✓ PDF at 1000: ", @sprintf("%.4e", pdf_val))
149        println("✓ CDF at 5000: ", round(cdf_val, digits=6))
150        
151        # Test lognormal distribution
152        psd_log = fluids.particle_size_distribution.PSDLognormal(s=0.5, d_characteristic=5E-6)
153        println("✓ Created lognormal PSD")
154        
155        vssa = psd_log.vssa
156        println("✓ Volume specific surface area: ", round(vssa, digits=2))
157        
158        span = psd_log.dn(0.9) - psd_log.dn(0.1)
159        println("✓ Span: ", @sprintf("%.4e", span))
160        
161        ratio_7525 = psd_log.dn(0.75)/psd_log.dn(0.25)
162        println("✓ D75/D25 ratio: ", round(ratio_7525, digits=6))
163        
164    catch e
165        println("Error in PSD tests: ", e)
166        rethrow(e)
167    end
168end
169function benchmark_fluids()
170    println("\nRunning benchmarks:")
171    
172    # Benchmark friction factor calculation
173    println("\nBenchmarking friction_factor:")
174    t1 = @elapsed for i in 1:1000000
175        fluids.friction_factor(Re=1e5, eD=0.0001)
176    end
177    println("Time for 1e6 friction_factor calls: ", round(t1, digits=6), " seconds")
178    println("Average time per call: ", round(t1/1000000, digits=6), " seconds")
179    
180    # Benchmark tank creation
181    println("\nBenchmarking TANK creation:")
182    t2 = @elapsed for i in 1:1000
183        fluids.TANK(L=3, D=5, horizontal=false,
184                   sideA="torispherical", sideB="torispherical",
185                   sideA_f=1, sideA_k=0.1, sideB_f=1, sideB_k=0.1)
186    end
187    println("Average time per creation: ", round(t2/1000, digits=6), " seconds")
188    
189end
190
191# Add this line at the end of your script to run the benchmark
192# Run all tests
193println("Running fluids tests from Julia...")
194test_fluids()
195test_atmosphere()
196test_tank()
197test_reynolds()
198test_psd()
199benchmark_fluids()
200println("\nAll tests completed!") 

Requirements

  • Python with fluids installed

  • PyCall.jl package

Usage Notes

  • 8 microsecond friction factor, 20 microsecond tank creation observed by author