# perl Integration

This example demonstrates how to use fluids from perl.

## Source Code

      1#!/usr/bin/perl
      2use strict;
      3use warnings;
      4use Inline Python => <<'END_PYTHON';
      5import fluids
      6END_PYTHON
      7use Time::HiRes qw(time);
      8use POSIX qw(ceil floor);
      9
     10# Function to test basic fluids functionality
     11sub test_fluids {
     12    eval {
     13        # Test basic module import and version
     14        my $version = Inline::Python::py_eval('fluids.__version__', 0);
     15        print "✓ Successfully imported fluids\n";
     16        print "✓ Fluids version: $version\n";
     17
     18        # Test basic Reynolds number calculation
     19        # Create kwargs dictionary in Python
     20        Inline::Python::py_eval('kwargs = {"V": 2.5, "D": 0.1, "rho": 1000, "mu": 0.001}');
     21        my $Re = Inline::Python::py_eval('fluids.Reynolds(**kwargs)', 0);
     22        print "✓ Reynolds number calculation successful: $Re\n";
     23        die "Invalid Reynolds number" unless $Re > 0;
     24
     25        # Test friction factor calculation
     26        Inline::Python::py_eval('kwargs = {"Re": 1e5, "eD": 0.0001}');
     27        my $fd = Inline::Python::py_eval('fluids.friction_factor(**kwargs)', 0);
     28        print "✓ Friction factor calculation successful: $fd\n";
     29        die "Invalid friction factor" unless ($fd > 0 && $fd < 1);
     30
     31        print "\nAll basic tests completed successfully!\n";
     32    };
     33    if ($@) {
     34        print "Error occurred: $@\n";
     35        die $@;
     36    }
     37}
     38
     39# Function to test atmosphere calculations
     40sub test_atmosphere {
     41    eval {
     42        # Test ATMOSPHERE_1976 class
     43        Inline::Python::py_eval('atm = fluids.ATMOSPHERE_1976(Z=5000)');
     44
     45        print "\nTesting atmosphere at 5000m elevation:\n";
     46        printf "✓ Temperature: %.4f\n", Inline::Python::py_eval('atm.T', 0);
     47        printf "✓ Pressure: %.4f\n", Inline::Python::py_eval('atm.P', 0);
     48        printf "✓ Density: %.6f\n", Inline::Python::py_eval('atm.rho', 0);
     49
     50        # Test derived properties
     51        printf "✓ Gravity: %.6f\n", Inline::Python::py_eval('atm.g', 0);
     52        printf "✓ Viscosity: %.6e\n", Inline::Python::py_eval('atm.mu', 0);
     53        printf "✓ Thermal conductivity: %.6f\n", Inline::Python::py_eval('atm.k', 0);
     54        printf "✓ Sonic velocity: %.4f\n", Inline::Python::py_eval('atm.v_sonic', 0);
     55
     56        # Test static methods
     57        my $g_high = Inline::Python::py_eval('fluids.ATMOSPHERE_1976.gravity(Z=1E5)', 0);
     58        printf "✓ High altitude gravity: %.6f\n", $g_high;
     59
     60        my $v_sonic = Inline::Python::py_eval('fluids.ATMOSPHERE_1976.sonic_velocity(T=300)', 0);
     61        printf "✓ Sonic velocity at 300K: %.4f\n", $v_sonic;
     62    };
     63    if ($@) {
     64        print "Error in atmosphere tests: $@\n";
     65        die $@;
     66    }
     67}
     68
     69# Function to test tank calculations
     70
     71
     72# Function to test tank calculations
     73sub test_tank {
     74    eval {
     75        # Test basic tank creation
     76        Inline::Python::py_eval(<<'END_PYTHON');
     77T1 = fluids.TANK(V=10, L_over_D=0.7, sideB='conical', horizontal=False)
     78END_PYTHON
     79        print "\nTesting tank calculations:\n";
     80        printf "✓ Tank length: %.6f\n", Inline::Python::py_eval('T1.L', 0);
     81        printf "✓ Tank diameter: %.6f\n", Inline::Python::py_eval('T1.D', 0);
     82
     83        # Test ellipsoidal tank
     84        Inline::Python::py_eval(<<'END_PYTHON');
     85tank_ellip = fluids.TANK(D=10, V=500, horizontal=False,
     86                       sideA='ellipsoidal', sideB='ellipsoidal',
     87                       sideA_a=1, sideB_a=1)
     88END_PYTHON
     89        printf "✓ Ellipsoidal tank L: %.6f\n", Inline::Python::py_eval('tank_ellip.L', 0);
     90
     91        # Test torispherical tank
     92        Inline::Python::py_eval(<<'END_PYTHON');
     93DIN = fluids.TANK(L=3, D=5, horizontal=False,
     94                sideA='torispherical', sideB='torispherical',
     95                sideA_f=1, sideA_k=0.1, sideB_f=1, sideB_k=0.1)
     96END_PYTHON
     97        printf "✓ Tank max height: %.6f\n", Inline::Python::py_eval('DIN.h_max', 0);
     98        printf "✓ Height at V=40: %.6f\n", Inline::Python::py_eval('DIN.h_from_V(40)', 0);
     99        printf "✓ Volume at h=4.1: %.5f\n", Inline::Python::py_eval('DIN.V_from_h(4.1)', 0);
    100        printf "✓ Surface area at h=2.1: %.5f\n", Inline::Python::py_eval('DIN.SA_from_h(2.1)', 0);
    101    };
    102    if ($@) {
    103        print "Error in tank tests: $@\n";
    104        die $@;
    105    }
    106}
    107# Function to benchmark fluids operations
    108
    109
    110# Function to benchmark fluids operations
    111sub benchmark_fluids {
    112    print "\nRunning benchmarks:\n";
    113
    114    # Benchmark friction factor calculation
    115    print "\nBenchmarking friction_factor:\n";
    116    my $start_time = time();
    117    Inline::Python::py_eval(<<'END_PYTHON');
    118for i in range(10000):
    119    fluids.friction_factor(Re=1e5, eD=0.0001)
    120END_PYTHON
    121    my $duration = time() - $start_time;
    122    printf "Time for 10000 friction_factor calls: %.6f seconds\n", $duration;
    123    printf "Average time per call: %.6f seconds\n", $duration/10000;
    124
    125    # Benchmark tank creation
    126    print "\nBenchmarking TANK creation:\n";
    127    $start_time = time();
    128    Inline::Python::py_eval(<<'END_PYTHON');
    129for i in range(1000):
    130    fluids.TANK(L=3, D=5, horizontal=False,
    131               sideA='torispherical', sideB='torispherical',
    132               sideA_f=1, sideA_k=0.1, sideB_f=1, sideB_k=0.1)
    133END_PYTHON
    134    $duration = time() - $start_time;
    135    printf "Time for 1000 TANK creations: %.6f seconds\n", $duration;
    136    printf "Average time per creation: %.6f seconds\n", $duration/1000;
    137}
    138
    139# Run all tests
    140print "Running fluids tests from Perl...\n";
    141test_fluids();
    142test_atmosphere();
    143test_tank();
    144benchmark_fluids();
    145print "\nAll tests completed!\n";

## Requirements

- Python with fluids installed

- Inline-Python: https://metacpan.org/dist/Inline-Python/view/Python.pod

## Usage Notes

- 2 microsecond friction factor, 10 microsecond tank creation observed by author