use strict;
package water_density;
# t ft^3/lb lb/ft^3 lb/gallon
my @tbl = (qw(
32 0.01602 62.41 8.344
40 0.01602 62.43 8.345
50 0.01602 62.41 8.343
60 0.01603 62.37 8.338
70 0.01605 62.31 8.329
80 0.01607 62.22 8.318
90 0.01610 62.12 8.304
100 0.01613 62.00 8.288
110 0.01617 61.86 8.270
120 0.01620 61.71 8.250
130 0.01625 61.55 8.228
140 0.01629 61.38 8.205
150 0.01634 61.19 8.180
160 0.01640 60.99 8.154
170 0.01645 60.79 8.126
180 0.01651 60.57 8.097
190 0.01657 60.34 8.067
200 0.01664 60.11 8.035
210 0.01670 59.86 8.002
212 0.01672 59.81 7.996
220 0.01678 59.61 7.969
240 0.01693 59.08 7.898
));
sub new {
my $class = shift;
my $s = {};
my (
$j,
$i,
$cnt,
);
$cnt = scalar(@tbl);
for ($j = 0; $j < $cnt; $j += 4) {
my %r = ();
@r{qw(t ft3_lb lb_ft3 lb_gal)} = @tbl[$j .. $j+3];
push(@{$s->{density}}, \%r);
}
bless $s, $class;
}
sub lb_in3 {
my $s = shift;
my $t = shift;
my $lb_ft3;
for (;;) {
my $d0;
my $d1;
($t < ($d0 = $s->{density}->[0])->{t}) &&
($lb_ft3 = $d0->{lb_ft3}) &&
(last);
($t >= ($d0 = $s->{density}->[$#{$s->{density}}])->{t}) &&
($lb_ft3 = $d0->{lb_ft3}) &&
(last);
for (my $i = 0; $i < $#{$s->{density}}; $i++) {
($t >= ($d0 = $s->{density}->[$i + 0])->{t}) &&
($t < ($d1 = $s->{density}->[$i + 1])->{t}) &&
(last);
}
$lb_ft3 = $d0->{lb_ft3} + (
(($t - $d0->{t}) / ($d1->{t} - $d0->{t})) *
($d1->{lb_ft3} - $d0->{lb_ft3})
);
last;
}
return($lb_ft3 / 12.0**3);
}
1;
package main;
# In order to calculate the friction loss of water, the Hazen-Williams
# formula is used:
# head =
# Where
# head = Friction loss in feet of water per 100 ft.
# gpm = Flow rate (gpm)
# hwc = Hazen-Williams Coefficient
# d = Inside Diameter (in.)
# The Hazen-Williams coefficient for Lamson Vylon HDPE pipe is
# 150 and doesn't change over time. With its superior corrosion
# resistance it will remain smooth and not corrode or tuberculate.
# head = (1044 * gpm**1.85) / (hwc**1.85 * d**4.865)
#
sub gpm_to_head {
my $s = shift();
my $ft_h20_per_100ft =
(1044.0 * $s->{gpm}**1.85) /
($s->{hwc}**1.85 * $s->{d}**4.865)
;
return($ft_h20_per_100ft * ($s->{l} / 100.0));
}
# Take d=diameter l=length head=head hwc=hazen-williams coefficient and calculate flow
# using head calculator iteratively.
sub head_to_gpm {
my $s = shift();
my $gpm_was = 0.0;
my $head_was = 0.0;
my $head_goal = $s->{head};
my $head;
$s->{gpm} = 1.0;
my $i;
for ($i = 0; $i < 20; $i++) {
$head = gpm_to_head($s);
(abs($head - $head_was) < 0.000001) && (last);
my $slope = ($head - $head_was) / ($s->{gpm} - $gpm_was);
my $gpm_nxt = $s->{gpm} + ($head_goal - $head) / $slope;
if (0) {
printf("head=%.5f head=%.5f slope=%.5f gpm=%.5f gpm_nxt=%.5f\n"
,$head_goal
,$head
,$slope
,$s->{gpm}
,$gpm_nxt
);
}
$gpm_was = $s->{gpm};
$s->{gpm} = $gpm_nxt;
$head_was = $head;
}
return($s->{gpm});
}
sub ft_h2o {
my $psi = shift;
return(2.3066587 * $psi);
}
sub gpm {
my $s = shift;
my $wd = water_density->new();
my $lb_in3_t0 = $wd->lb_in3($s->{t0});
my $lb_in3_t1 = $wd->lb_in3($s->{t1});
if (0) {
printf(
"t0=%s degF\n".
"t1=%s degF\n".
"rise=%s inch\n" .
"lb per gal t0=%.5f\nlb per gal t1=%.5f\n"
,$s->{t0}
,$s->{t1}
,$s->{rise}
,$lb_in3_t0 * 231.0
,$lb_in3_t1 * 231.0
);
}
my $lb_t0 = $s->{rise} * $lb_in3_t0;
my $lb_t1 = $s->{rise} * $lb_in3_t1;
my $lb_diff = $lb_t0 - $lb_t1;
my $ft_h2o = ft_h2o($lb_diff);
printf(
"psi_t0=%.5f psi_t1=%.5f psi_diff=%.5f psi head=%.5f ft_H2O\n"
,$lb_t0
,$lb_t1
,$lb_diff
,$ft_h2o
);
$s->{head} = $ft_h2o;
printf("t0=%s degF t1=%s degF rise=%s inch d=%s inch length=%s ft gpm=%.5f\n"
,$s->{t0}
,$s->{t1}
,$s->{rise}
,$s->{d}
,$s->{l}
,head_to_gpm($s)
);
}
sub main {
printf("\n");
my %s;
# Hazen-Williams coefficient=100 diameter=1.5 inch length = 16 ft.
$s{hwc} = 100.0;
$s{d} = 1.5;
$s{l} = 16.0;
$s{rise} = 54.0;
$s{t0} = 185;
$s{t1} = 230;
gpm(\%s);
printf("\n");
$s{hwc} = 100.0;
$s{d} = 1.25;
$s{l} = 16.0;
$s{rise} = 54.0;
$s{t0} = 160;
$s{t1} = 220;
gpm(\%s);
printf("\n");
}
main();