XPRESS/Sweep/Temperature.pm untested

filter: Statementstmt 0.0% 0 / 179 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 270 CC 27 SCAR 49.4
CC
27
Cov
0%
stabilize
306.0
check_config_paramters
42.0
exit_loop
42.0
CRAP 139.8
1 package Lab::XPRESS::Sweep::Temperature;
2 #ABSTRACT: Temperature sweep
3 $Lab::XPRESS::Sweep::Temperature::VERSION = '3.899';
4 use v5.20;
5  
6 use Lab::XPRESS::Sweep;
7 use Statistics::Descriptive;
8 use Time::HiRes qw/usleep/;
9 use strict;
10  
11 our @ISA = ('Lab::XPRESS::Sweep');
12  
13 sub new {
14 my $proto = shift;
15 my @args = @_;
16 my $class = ref($proto) || $proto;
17  
18 # define default values for the config parameters:
19 my $self->{default_config} = {
20 id => 'Temperature_sweep',
21 filename_extension => 'T=',
22 interval => 1,
23 points => [ 0, 10 ],
24 duration => [1],
25 stepwidth => 1,
26 pid => undef,
27 mode => 'continuous',
28 allowed_instruments => [
29 qw/Lab::Instrument::ITC Lab::Instrument::TCD
30 Lab::Instrument::OI_ITC503 Lab::Instrument::OI_Triton
31 Lab::Instrument::Lakeshore340 Lab::Instrument::TRMC2/,
32 ],
33 allowed_sweep_modes => [ 'continuous', 'step', 'list' ],
34  
35 sensor => undef,
36 stabilize_measurement_interval => 1,
37 stabilize_observation_time => 3 * 60,
38 tolerance_setpoint => 0.2,
39 std_dev_instrument => 0.15,
40 std_dev_sensor => 0.15,
41  
42 max_stabilization_time => undef,
43 setter_args => [],
44 getter_args => [],
45  
46 };
47  
48 # create self from Sweep basic class:
49 $self = $class->SUPER::new( $self->{default_config}, @args );
50 bless( $self, $class );
51  
52 # check and adjust config values if necessary:
53 $self->check_config_paramters();
54  
55 # init mandatory parameters:
56 $self->{DataFile_counter} = 0;
57 $self->{DataFiles} = ();
58  
59 return $self;
60 }
61  
62 sub check_config_paramters {
63 my $self = shift;
64  
65 # No Backsweep allowed; adjust number of Repetitions if Backsweep is 1:
66 if ( $self->{config}->{mode} eq 'continuous' ) {
67 if ( $self->{config}->{backsweep} == 1 ) {
68 $self->{config}->{repetitions} /= 2;
69 $self->{config}->{backsweep} = 0;
70 }
71 }
72  
73 if ( defined $self->{config}->{pid} ) {
74 if ( ref( $self->{config}->{pid} ) eq 'ARRAY' ) {
75 if ( ref( @{ $self->{config}->{pid} }[0] ) ne 'ARRAY' ) {
76 $self->{config}->{pid} = [ $self->{config}->{pid} ];
77 }
78 }
79 else {
80 $self->{config}->{pid} = [ [ $self->{config}->{pid} ] ];
81 }
82 }
83 else {
84 $self->{config}->{pid} = [undef];
85 }
86  
87 # Set loop-Interval to Measurement-Interval:
88 $self->{loop}->{interval} = $self->{config}->{interval};
89  
90 }
91  
92 sub start_continuous_sweep {
93 my $self = shift;
94  
95 print
96 "Stabilize Temperature at upper limit (@{$self->{config}->{points}}[1] K) \n";
97 $self->stabilize( @{ $self->{config}->{points} }[1] );
98  
99 print "Reached upper limit -> start cooling ... \n";
100 $self->{config}->{instrument}->set_heatercontrol('MAN');
101 $self->{config}->{instrument}->set_heateroutput(0);
102 }
103  
104 sub go_to_next_step {
105 my $self = shift;
106  
107 $self->stabilize( @{ $self->{config}->{points} }[ $self->{iterator} ] );
108  
109 }
110  
111 sub exit_loop {
112 my $self = shift;
113  
114 my $TEMPERATURE;
115  
116 if ( $self->{config}->{mode} =~ /step|list/ ) {
117 if (
118 not
119 defined @{ $self->{config}->{points} }[ $self->{iterator} + 1 ] )
120 {
121 return 1;
122 }
123 }
124 elsif ( $self->{config}->{mode} =~ /continuous/ ) {
125 if ( defined $self->{config}->{sensor} ) {
126 $TEMPERATURE = $self->{config}->{sensor}->get_value();
127 }
128 else {
129 $TEMPERATURE = $self->get_value();
130 }
131 if ( $TEMPERATURE < @{ $self->{config}->{points} }[0] ) {
132 return 1;
133 }
134 else {
135 return 0;
136 }
137  
138 }
139  
140 return 0;
141  
142 }
143  
144 sub get_value {
145 my $self = shift;
146 return $self->{config}->{instrument}
147 ->get_value( @{ $self->{config}->{getter_args} } );
148 }
149  
150 sub halt {
151 return shift;
152 }
153  
154 sub stabilize {
155  
156 use Term::ReadKey;
157  
158 my $self = shift;
159 my $setpoint = shift;
160  
161 my $time0 = time();
162  
163 my @T_INSTR;
164 my @T_SENSOR;
165  
166 my @MEDIAN_INSTR;
167 my @MEDIAN_SENSOR;
168  
169 my $MEDIAN_INSTR_MEDIAN = undef;
170 my $INSTR_STD_DEV = undef;
171 my $SENSOR_STD_DEV = undef;
172  
173 my $criterion_setpoint = 0;
174 my $criterion_std_dev_INSTR = 0;
175 my $criterion_std_dev_SENSOR = 1;
176  
177 my $pid = shift @{ $self->{config}->{pid} };
178 if ( defined $pid ) {
179  
180 my $p = shift @{$pid};
181 my $i = shift @{$pid};
182 my $d = shift @{$pid};
183  
184 $self->{config}->{instrument}->set_PID( $p, $i, $d );
185  
186 }
187  
188 $self->{config}->{instrument}->set_heatercontrol('AUTO');
189 $self->{config}->{instrument}
190 ->set_T( $setpoint, @{ $self->{config}->{setter_args} } );
191  
192 local $| = 1;
193  
194 my $time0 = time();
195  
196 print "Stabilize Temperature at $setpoint K ... (Press 'c' to skip)\n\n";
197  
198 #my $line1 = "\rElapsed: $elapsed_time \n Current Temp INSTR: @T_INSTR[-1] \n Current Temp SENSOR: @T_SENSOR[-1] \n ";
199 #my $line2 = "Current Median: @MEDIAN_INSTR[-1] \n Std. Dev. T Instr. : $INSTR_STD_DEV \n Std. Dev. T Sensor : $SENSOR_STD_DEV \n ";
200 #my $line3 = "CRIT SETPOINT: $criterion_setpoint \n CRIT Std. Dev. T Instr. : $criterion_std_dev_INSTR \n CRIT Std. Dev. T Sensor : $criterion_std_dev_SENSOR \n ";
201 print " Time " . " | "
202 . " TEMP " . " | " . "SENS " . " | " . "MED_I" . " | " . "ISD "
203 . " | " . "SSD " . " | " . "C1" . " | " . "C2" . " | " . "C3" . " \n";
204  
205 ReadMode('cbreak');
206 while (1) {
207  
208 #----------COLLECT DATA--------------------
209 my $T_INSTR = $self->get_value();
210 push( @T_INSTR, $T_INSTR );
211  
212 if (
213 scalar @T_INSTR > int(
214 $self->{config}->{stabilize_observation_time}
215 / $self->{config}->{stabilize_measurement_interval}
216 )
217 ) {
218 shift(@T_INSTR);
219  
220 my $stat = Statistics::Descriptive::Full->new();
221 $stat->add_data( \@T_INSTR );
222 my $MEDIAN_INSTR = $stat->median();
223  
224 push( @MEDIAN_INSTR, $MEDIAN_INSTR );
225  
226 if (
227 scalar @MEDIAN_INSTR > int(
228 $self->{config}->{stabilize_observation_time}
229 / $self->{config}->{stabilize_measurement_interval}
230 )
231 ) {
232 shift(@MEDIAN_INSTR);
233 }
234 }
235  
236 if ( defined $self->{config}->{sensor} ) {
237 my $T_SENSOR = $self->{config}->{sensor}->get_value();
238 push( @T_SENSOR, $T_SENSOR );
239  
240 if (
241 scalar @T_SENSOR > int(
242 $self->{config}->{stabilize_observation_time}
243 / $self->{config}->{stabilize_measurement_interval}
244 )
245 ) {
246 shift(@T_SENSOR);
247  
248 my $stat = Statistics::Descriptive::Full->new();
249 $stat->add_data( \@T_SENSOR );
250 my $MEDIAN_SENSOR = $stat->median();
251  
252 push( @MEDIAN_SENSOR, $MEDIAN_SENSOR );
253  
254 if (
255 scalar @MEDIAN_SENSOR > int(
256 $self->{config}->{stabilize_observation_time}
257 / $self->{config}
258 ->{stabilize_measurement_interval}
259 )
260 ) {
261 shift(@MEDIAN_SENSOR);
262 }
263 }
264 }
265  
266 #--------CHECK THE CRITERIONS--------------
267  
268 if ( defined @MEDIAN_INSTR[-1] ) {
269 if (
270 abs( $setpoint - @MEDIAN_INSTR[-1] )
271 < $self->{config}->{tolerance_setpoint} ) {
272 $criterion_setpoint = 1;
273 }
274 else {
275 $criterion_setpoint = 0;
276 }
277 }
278  
279 # if (scalar @MEDIAN_INSTR >= int($self->{config}->{stabilize_observation_time}/$self->{config}->{stabilize_measurement_interval}) - 1) {
280  
281 # my $stat = Statistics::Descriptive::Full->new();
282 # $stat->add_data(\@MEDIAN_INSTR);
283 # $MEDIAN_INSTR_MEDIAN = $stat->median();
284  
285 # if (abs($setpoint - $MEDIAN_INSTR_MEDIAN) < $self->{config}->{tolerance_setpoint}) {
286 # $criterion_setpoint = 1;
287 # }
288 # else {
289 # $criterion_setpoint = 0;
290 # }
291 # }
292  
293 if (
294 scalar @T_INSTR >= int(
295 $self->{config}->{stabilize_observation_time}
296 / $self->{config}->{stabilize_measurement_interval}
297 ) - 1
298 ) {
299 my $stat = Statistics::Descriptive::Full->new();
300 $stat->add_data( \@T_INSTR );
301 $INSTR_STD_DEV = $stat->standard_deviation();
302  
303 if ( $INSTR_STD_DEV < $self->{config}->{std_dev_instrument} ) {
304 $criterion_std_dev_INSTR = 1;
305 }
306 else {
307 $criterion_std_dev_INSTR = 0;
308 }
309 }
310  
311 if ( defined $self->{config}->{sensor} ) {
312 if (
313 scalar @T_SENSOR >= int(
314 $self->{config}->{stabilize_observation_time}
315 / $self->{config}->{stabilize_measurement_interval}
316 ) - 1
317 ) {
318 my $stat = Statistics::Descriptive::Full->new();
319 $stat->add_data( \@T_SENSOR );
320 $SENSOR_STD_DEV = $stat->standard_deviation();
321  
322 if ( $SENSOR_STD_DEV < $self->{config}->{std_dev_sensor} ) {
323 $criterion_std_dev_SENSOR = 1;
324 }
325 else {
326 $criterion_std_dev_SENSOR = 0;
327 }
328 }
329 }
330  
331 my $elapsed_time = $self->convert_time( time() - $time0 );
332  
333 my $output
334 = $elapsed_time . " | " . sprintf( "%3.4f", @T_INSTR[-1] ) . " | "
335  
336 # . sprintf( "%3.3f", @T_SENSOR[-1] ) . " | "
337 # . sprintf( "%3.3f", @MEDIAN_INSTR[-1] ) . " | "
338 . sprintf( "%2.4f", $INSTR_STD_DEV ) . " | "
339  
340 # . sprintf( "%2.3f", $SENSOR_STD_DEV ) . " | "
341 . $criterion_setpoint . " | "
342 . $criterion_std_dev_INSTR . " | "
343 . $criterion_std_dev_SENSOR;
344  
345 print $output;
346  
347 if ( $criterion_std_dev_INSTR
348 * $criterion_std_dev_SENSOR
349 * $criterion_setpoint ) {
350 last;
351 }
352 elsif (
353 defined $self->{config}->{max_stabilization_time}
354 and ( ( time() - $time0 )
355 >= $self->{config}->{max_stabilization_time} )
356 ) {
357 last;
358 print "\n";
359 }
360  
361 else {
362  
363 }
364  
365 my $char = ReadKey(1e-5);
366 if ( defined $char and $char eq 'c' ) {
367 last;
368 }
369 sleep( $self->{config}->{stabilize_measurement_interval} );
370  
371 print "\r";
372  
373 }
374 ReadMode('normal');
375 $| = 0;
376  
377 print "\nTemperature stabilized at $setpoint K \n";
378 }
379  
380 sub convert_time {
381 my $self = shift;
382  
383 my $time = shift;
384 my $days = int( $time / 86400 );
385 $time -= ( $days * 86400 );
386 my $hours = int( $time / 3600 );
387 $time -= ( $hours * 3600 );
388 my $minutes = int( $time / 60 );
389 my $seconds = $time % 60;
390  
391 $time
392 = sprintf( "%02dh", $hours )
393 . sprintf( "%02dm", $minutes )
394 . sprintf( "%02ds", $seconds );
395 return $time;
396 }
397  
398 1;
399  
400 __END__