This repository was archived by the owner on Oct 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStimulus.pm
529 lines (462 loc) · 18.7 KB
/
Stimulus.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#-#####################################################################################
#- File: Stimulus.pm
#- Synopsys: Functions for stimulating network.
#-#####################################################################################
#- Detailed Description:
#- ---------------------
#-
#-#####################################################################################
#######################################################################################
# TO-DO LIST
#######################################################################################
#######################################################################################
# Package interface
#######################################################################################
package Stimulus;
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(
clamping_equation
impulse_equation
wash_equation
staircase_equation
staircase_sample
ramp_equation
ss_ramp_equation
rand_ss_ramp_equation
);
use Utils;
use Globals;
#--------------------------------------------------------------------------------------
# Function: clamping_equation
# Synopsys: Generate a source/sink equation pair to clamp a node at a given concentration
#--------------------------------------------------------------------------------------
sub clamping_equation {
my %args = (
# default values
NODE => undef,
PERIOD => undef,
DELAY => 0,
STRENGTH => undef,
CONCENTRATION => undef,
DUTY => 100,
@_, # argument pair list overwrites defaults
);
$args{PERIOD} = -1 if ($args{DUTY} == 100);
check_args(\%args, 6);
my $node = $args{NODE};
my $period = $args{PERIOD};
my $delay = $args{DELAY};
my $strength = $args{STRENGTH};
my $concentration = $args{CONCENTRATION};
my $duty = $args{DUTY};
if ($duty < 100) {
return ("null -> $node; clamp_source_$node=\"0.5*$concentration*$strength*(square(2*pi*(t-$delay)/$period, $duty)+1)\"",
"$node -> null; clamp_sink_$node=$strength");
} else {
return ("null -> $node; clamp_source_$node=".$concentration*$strength,
"$node -> null; clamp_sink_$node=$strength");
}
}
#--------------------------------------------------------------------------------------
# Function: impulse_equation
# Synopsys:
#--------------------------------------------------------------------------------------
sub impulse_equation {
my %args = (
# default values
NODE => undef,
PERIOD => undef,
DELAY => undef,
CONCENTRATION => undef,
IMPULSE_LENGTH => undef,
@_, # argument pair list overwrites defaults
);
check_args(\%args, 5);
my $node = $args{NODE};
my $period = $args{PERIOD};
my $delay = $args{DELAY};
my $concentration = $args{CONCENTRATION};
my $impulse_length = $args{IMPULSE_LENGTH};
if ($delay + $impulse_length > $period) {
printn "impulse_equation: ERROR -- impulse period wrap-around";
exit(1);
}
my $level = $concentration/$impulse_length;
my $duty = $impulse_length/$period*100;
my $leading_edge = $delay;
my $trailing_edge = $delay + $impulse_length;
return {
equations => [
"null -> $node; impulse_source_$node=\"0.5*$level*(square(2*pi*(t-$delay)/$period, $duty)+1)\"",
],
events => [$leading_edge, $trailing_edge],
values => [],
};
}
#--------------------------------------------------------------------------------------
# Function: wash_equation
# Synopsys:
#--------------------------------------------------------------------------------------
sub wash_equation {
my %args = (
# default values
NODE => undef,
PERIOD => -1,
DELAY => 0,
STRENGTH => undef,
WASH_LENGTH => undef,
SINK_NODE => "null",
@_, # argument pair list overwrites defaults
);
$args{DELAY} = -1 if ($args{PERIOD} == -1);
$args{WASH_LENGTH} = -1 if ($args{PERIOD} == -1);
check_args(\%args, 6);
my $node = $args{NODE};
my $sink_node = $args{SINK_NODE};
my $period = $args{PERIOD};
my $delay = $args{DELAY};
my $strength = $args{STRENGTH};
my $wash_length = $args{WASH_LENGTH};
my $duty;
if ($period != -1) {
if ($delay + $wash_length > $period) {
printn "wash_equation: ERROR -- wash period wrap-around";
exit;
}
$duty = $wash_length/$period*100;
return ("$node -> $sink_node; wash_sink_$node=\"0.5*$strength*(square(2*pi*(t-$delay)/$period, $duty)+1)\"");
} else {
return ("$node -> $sink_node; wash_sink_$node=$strength");
}
}
#--------------------------------------------------------------------------------------
# Function: staircase_equation
# Synopsys: Generate a source/sink equation pair staircase-style ramp up and down.
# DUTY indicates time spent at high level (incl. rise/fall times), RFTIME
# gives the rise/fall times, and STEPS gives how many steps used when changing.
# Also returns a list of event times where step function changes
# (first period only), and the expected steady-state value of the function
# right BEFORE the event.
# The ramp_equation will touch at the bottom/top corners of the corresponding
# staircase function when rising/falling.
#--------------------------------------------------------------------------------------
sub staircase_equation {
my %args = (
# default values
NODE => undef,
PERIOD => undef,
DELAY => 0,
STRENGTH => undef,
CONCENTRATION => undef,
DUTY => 50,
RFTIME => undef,
STEPS => undef, # of steps in each direction
@_, # argument pair list overwrites defaults
);
check_args(\%args, 8);
my $node = $args{NODE};
my $period = $args{PERIOD};
my $delay = $args{DELAY};
my $strength = $args{STRENGTH};
my $concentration = $args{CONCENTRATION};
my $duty = $args{DUTY};
my $rftime = $args{RFTIME};
my $steps = $args{STEPS};
my $pulse_width = $duty * $period / 100.0;
my $square_width = $pulse_width - $rftime;
my $square_duty = ($square_width) / $period * 100.0;
if ($steps < 1) {
printn "ERROR: staircase_equation -- step parameter must be >= 1";
exit(1);
}
if ($pulse_width < 2 * $rftime) {
printn "ERROR: staircase_equation -- parameters are inconsistent (rise/fall time exceeds computed pulse width)";
exit(1);
}
if ($square_duty <= 0) {
printn "ERROR: staircase_equation -- parameters are inconsistent (rise/fall time too large?)";
exit(1);
}
if (($steps < 2) && ($rftime > 0)) {
printn "ERROR: staircase_equation -- parameters are inconsistent (cannot implement non-zero rise/fall time with indicated # of steps)";
exit(1);
}
my $square_delay = ($steps < 2) ? 0 : $rftime / ($steps);
my @stair_source_nodes;
my @leading_edge_events;
my @trailing_edge_events;
my @leading_edge_values;
my @trailing_edge_values;
for (my $i=0; $i < $steps; $i++) {
my $shift = $i * $square_delay + $delay;
push @leading_edge_events, $shift;
push @leading_edge_values, $i / $steps * $concentration;
push @trailing_edge_events, $shift + $square_width;
push @trailing_edge_values, $concentration * ($steps - $i) / $steps;
# to prevent roundoff error, multiply by pi as very last step, else
# we may get **old** value at event time instead of the new value
push @stair_source_nodes, "0.5*$concentration*$strength*(1 + square((t-$shift)/$period*2*pi, $square_duty))";
}
my $stair_source_node = "(".join(" + ", @stair_source_nodes).")/$steps";
my @events = (@leading_edge_events, @trailing_edge_events);
# only events in the first period are included here. also, if the delay offset
# is too large, some events will fall outside the period, so we must use
# a modulus operator to fix this. we also use Utils::fmod which can handle
# fractional dividends
@events = map {fmod($_,$period)} @events;
my @order = sort {$events[$a] <=> $events[$b]} (0..$#events);
@order = grep {$events[$_] != 0} @order; # don't include 0.0 as an event
@events = map {$events[$_]} @order;
my @values = (@leading_edge_values, @trailing_edge_values);
@values = map {$values[$_]} @order;
printn "staircase_equation: event list is @events" if $verbosity >= 3;
printn "staircase_equation: value list is @values" if $verbosity >= 3;
if ($duty < 100) {
# final value is value at last event +/- one step
my $final_value = $values[@values-1];
$final_value += (
fmod(($period - $delay),$period) <= ($pulse_width - $rftime) &&
fmod(($period - $delay), $period) > 0 ?
($concentration/$steps) :
(-$concentration/$steps)
);
return {
equations => [
"null -> $node; clamp_source_$node=\"$stair_source_node\"",
"$node -> null; clamp_sink_$node=$strength",
],
events => \@events,
values => \@values,
final_value => $final_value,
};
} else {
return {
equations => [
"null -> $node; clamp_source_$node=".$concentration*$strength,
"$node -> null; clamp_sink_$node=$strength",
],
events => [],
values => [],
final_value => $concentration,
};
}
}
#--------------------------------------------------------------------------------------
# Function: staircase_sample
# Synopsys: Call staircase_equation with dummy values
#--------------------------------------------------------------------------------------
sub staircase_sample {
my %args = (
# default values
CONCENTRATION => undef,
PERIOD => undef,
DELAY => 0,
DUTY => 50,
RFTIME => undef,
STEPS => undef, # of steps in each direction
@_, # argument pair list overwrites defaults
);
check_args(\%args, 6);
my $staircase_ref = staircase_equation(
NODE => "DUMMY",
STRENGTH => 1.0,
%args,
);
return {
events => $staircase_ref->{events},
values => $staircase_ref->{values},
final_value => $staircase_ref->{final_value},
}
}
#--------------------------------------------------------------------------------------
# Function: ramp_equation
# Synopsys: Generate a source/sink equation pair to generate linear ramp up and down.
# Arguments and return values are identical to staircase_equation
# for compatibility, even though some arguments are not applicable.
#--------------------------------------------------------------------------------------
sub ramp_equation {
my %args = (
# default values
NODE => undef,
PERIOD => undef,
DELAY => 0,
STRENGTH => undef,
CONCENTRATION => undef,
DUTY => 50,
RFTIME => undef,
STEPS => undef, # N/A
@_, # argument pair list overwrites defaults
);
check_args(\%args, 8);
my $node = $args{NODE};
my $period = $args{PERIOD};
my $delay = $args{DELAY};
my $strength = $args{STRENGTH};
my $concentration = $args{CONCENTRATION};
my $duty = $args{DUTY};
my $rftime = $args{RFTIME};
my $steps = $args{STEPS};
my $pulse_width = $duty * $period / 100.0;
my $ramp_duty = ($rftime) / $period * 100.0;
my $square_duty = ($pulse_width - 2*$rftime) / $period * 100.0;
if ($pulse_width < 2 * $rftime) {
printn "ERROR: ramp_equation -- parameters are inconsistent (rise/fall time exceeds computed pulse width)";
exit(1);
}
# n.b. in the square() function, very important to *(pi) as very last step, else roundoff error introduced messes up the square()
# function interval, such that square(falling edge time) = +1 instead of -1 --> this introduces an extra discontinuity!!
# i.e. at event time, we will add old value of leading square with new value of lagging square, and this gives a spike with twice
# the intended value at (and only at) the event time!!!
my $ramp_source_node = "(";
my $rise_ramp_delay = $delay;
my $fall_ramp_delay = $delay + $pulse_width - $rftime;
$ramp_source_node .= "0.5*(mod(t,$period)-$rise_ramp_delay)/$rftime*$concentration*$strength*(1 + square((t-$rise_ramp_delay)/$period*2*pi, $ramp_duty))";
$ramp_source_node .= "+ 0.5*$concentration*$strength*(1 + square((t-$rise_ramp_delay-$rftime)/$period*2*pi, $square_duty))";
$ramp_source_node .= "+ 0.5*(-mod(t,$period)+$fall_ramp_delay+$rftime)/$rftime*$concentration*$strength*(1 + square((t-$fall_ramp_delay)/$period*2*pi, $ramp_duty))";
$ramp_source_node .= ")";
my @events;
if ($delay + $rftime != $fall_ramp_delay) {
@events= ($delay, $delay + $rftime, $fall_ramp_delay, $fall_ramp_delay + $rftime);
} else {
@events= ($delay, $delay + $rftime, $fall_ramp_delay + $rftime);
}
# only events in the first period are included here. also, if the delay offset
# is too large, some events will fall outside the period, so we must use
# a modulus operator to fix this. we also use Utils::fmod which can handle
# fractional dividends
@events = map {fmod($_, $period)} @events;
my @order = sort {$events[$a] <=> $events[$b]} (0..$#events);
@order = grep {$events[$_] != 0} @order; # don't include 0.0 as an event
@events = map {$events[$_]} @order;
my @values = (0, $concentration, $concentration, 0);
@values = map {$values[$_]} @order;
printn "ramp_equation: event list is @events" if $verbosity >= 3;
printn "ramp_equation: value list is @values" if $verbosity >= 3;
if ($duty < 100) {
return {
equations => [
"null -> $node; clamp_source_$node=\"$ramp_source_node\"",
"$node -> null; clamp_sink_$node=$strength",
],
events => \@events,
values => \@values,
}
} else {
return {
equations => [
"null -> $node; clamp_source_$node=".$concentration*$strength,
"$node -> null; clamp_sink_$node=$strength"
],
events => [],
values => [],
};
}
}
#--------------------------------------------------------------------------------------
# Function: ss_ramp_equation
# Synopsys: Generate a non-periodic source/sink equation pair to generate linear ramp
# up and down over several steps. The system is brought to steady-state at
# each step.
#--------------------------------------------------------------------------------------
sub ss_ramp_equation {
my %args = (
# default values
NODE => undef,
DELAY => "~", # default is to wait for steady-state before applying stimulus
STRENGTH => undef,
RANGE => undef,
STEPS => undef,
RAMP_TIME => undef,
@_, # argument pair list overwrites defaults
);
check_args(\%args, 6);
my $node = $args{NODE};
my $delay = $args{DELAY};
my $strength = $args{STRENGTH};
my $range = $args{RANGE};
my $steps = $args{STEPS};
my $ramp_time = $args{RAMP_TIME};
my $step_size = $range / $steps; # the concentration changing size
my $step_time = $ramp_time / $steps;
my @events = ($delay, map {"~"} (1..2*$steps));
my @values = (0);
push @values, map {$_*$step_size} (1..$steps);
push @values, map {($steps-$_)*$step_size} (1..$steps);
my $ramp_source_node = "(";
for (my $i=0; $i < (@events-1)/2; $i++) {
my $ii = $i + 1;
my $jj = $i + 1 + (@events-1)/2;
$ramp_source_node .= "+(event_flags($ii) && ~event_flags($jj))*min((t-event_times($ii))/$step_time, 1)*$step_size*$strength";
$ramp_source_node .= "+event_flags($jj)*max(1-(t-event_times($jj))/$step_time, 0)*$step_size*$strength";
}
$ramp_source_node .= ")";
printn "ramp_equation: event list is @events" if $verbosity >= 3;
printn "ramp_equation: value list is @values" if $verbosity >= 3;
return {
equations => [
"null -> $node; clamp_source_$node=\"$ramp_source_node\"",
"$node -> null; clamp_sink_$node=$strength",
],
events => \@events,
values => \@values,
}
}
#--------------------------------------------------------------------------------------
# Function: rand_ss_ramp_equation
# Synopsys: Generate a non-periodic source/sink equation pair to generate linear ramp
# up and down over several steps but with random step size.
# The system is brought to steady-state at each step.
#--------------------------------------------------------------------------------------
sub rand_ss_ramp_equation {
my %args = (
# default values
NODE => undef,
DELAY => "~", # default is to wait for steady-state before applying stimulus
STRENGTH => undef,
STEPS => undef,
RAMP_TIME => undef,
MIN => 1,
MAX => 100,
@_, # argument pair list overwrites defaults
);
check_args(\%args, 7);
my $node = $args{NODE};
my $delay = $args{DELAY};
my $strength = $args{STRENGTH};
my $steps = $args{STEPS};
my $ramp_time = $args{RAMP_TIME};
my $lg_min = $args{MIN};
my $lg_max = $args{MAX};
my $step_size_exp = ((log($lg_max)-log($lg_min))/log(10)/($steps-1));
my $step_time = $ramp_time / $steps || 1;
my $step_size = $lg_min;
my @events = ($delay, map {"~"} (1..2*$steps));
my @values = ();
for (my $i=0; $i < $steps; $i++) {
$step_size = $lg_min * 10 ** ($step_size_exp*($i-1)) + rand() * (9/10) * ($lg_min * 10 ** ($step_size_exp*($i)));
push @values, $step_size;
}
my $ramp_source_node = "(";
for (my $i=0; $i < $steps; $i++) {
my $ii = 2 * $i + 1;
my $jj = $ii + 1;
my $jj_pre = $jj - 1;
my $amp = $values[$i];
$ramp_source_node .= "+(event_flags($ii) && ~event_flags($jj))*min((t-event_times($ii))/$step_time, 1)*($amp)*$strength";
$ramp_source_node .= "+event_flags($jj)*max(1-(t-event_times($jj))/$step_time, 0)*($amp)*$strength";
}
$ramp_source_node .= ")";
printn "ramp_equation: event list is @events" if $verbosity >= 3;
printn "ramp_equation: value list is @values" if $verbosity >= 3;
return {
equations => [
"null -> $node; clamp_source_$node=\"$ramp_source_node\"",
"$node -> null; clamp_sink_$node=$strength",
],
events => \@events,
values => \@values,
}
}