This repository has been archived by the owner on Jun 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
WebAtoms.pm
586 lines (496 loc) · 16.9 KB
/
WebAtoms.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#### ===============================================================
#### This causes /neither/ Larch /nor/ Ifeffit to be loaded
#### also forces Demeter to be in web mode very early in startup
BEGIN {
$ENV{DEMETER_NO_BACKEND} = 1;
$ENV{DEMETER_MODE} = 'web';
}
#### ===============================================================
#### ===============================================================
#### This is used to capture error messages from Xray::Crystal::Cell
#### and Demeter::Atoms in a way that can be displayed in the response
#### box of WebAtoms
BEGIN {
use Demeter qw(:atoms :ui=web);
}
package Xray::Crystal::Cell;
no warnings 'once';
sub carp {
$WebAtoms::warning_messages .= $_[0];
};
package Demeter::Atoms;
no warnings 'once';
sub carp {
$WebAtoms::warning_messages .= $_[0];
};
#### ===============================================================
package WebAtoms;
use Dancer ':syntax';
use Demeter::Constants qw($NUMBER);
use Demeter::StrTypes qw( Element );
use Chemistry::Elements qw(get_symbol get_Z);
use File::Basename;
use File::Copy;
use List::Util qw(max);
use List::MoreUtils; # not importing "any" to avoid collision with Dancer's "any"
use Safe;
use HTTP::Tiny;
$SIG{__WARN__} = sub {accumulate($_[0])};
our $VERSION = '1';
our $atoms = Demeter::Atoms->new;
our $warning_messages = q{};
our $output = 'feff';
our $fversion = 8;
$atoms->feff_version($fversion);
our $maxsites = 4;
get '/' => sub {
my $problems = q{};
my $response = q{};
my $e = [];
my $x = [];
my $y = [];
my $z = [];
my $t = [];
my $nsites = $maxsites;
my $icore = 0;
my $feffv = q{};
my $add = param('add');
my $reset = param('reset');
my $compute = param('co');
my $file = param('file');
my $url = param('url');
my $form = param('form');
my $hashref = params;
my $nparams = keys(%$hashref);
# user just uploaded a file or fetched a URL
if (defined($file) and $file) {
$nsites = $#{$atoms->sites};
$nsites = 4 if $nsites == -1;
foreach my $i (0 .. $nsites) { # need to jigger sites into the form the template expects
if ($atoms->sites->[$i]) {
($e->[$i], $x->[$i], $y->[$i], $z->[$i], $t->[$i]) = split(/\|/, $atoms->sites->[$i]);
} else {
($e->[$i], $x->[$i], $y->[$i], $z->[$i], $t->[$i]) = (q{},0,0,0,q{});
}
};
# user supplied a URL
} elsif (defined($url)) {
$file = fetch_url($url); # URL copied to local upload directory
if (not $file) {
$problems = "- Unable to download " . param('urlfail') . " or file is not an atoms.inp file\n";
};
$nsites = $#{$atoms->sites};
$nsites = 4 if $nsites == -1;
foreach my $i (0 .. $nsites) { # need to jigger sites into the form the template expects
if ($atoms->sites->[$i]) {
($e->[$i], $x->[$i], $y->[$i], $z->[$i], $t->[$i]) = split(/\|/, $atoms->sites->[$i]);
} else {
($e->[$i], $x->[$i], $y->[$i], $z->[$i], $t->[$i]) = (q{},0,0,0,q{});
}
};
# user just tried to read an unreadable/unusable local file or URL
} elsif (defined(param('urlfail'))) {
$problems = "- Unable to download " . param('urlfail') . " or file is not an atoms.inp file\n";
# user just clicked on the Reset button
} elsif (defined($reset)) {
$atoms->clear;
$atoms->feff_version($fversion);
# user just clicked on the 'Add a site' button
} elsif (defined($add)) {
$nsites = $#{$atoms->sites};
foreach my $i (0 .. $nsites) { # need to jigger sites into the form the template expects
if ($atoms->sites->[$i]) {
($e->[$i], $x->[$i], $y->[$i], $z->[$i], $t->[$i]) = split(/\|/, $atoms->sites->[$i]);
} else {
($e->[$i], $x->[$i], $y->[$i], $z->[$i], $t->[$i]) = (q{},0,0,0,q{});
}
};
# the form contents just got posted and we are redirected here
} elsif (defined($form)) {
$problems = $atoms->message_buffer;
$nsites = $#{$atoms->sites};
$nsites = 4 if $nsites == -1;
($e, $x, $y, $z, $t)= ([], [], [], [], []);
foreach my $i (0 .. $nsites) { # need to jigger sites into the form the template expects
if ($atoms->sites->[$i]) {
($e->[$i], $x->[$i], $y->[$i], $z->[$i], $t->[$i]) = split(/\|/, $atoms->sites->[$i]);
} else {
($e->[$i], $x->[$i], $y->[$i], $z->[$i], $t->[$i]) = (q{},0,0,0,q{});
}
};
$file ||= join('-', List::MoreUtils::uniq(@$e));
$file =~ s{\-+\z}{};
} else {
$atoms->clear;
$atoms->feff_version($fversion);
};
## begin collecting error text as needed
my $additional = q{};
if ($atoms->cell->group->is_first and $#{$atoms->cell->group->shiftvec} > -1) {
my $vec = sprintf("(%s, %s, %s)", @{$atoms->cell->group->shiftvec});
$additional = "
This space group symbol identifies the first standard setting. You may need to
use a shift vector of
$vec
If you see multiply occupied positions or if the coordination seems wrong, you
should try using that shift vector.
";
};
## begin constructing the response text according to the current state of things
# problems found while reading the crystal data
if ($problems) {
$response = $problems;
$output = "trouble";
# adding a site or resetting, response should be blank
} elsif (defined($add) or defined($reset)) {
$response = q{};
# response text contains problems found while processing crystal data
} elsif ($atoms->cell->group->warning) {
$response = $atoms->cell->group->warning;
$output = "trouble";
# the user has asked to see the diagnostic text
} elsif ($output eq 'object') {
$response = $atoms->serialization;
$additional .= $warning_messages;
$additional = join("\n", map {' *!!! ' . $_} (split(/\n/, $additional))) . "\n\n" if ($additional !~ m{\A\s*\z});
$response = $additional . $response;
$warning_messages = q{};
# the normal state of things -- post the feff.inp file or other output
} elsif ($#{$atoms->sites} > -1) {
$feffv = ($output eq 'feff') ? $output.$fversion : $output;
$response = $atoms->Write($feffv);
$additional .= $warning_messages;
$additional = join("\n", map {' *!!! ' . $_} (split(/\n/, $additional))) . "\n\n" if ($additional !~ m{\A\s*\z});
$response = $additional . $response;
$warning_messages = q{};
# the user is visiting the page for the first time
} else {
$response = $atoms->message_buffer;
$atoms->message_buffer(q{});
##$response = $atoms.$/;
};
## an additional site was requested
$nsites = $#{$atoms->sites}+1;
$nsites = 4 if $nsites < 4;
if ($add) {
++$nsites;
++$maxsites;
push @$e, 'H';
push @$x, 0;
push @$y, 0;
push @$z, 0;
push @$t, q{};
};
## figure out the default file name for saving the response
#my $style = $atoms->feff_version . $atoms->ipot_style;
my $style = '8' . $atoms->ipot_style;
my $outfile = $output;
if ($output =~ m{atoms|feff|p1}) {
$outfile .= '.inp';
#} elsif ($output =~ m{trouble}) {
# $outfile .= '.txt';
} else {
$outfile .= '.txt';
};
## make the title text for the generated web page look pretty
if (defined($file) and ($file !~ m{\A\s*\z})) {
$file = ' - ' . $file;
};
## make sure the correct site is selected as the absorber
foreach my $i (0 .. $#{$e}) {
$icore = $i if (defined(lc($t->[$i])) and defined($atoms->core) and (lc($t->[$i]) eq lc($atoms->core)) or
defined(lc($t->[$i])) and defined($atoms->core) and (lc($e->[$i]) eq lc($atoms->core)));
};
$icore = 0 if ((not defined($e->[$icore])) or ($e->[$icore] =~ m{\A\s*\z}));
#####################
# post the new page #
#####################
template 'index', {dversion => $Demeter::VERSION,
waversion => $VERSION,
nsites => max($nsites,$maxsites),
space => $atoms->space,
a => $atoms->a,
b => $atoms->b,
c => $atoms->c,
alpha => $atoms->alpha,
beta => $atoms->beta,
gamma => $atoms->gamma,
rclus => $atoms->rmax,
rmax => $atoms->rpath,
rscf => $atoms->rscf,
shift_x => $atoms->shiftvec->[0],
shift_y => $atoms->shiftvec->[1],
shift_z => $atoms->shiftvec->[2],
edge => $atoms->edge,
style => $style,
icore => $icore,
e => $e,
x => $x,
y => $y,
z => $z,
t => $t,
file => $file,
outfile => $outfile,
response => $response,
};
};
post '/atomsinp' => sub {
my $e = [];
my $x = [];
my $y = [];
my $z = [];
my $t = [];
my $nsites = 4;
my $feffv = q{};
my $problems = q{};
$atoms->message_buffer(q{});
#####################################
# retrieve values from the web page #
#####################################
## lattice constants, numbers
my $a = param('a') || 0;
my $b = param('b') || 0;
my $c = param('c') || 0;
my $alpha = param('al') || 90;
my $beta = param('be') || 90;
my $gamma = param('ga') || 90;
my @shift = (param('sx')||0, param('sy')||0, param('sz')||0);
## radii, numbers
my $rclus = param('rc') || 9;
my $rmax = param('rm') || 5;
my $rscf = param('rs') || 4;
my $space = param('sp') || q{};
## string options
my $edge = param('ed') || 'k';
my $style = param('st') || '8elements';
my ($v, $s) = (8, 'elements');
if ($style =~ m{\A([68])(elements|sites|tags)}i) {
($v, $s) = ($1, $2);
};
$output = param('ou') || 'feff';
$fversion = $v;
$atoms->clear;
$atoms->feff_version($fversion);
##################################################
# sanitize the values retrived from the web page #
##################################################
my ($val, $p);
## lattics constants, numbers
($val, $p) = check_number($b, 0, 'Lattice constant b', 0);
$problems .= $p;
$atoms->b($val) if $val;
($val, $p) = check_number($c, 0, 'Lattice constant c', 0);
$problems .= $p;
$atoms->c($val) if $val;
($val, $p) = check_number($a, 0, 'Lattice constant a', 0);
$problems .= $p;
$atoms->a($val);
$atoms->b($atoms->a) if ($atoms->b == 0);
$atoms->c($atoms->a) if ($atoms->c == 0);
($val, $p) = check_number($beta, 90, 'Angle beta', 0);
$problems .= $p;
$atoms->beta($val);
($val, $p) = check_number($gamma, 90, 'Angle gamma', 0);
$problems .= $p;
$atoms->gamma($val);
($val, $p) = check_number($alpha, 90, 'Angle alpha', 0);
$problems .= $p;
$atoms->alpha($val);
$atoms->beta( $atoms->alpha) if ($atoms->beta == 0);
$atoms->gamma($atoms->alpha) if ($atoms->gamma == 0);
## shift vector, numbers, must be interpreted e.g. 1/3 -> 0.33333
@shift = map {_interpret($_)} @shift;
($val, $p) = check_number($shift[0], 0, 'Shift vector X coordinate', 1);
$problems .= $p;
$shift[0] = $val;
($val, $p) = check_number($shift[1], 0, 'Shift vector Y coordinate', 1);
$problems .= $p;
$shift[1] = $val;
($val, $p) = check_number($shift[2], 0, 'Shift vector Z coordinate', 1);
$problems .= $p;
$shift[2] = $val;
$atoms->shiftvec(\@shift);
## radii, numbers
($val, $p) = check_number($rclus, 9, 'Cluster size', 0);
$problems .= $p;
$atoms->rmax($val) if $val;
($val, $p) = check_number($rmax, 5, 'Longest path length', 0);
$problems .= $p;
$atoms->rpath($val) if $val;
($val, $p) = check_number($rscf, 4, 'Self consistency radius', 0);
$problems .= $p;
$atoms->rscf($val) if $val;
## lists, strings
if (List::MoreUtils::any {lc($edge) eq $_} qw(k l1 l2 l3)) {
$atoms->edge($edge);
} else {
$problems .= "- Edge is not one of K, L1, L2, or L3 (was $edge)\n";
};
if (List::MoreUtils::any {lc($s) eq $_} qw(elements tags sites)) {
$atoms->ipot_style($s);
$atoms->feff_version($v);
} else {
$problems .= "- Style is not one of elements, tags, or sites (was $s)\n";
};
$atoms->space($space) if defined $space;
$atoms->cell->space_group($space); # why is this necessary!!!!! why is the trigger not being triggered?????
$problems .= sprintf("- %s (was %s)\n", $atoms->cell->group->warning, $space) if $atoms->cell->group->warning;
########################################
# retrieve and sanitize the atoms list #
########################################
my $count = 50; # try to figure out from the form data how many sites are defined
while ($count > -1) {
if (defined(param('e'.$count))) {
$nsites = $count+1;
last;
};
--$count;
};
$atoms->clear_sites;
my $core = param('core') || 0;
foreach my $i (0 .. $nsites-1) {
my $site_problems = q{};
$e->[$i] = param('e'.$i) || q{};
$x->[$i] = param('x'.$i) || 0;
$y->[$i] = param('y'.$i) || 0;
$z->[$i] = param('z'.$i) || 0;
$t->[$i] = param('t'.$i) || param('e'.$i) || q{};
$x->[$i] = _interpret($x->[$i]);
$y->[$i] = _interpret($y->[$i]);
$z->[$i] = _interpret($z->[$i]);
if ($e->[$i] and (not is_Element(get_symbol($e->[$i])))) {
$site_problems .= sprintf("- Symbol for site %d is not a valid element symbol (was $e->[$i])\n", $i+1);
}
;
($val, $p) = check_number($x->[$i], 0, sprintf("x coordinate for site %d", $i+1), 1);
$site_problems .= $p;
$x->[$i] = $val;
($val, $p) = check_number($y->[$i], 0, sprintf("y coordinate for site %d", $i+1), 1);
$site_problems .= $p;
$y->[$i] = $val;
($val, $p) = check_number($z->[$i], 0, sprintf("z coordinate for site %d", $i+1), 1);
$site_problems .= $p;
$z->[$i] = $val;
$e->[$i] = get_symbol($e->[$i]);
if ($site_problems) {
$problems .= $site_problems;
} elsif (is_Element($e->[$i])) {
my $this = join("|",$e->[$i], $x->[$i], $y->[$i], $z->[$i], $t->[$i]);
$atoms->push_sites($this);
if ($i == $core) {
$atoms->core($t->[$i]);
};
};
};
if ($atoms->core =~ m{\A\s*\z}) {
foreach my $i (0 .. $nsites-1) {
if ($t->[$i] !~ m{\A\s*\z}) {
$atoms->core($t->[$i]);
last;
};
};
};
$problems .= " - You have not specified a space group symbol.\n" if ($space =~ m{\A\s*\z});
$problems .= " - You have not specified lattice constants.\n" if ($atoms->a == 0);
$atoms->message_buffer($problems);
redirect '/?form=1';
};
#####################################################################################################
# localfile route, read the provided filename, load data into an Atoms object, reroute to main page #
# example found at http://perlmaven.com/uploading-files-with-dancer2 #
#####################################################################################################
post '/fromdisk' => sub {
my $data = request->upload('lfile');
if (not $data) {
redirect '/';
return;
};
## make sure the uploads directory exists
#my $dir = path(config->{appdir}, 'uploads');
#mkdir $dir if not -e $dir;
my $dir = File::Spec->tmpdir();
## copy the file to the server's disk space
my $path = path($dir, $data->basename);
$data->link_to($path);
$atoms->clear;
$atoms->feff_version($fversion);
if ($path =~ m{cif\z}i) {
$atoms->cif($path);
} else {
if (Demeter->is_atoms($path)) {
$atoms->file($path);
} else {
unlink $path;
redirect '/?urlfail='.$data->basename;
};
};
## redirect with the name of the file in the server's diskspace
unlink $path;
redirect '/?file='.$data->basename;
};
######################################################
# this is the workhorse for the fetch and url routes #
######################################################
sub fetch_url {
my ($url) = @_;
## fetch the content provided by the URL
my $payload;
my $response = HTTP::Tiny->new->get($url);
if ($response->{success}) {
$payload = $response->{content};
} else {
return 0;
};
my $file = basename($url);
## make sure the uploads directory exists
#my $dir = path(config->{appdir}, 'uploads');
#mkdir $dir if not -e $dir;
my $dir = File::Spec->tmpdir();
## write the URL content to a local file
my $path = path($dir, $file);
open(my $I, '>', $path);
print $I $payload;
close $I;
## import the URL content into a Demeter::Atoms object
$atoms->clear;
$atoms->feff_version($fversion);
if ($url =~ m{cif\z}i) {
$atoms->cif($path);
} else {
if (Demeter->is_atoms($path)) {
$atoms->file($path);
} else {
return 0;
};
};
## clean up and done
unlink $path;
return $file;
};
## callback for $SIG{__WARN__}
sub accumulate {
$warning_messages .= $_[0];
};
## safely evaluate a "number" like 1/2 into a float
sub _interpret {
my ($str) = @_;
my $cpt = new Safe;
my $retval = $cpt->reval($str);
return $retval;
};
## verify that $number can be interpreted as an integer or a float,
## optionally checking if it is negative, returning a sensible default
## if not a number
sub check_number {
my ($number, $default, $prefix, $negok) = @_;
my $problem = q{};
if ($number !~ m{\A$NUMBER\z}) {
$problem = " - $prefix was not a number (was $number)\n";
} elsif (($number < 0) and (not $negok)) {
$problem .= " - $prefix was negative (was $number)\n";
} else {
$default = $number;
};
return ($default, $problem);
};
true;