lib/Tribology/Lubricant.pm

filter: Statementstmt 83.5% 127 / 152 Branchbran 62.0% 36 / 58 Conditioncond 43.4% 10 / 23 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 83.3% 15 / 18 Podpod 80.0% 8 / 10 totaltotal 72.6% 199 / 274 CC 38 SCAR 28.4
CC
38
Cov
74%
data_table
72.0
LH
17.4
visc
5.0
CRAP 17.1
↑ Summary
1 package Tribology::Lubricant;
2  
3 =head1 NAME
4  
5 Tribology::Lubricant - Data type that represents a Lubricant class.
6  
7 =head1 DESCRIPTION
8  
9 This class, given technical data based on lubricant TDS/PDS documents assists in calculation of various Rheologic characteristics of the lubricant. Such as:
10  
11 =over 4
12  
13 =item *
14  
15 V-T behavior (C<m()>) using B<Ubbelohde-Walter> equation
16  
17 =item *
18  
19 Calculate viscosity at a given temperature ( C<visc()> ) when any of two calibration points are known
20  
21 =item *
22  
23 Calculate viscosity index using ASTM D2270's B<A> and B<B> procedures ( C<vi()> )
24  
25 =item *
26  
27 Lookup B<L> and B<H> constants of the lubricant using ASTM D2270 Table and using linear interoplation whenever neccessary ( C<LH()> )
28  
29 =back
30  
31 =head1 SYNOPSIS
32  
33 require Tribology::Lubricant;
34  
35 # We already have viscosity at 40C and 100C.
36 my $lub = Tribology::Lubricant->new({
37 label => "Naphthenic spindle oil",
38 visc40 => 30,
39 visc100 => 100
40 });
41  
42 # Viscosity @ 50C:
43 my $visc50 = $lub->visc(50);
44  
45 # Viscosity index (VI)
46 my $vi = $lub->vi;
47  
48 # Viscosity-temperature constant:
49 my $vtc = $lub->vtc;
50  
51 # m-value, aka V-T behavior coefficient
52 my $m = $lub->m;
53  
54 # To draw the V-T (hyperbolic) graph of this particular lubricant we can generate data-points, say, from -20 to +100:
55  
56 my @data_points;
57 for my $T(-20..100) {
58 push @data_points, [$T, $lub->visc($T)];
59 }
60  
61 # Now you may pass @data_points to either GDGrap(Perl) or Highcharts(JS).
62  
63 =cut
64  
65 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 1 our $VERSION = '0.03';
70  
71 =head2 new(\%attr)
72  
73 Constructor. Following attributes (all optional) can be passed:
74  
75 =over 4
76  
77 =item label
78  
79 Arbitrary label of the lubricant. Used in graph data or report tables, charts
80  
81 =item visc40, visc100
82  
83 Viscosity @ 40 and 100 degrees Celcius.
84  
85 =item vi
86  
87 Viscosity index of the lubricant.
88  
89 =item density
90  
91 Specific gravity of the lubricant at a given temperature point. Must be passed a hashref of Temprature-Density values. Density
92 must be in kg/cm3.
93  
94 =back
95  
96 B<IMPORTANT> C<visc40> and C<visc100> are just convenience attributes, since they are most widely given in product TDSs.
97 If you don't have calibration points at these temperatures IGNORE these attributes. Instead,
98 create empty constructor, set the calibration values you already have using C<visc()> method. Such as:
99  
100 my $lubricant = Tribology::Lubricant->new({label => "Hypothetical lubricant"});
101 $lubricant->visc(50, 80);
102 $lubricant->visc(100, 5.23);
103  
104 =cut
105  
106 9 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
107 9 my ( $class, $arg ) = @_;
108  
109 9 ▶ $arg ||= {};
Condition100%2/2
$arg ||= {}
l!l
81
MC/DC100%1/1
$arg ||= {}
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
110  
111 9 my %internals = (
112 __visc_calibration_points => {},
113 %$arg
114 );
115  
116 9 ▶ if ( defined( $internals{visc40} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $internals{"visc40"}) 7 2 9
117 7 $internals{__visc_calibration_points}{40} = $internals{visc40};
118 }
119  
120 9 ▶ if ( defined $internals{visc100} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $internals{"visc100"}) 0 9 9
121 0 $internals{__visc_calibration_points}{100} = $internals{visc100};
122 }
123 9 return bless( \%internals, $class );
124 }
125  
126 =head2 label($new_label)
127  
128 Returns and/or sets label of the lubricant
129  
130 =cut
131  
132 16 ▶sub label {
Subroutine label: called CC=2 SCAR=7.6
Pod documented
133 16 my ( $self, $new_label ) = @_;
134  
135 16 ▶ if ($new_label) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_label) 0 16 16
136 0 $self->{label} = $new_label;
137 }
138 16 return $self->{label};
139 }
140  
141 =head2 visc($T, $cst)
142  
143 Given temperature ($T) in celcius returns kinematic viscosity in cst. If such value was not given to the constructor it attempts to calculate
144 this number using B<Ubbelohde-Walter> equation. For this to be possible at least two calibration points must be given to C<new()> or two calibration
145 points must be set using two-argument syntax of C<visc()>.
146  
147 If second argument is passed sets the viscosity point and returns the value $cst as is.
148  
149 # 1.10 (eni), bo'yi ( 2.27 )
150  
151 =cut
152  
153 7127 ▶sub visc {
Subroutine visc: called CC=5 SCAR=16.2
Pod documented
154 7127 my ( $self, $T, $cst ) = @_;
155  
156 7127 ▶ unless ( defined $T ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $T) 0 7127 7127
157 0 croak "visc(): usage error";
158 }
159  
160 7127 ▶ if ( defined $cst ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cst) 18 7109 7127
161 18 $self->{__visc_calibration_points}{$T} = $cst;
162 18 return $cst;
163 }
164  
165 # If this calibration point was already given just return it:
166 7109 ▶ if ( my $visc = $self->{__visc_calibration_points}{$T} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $visc = $self->{"__visc_calibration_points"}{$T}) 5715 1394 7109
167 5715 return $visc;
168 }
169  
170 # We have to attempt to calculate the viscosity at this given temp
171  
172 1394 my @calibrations = @{ $self->__calibration_points(2) };
173 1393 ▶ unless ( scalar(@calibrations) == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @calibrations == 2) 0 1393 1393
174 0 croak "visc(): Not enough calibration points to complete Ubbelohde-Walter equation";
175 }
176  
177 1393 my $c1_temp = $calibrations[0][0];
178 1393 my $c1_cst = $calibrations[0][1];
179  
180 1393 my $c2_temp = $calibrations[1][0];
181 1393 my $c2_cst = $calibrations[1][1];
182  
183 1393 my $vtc = $self->vtc;
184  
185 #my $b = ( log( log( $c1_cst + $vtc ) ) - log( log( $c2_cst + $vtc ) ) ) / ( log($c2_temp) - log($c1_temp) );
186 1393 my $m = $self->m;
187 1393 my $a = log( log( $c2_cst + $vtc ) / log(10) ) / log(10) + ( $m * log($c2_temp) / log(10) );
188  
189 #carp "\$c2_cst = $c2_cst";
190 #carp "\$c2_temp = $c2_temp";
191 #carp "\$a = $a";
192 #carp "\$m = $m";
193 #carp "\$vtc = $vtc";
194  
195 1393 my $c = $a * 100;
196 1393 my $d = $m * 25;
197  
198 1393 my $visc = exp(1)**( log(10) * exp(1)**( ( ( $c * log(10) ) / 100 ) - ( ( $d * log( __c2k($T) ) ) / 25 ) ) ) - 0.7;
199  
200 1393 $self->{__visc_calibration_points}{$T} = $visc;
201 1393 return $visc;
202 } ## end sub visc
203  
204 =head2 m()
205  
206 Heart of the B<Ubbelohde-Walter> equation. This is the coeffient that characterises V-T behavior of oils. It's a double-logarithmic V-T graph slope.
207 It requires at least two calibration points be present, or must be calculatable to work. Otherwise it throws error (croaks).
208  
209 =cut
210  
211 1407 ▶sub m {
Subroutine m: called CC=2 SCAR=7.0
Pod documented
212 1407 my $self = shift;
213  
214 1407 my $calibrations = $self->__calibration_points(2);
215  
216 1407 ▶ unless ( scalar @$calibrations == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$calibrations == 2) 0 1407 1407
217 0 croak "m(): at least 2 calibration points are required to properly calculate V-T slope (m).";
218 }
219  
220 1407 my $c1_temp = $calibrations->[0][0];
221 1407 my $c1_cst = $calibrations->[0][1];
222  
223 1407 my $c2_temp = $calibrations->[1][0];
224 1407 my $c2_cst = $calibrations->[1][1];
225
226 #carp "c1_temp = $c1_temp\nc1_cst=$c1_cst\n";
227 #carp "c2_temp = $c2_temp\nc2_cst=$c2_cst\n";
228  
229 1407 my $vtc = $self->vtc;
230 1407 return ( log( log( $c1_cst + $vtc ) ) - log( log( $c2_cst + $vtc ) ) ) / ( log($c2_temp) - log($c1_temp) ) ;
231 }
232  
233 =head2 LH()
234  
235 Returns B<L> and B<H> values for the given lubricant. For this method to work lubricant's viscosity @ 100C must be known or calculatable.
236  
237 =cut
238  
239 31 ▶sub LH {
Subroutine LH: called CC=16 SCAR=28.6
Pod documented
240 31 my ($self) = @_;
241  
242 31 my $cst100 = $self->visc(100);
243 31 ▶ unless ($cst100) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cst100) 0 31 31
244 0 croak "__LH(): Viscosity of the lubricant \@ 100C is unknown. Cannot proceed further";
245 }
246  
247 31 ▶ if ( $cst100 < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cst100 < 2) 0 31 31
248 0 croak "__LH(): ASTM D2270 does not define VI for lubricants below 2cst \@ 100C";
249 }
250  
251 31 ▶ if ( $cst100 > 70 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cst100 > 70) 1 30 31
252 1 my $L = ( 0.8353 * ( $cst100**2 ) ) + 14.67 * $cst100 - 216;
253 1 my $H = ( 0.1684 * ( $cst100**2 ) ) + 11.85 * $cst100 - 97;
254 1 return ( $L, $H );
255 }
256  
257 30 my ( @one_before, @one_after );
258 30 seek( DATA, 0, 0 );
259 30 ▶ while ( my $line = <DATA> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline DATA)) 17745 0 17745
260 17745 ▶ next unless length($line);
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 17745 17745
261 17745 ▶ next if $line =~ m/^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 120 17625 17745
262 17625 ▶ next if $line =~ m/^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 4680 12945 17625
263  
264 12945 my ( $visc, $L, $H ) = $line =~ m/^
265 ([\d\.]+) \s+ ([\d\.]+) \s+ ([\d\.]+)
266 \s* $/x;
267  
268 12945 ▶ next unless ( $visc && $L && $H );
Branch: 1 branch
BranchTrueFalseTotal
unless $visc and $L and $H 9720 3225 12945
Condition66%4/6
$visc and $L and $H
!ll&&!rl&&r
972003225
$visc and $L
!ll&&!rl&&r
972003225
MC/DC33%1/3
$visc and $L and $H
$visc $L $H
Truth table50%2/4
A and B and C
A= $viscB= $LC= $H
ABCresult
0XX0
10X0
1100
1111
269 3225 ▶ return ( $L, $H ) if ( $visc == $cst100 );
Branch: 1 branch
BranchTrueFalseTotal
if $visc == $cst100 13 3212 3225
270 3212 ▶ if ( $visc < $cst100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($visc < $cst100) 3195 17 3212
271 3195 @one_before = ( $visc, $L, $H );
272 3195 next;
273 }
274 17 ▶ if ( $visc > $cst100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($visc > $cst100) 17 0 17
275 17 push @one_after, $visc, $L, $H;
276 17 last;
277 }
278 }
279  
280 17 ▶ unless ( @one_before && @one_after ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@one_before and @one_after) 0 17 17
Condition33%1/3
@one_before and @one_after
!ll&&!rl&&r
0017
MC/DC0%0/2
@one_before and @one_after
@one_before @one_after
Truth table33%1/3
A and B
A= @one_beforeB= @one_after
ABresult
0X0
100
111
281 0 croak "__LH(): has nothing to interoplate";
282 }
283  
284 17 my $visc1 = $one_before[0];
285 17 my $visc2 = $one_after[0];
286 17 my $visc_delta = $visc2 - $visc1;
287  
288 17 my $L1 = $one_before[1];
289 17 my $L2 = $one_after[1];
290 17 my $L_delta = $L2 - $L1;
291  
292 17 my $H1 = $one_before[2];
293 17 my $H2 = $one_after[2];
294 17 my $H_delta = $H2 - $H1;
295  
296 17 my $L1_per_unit = $L_delta / $visc_delta;
297 17 my $H1_per_unit = $H_delta / $visc_delta;
298  
299 17 my $L = $L1 + ( ( $cst100 - $visc1 ) * $L1_per_unit );
300 17 my $H = $H1 + ( ( $cst100 - $visc1 ) * $H1_per_unit );
301  
302 17 return ( $L, $H );
303 } ## end sub LH
304  
305  
306 0 ▶sub data_table {
Subroutine data_table: not called CC=8 SCAR=42.8
Pod undocumented
307 0 my $self = shift;
308
309 0 my @data = ();
310 0 seek( DATA, 0, 0 );
311 0 ▶ while ( my $line = <DATA> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline DATA)) 0 0 0
312 0 ▶ next unless length($line);
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 0 0
313 0 ▶ next if $line =~ m/^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 0 0
314 0 ▶ next if $line =~ m/^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
315  
316 0 my ( $visc, $L, $H ) = $line =~ m/^
317 ([\d\.]+) \s+ ([\d\.]+) \s+ ([\d\.]+)
318 \s* $/x;
319  
320 0 ▶ next unless ( $visc && $L && $H );
Branch: 1 branch
BranchTrueFalseTotal
unless $visc and $L and $H 0 0 0
Condition0%0/6
$visc and $L and $H
!ll&&!rl&&r
000
$visc and $L
!ll&&!rl&&r
000
MC/DC0%0/3
$visc and $L and $H
$visc $L $H
Truth table0%0/4
A and B and C
A= $viscB= $LC= $H
ABCresult
0XX0
10X0
1100
1111
321
322 0 push @data, [$visc, $L, $H];
323 }
324 0 return \@data;
325 }
326  
327 =head2 vi()
328  
329 Returns viscosity index of the lubricant, if such is possible. Remember, for this to be possible
330 calibration points at 40C and 100C must be available or calculatble. If it's impossible, it returns undef and writes a warning
331 to STDERR. When checking for error you must check for C<undef> at return.
332  
333 =cut
334  
335 17 ▶sub vi {
Subroutine vi: called CC=2 SCAR=6.9
Pod documented
336 17 my ($self) = @_;
337  
338 17 my $vi = $self->__vi_lt_100;
339 16 ▶ if ( $vi > 100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vi > 100) 12 4 16
340 12 $vi = $self->__vi_gt_100;
341 }
342 16 return $vi;
343 }
344  
345 36 ▶sub log10 {
Subroutine log10: called CC=1 SCAR=0.0
Pod undocumented
346 36 my ($n) = @_;
347 36 return ( log($n) / log(10) );
348 }
349  
350 =head2 vtc()
351  
352 Returns B<VTC - viscosity-temperature constant> used in B<Ubbelohde-Walter> equation to better differentiate V-T behavior
353 when the influence of temperature is low. This constant must be used to accurately (or properly) calculate C<m>.
354 To calculate this value properly we need to have calibration points at 40C and 100C. If either of these points are missing
355 C<vtc()> defaults to 0.8.
356  
357 =cut
358  
359 2816 ▶sub vtc {
Subroutine vtc: called CC=3 SCAR=11.0
Pod documented
360 2816 my $self = shift;
361  
362 2816 my $visc40 = $self->{__visc_calibration_points}{40};
363 2816 my $visc100 = $self->{__visc_calibration_points}{100};
364  
365 2816 ▶ return 0.8 unless ( $visc40 && $visc100 );
Branch: 1 branch
BranchTrueFalseTotal
unless $visc40 and $visc100 1 2815 2816
Condition66%2/3
$visc40 and $visc100
!ll&&!rl&&r
012815
MC/DC50%1/2
$visc40 and $visc100
$visc40 $visc100
Truth table66%2/3
A and B
A= $visc40B= $visc100
ABresult
0X0
100
111
366 2815 return ( $visc40 - $visc100 ) / $visc40 ;
367 }
368  
369 =head2 is_mineral
370  
371 Based on the C<m> constant or C<vi> attempts to guess if current instance represents a mineral oil.
372  
373 =cut
374  
375 0 ▶sub is_mineral {
Subroutine is_mineral: not called CC=1 SCAR=6.9
Pod documented
376 0 my $self = shift;
377 0 my $vi = $self->vi;
378 0 die "work in progress";
379 }
380  
381 =head1 INTERNALS
382  
383 =head2 __c2k($T)
384  
385 Given temperature in celcius converts it to Kelvin
386  
387 =cut
388  
389 6994 ▶sub __c2k {
Subroutine __c2k: called CC=2 SCAR=6.9
390 6994 my ($c) = @_;
391 6994 ▶ unless (defined $c) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c) 1 6993 6994
392 1 die "__c2k(): usage error";
393 }
394 6993 return ( $c + 273.15 );
395 }
396  
397 =head2 __k2c($T)
398  
399 Given temperature in Kelvin converts it to celcius
400  
401 =cut
402  
403 0 ▶sub __k2c {
Subroutine __k2c: not called CC=1 SCAR=6.9
404 0 my ($k) = @_;
405 0 return ( $k - 273.15 );
406 }
407  
408 =head2 __calibration_points($limit)
409  
410 Returns all known calibration points to the lubricant as array reference. If C<$limit> is given limits the result set to that many points.
411 The points are guaranteed to be in ascending order by temperature. All temperature points are converted to Kelvin, since that's
412 what all internal formulas rely on.
413  
414 =cut
415  
416 2801 ▶sub __calibration_points {
Subroutine __calibration_points: called CC=1 SCAR=0.0
417 2801 my ( $self, $limit ) = @_;
418  
419 2801 my @calibrations = ( sort { $a <=> $b } keys %{ $self->{__visc_calibration_points} } );
420  
421 2801 my $lowest_temp = $calibrations[0];
422 2801 my $highest_temp = $calibrations[-1];
423  
424 return [
425 2801 [ __c2k($lowest_temp), $self->visc($lowest_temp) ],
426 [ __c2k($highest_temp), $self->visc($highest_temp) ]
427 ];
428 }
429  
430 =head2 __vi_lt_100()
431  
432 Uses algorithm described in B<5. Procedure A> section of ASTM D2270. When you use C<vi()> it invokes either method
433 accordingly.
434  
435 =cut
436  
437 17 ▶sub __vi_lt_100 {
Subroutine __vi_lt_100: called CC=3 SCAR=11.8
438 17 my ($self) = @_;
439  
440 17 my $cst40 = $self->visc(40);
441 16 my $cst100 = $self->visc(100);
442  
443 16 ▶ unless ( $cst40 && $cst100 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cst40 and $cst100) 0 16 16
Condition33%1/3
$cst40 and $cst100
!ll&&!rl&&r
0016
MC/DC0%0/2
$cst40 and $cst100
$cst40 $cst100
Truth table33%1/3
A and B
A= $cst40B= $cst100
ABresult
0X0
100
111
444 0 croak "vi2(): viscosities at 40C and 100C must be known or calculatable";
445 }
446  
447 16 my ( $L, $H ) = $self->LH;
448 16 my $vi = ( ( $L - $cst40 ) / ( $L - $H ) ) * 100;
449  
450 16 return sprintf( "%d", $vi );
451 }
452  
453 =head2 __vi_gt_100()
454  
455 Uses algorithm described in B<6. Procedure B> section of ASTM D2270. When you use C<vi()> it invokes either method
456 accordingly.
457  
458 =cut
459  
460 12 ▶sub __vi_gt_100 {
Subroutine __vi_gt_100: called CC=1 SCAR=0.0
461 12 my ($self) = @_;
462  
463 12 my ( undef, $H ) = $self->LH;
464 12 my $cst40 = $self->visc(40);
465 12 my $cst100 = $self->visc(100);
466  
467 12 my $N = ( log10($H) - log10($cst40) ) / log10($cst100);
468 12 my $vi = ( ( ( 10**$N ) - 1 ) / 0.00715 ) + 100;
469 12 return sprintf( "%d", $vi );
470 }
471  
472 =head1 SEE ALSO
473  
474 L<Lubricants and Lubrication|http://www.amazon.com>, Second Edition by Wiley-VCH
475  
476 =cut
477  
478 1 1;
479  
480 __DATA__
↑ Summary