File Coverage

blib/lib/HackaMol/X/Calculator.pm
Criterion Covered Total %
statement 40 40 100.0
branch 16 16 100.0
condition n/a
subroutine 12 12 100.0
pod 0 2 0.0
total 68 70 97.1


line stmt bran cond sub pod time code
1             package HackaMol::X::Calculator;
2             $HackaMol::X::Calculator::VERSION = '0.012';
3             # ABSTRACT: Abstract calculator class for HackaMol
4 2     2   266567 use 5.008;
  2         5  
  2         61  
5 2     2   994 use Moose;
  2         489654  
  2         14  
6 2     2   11918 use MooseX::StrictConstructor;
  2         35395  
  2         8  
7 2     2   13623 use Moose::Util::TypeConstraints;
  2         3  
  2         16  
8 2     2   3014 use namespace::autoclean;
  2         3  
  2         11  
9 2     2   151 use Carp;
  2         4  
  2         678  
10              
11             with qw(HackaMol::X::Roles::ExtensionRole);
12              
13             sub _build_map_in{
14 1     1   12 my $sub_cr = sub { return (@_) };
  1     1   46  
15 1         24 return $sub_cr;
16             }
17              
18             sub _build_map_out{
19 1     1   10 my $sub_cr = sub { return (@_) };
  1     1   43  
20 1         23 return $sub_cr;
21             }
22              
23             sub build_command {
24             # exe -options file.inp -moreoptions > file.out
25 5     5 0 1511 my $self = shift;
26 5 100       146 return 0 unless $self->exe;
27 4         21 my $cmd;
28 4         87 $cmd = $self->exe;
29 4 100       121 $cmd .= " " . $self->in_fn->stringify if $self->has_in_fn;
30 4 100       208 $cmd .= " " . $self->exe_endops if $self->has_exe_endops;
31 4 100       171 $cmd .= " > " . $self->out_fn->stringify if $self->has_out_fn;
32              
33             # no cat of out_fn because of options to run without writing, etc
34 4         94 return $cmd;
35             }
36              
37             sub BUILD {
38 12     12 0 13 my $self = shift;
39              
40 12 100       359 if ( $self->has_scratch ) {
41 7 100       178 $self->scratch->mkpath unless ( $self->scratch->exists );
42             }
43              
44 12 100       1368 unless ( $self->has_command ) {
45 9 100       279 return unless ( $self->has_exe );
46 2         10 my $cmd = $self->build_command;
47 2         49 $self->command($cmd);
48             }
49 5         184 return;
50             }
51              
52             __PACKAGE__->meta->make_immutable;
53              
54             1;
55              
56             __END__
57              
58             =pod
59              
60             =head1 NAME
61              
62             HackaMol::X::Calculator - Abstract calculator class for HackaMol
63              
64             =head1 VERSION
65              
66             version 0.012
67              
68             =head1 SYNOPSIS
69              
70             use Modern::Perl;
71             use HackaMol;
72             use HackaMol::X::Calculator;
73             use Path::Tiny;
74            
75             my $path = shift || die "pass path to gaussian outputs";
76            
77             my $hack = HackaMol->new( data => $path, );
78            
79             foreach my $out ( $hack->data->children(qr/\.out$/) ) {
80              
81             my $Calc = HackaMol::X::Calculator->new(
82             out_fn => $out,
83             map_out => \&output_map,
84             );
85            
86             my $energy = $Calc->map_output(627.51);
87            
88             printf( "%-40s: %10.6f\n", $Calc->out_fn->basename, $energy );
89            
90             }
91            
92             # our function to map molec info from output
93            
94             sub output_map {
95             my $calc = shift;
96             my $conv = shift;
97             my $re = qr/-\d+.\d+/;
98             my @energys = $calc->out_fn->slurp =~ m/SCF Done:.*(${re})/g;
99             return ( $energys[-1] * $conv );
100             }
101              
102             =head1 DESCRIPTION
103              
104             The HackaMol::X::Calculator extension generalizes molecular calculations using external programs.
105             The Calculator class consumes the HackaMol::X::Roles::ExtensionRole role, which manage the running of executables...
106             perhaps on files; perhaps in directories. This extension is intended to provide a
107             simple example of interfaces with external programs. This is a barebones use of the ExtensionRole that is
108             intended to be flexible. See the examples and testing directory for use of the map_in and map_out functions
109             inside and outside of the map_input and map_output functions. Extensions with more rigid and encapsulated
110             APIs can evolve from this starting point. In the synopsis, a Gaussian output is processed for the SCF Done
111             value (a classic scripting of problem computational chemists). See the examples and tests to learn how the
112             calculator can be used to:
113              
114             1. generate inputs
115             2. run programs
116             3. process outputs
117              
118             via interactions with HackaMol objects.
119              
120             =head1 ATTRIBUTES
121              
122             =head2 scratch
123              
124             If scratch is set, the object will build that directory if needed. See HackaMol::PathRole for more information about
125             the scratch attribute.
126              
127             =head1 SEE ALSO
128              
129             =over 4
130              
131             =item *
132              
133             L<HackaMol>
134              
135             =item *
136              
137             L<HackaMol::X::Roles::Extension>
138              
139             =item *
140              
141             L<HackaMol::PathRole>
142              
143             =item *
144              
145             L<Path::Tiny>
146              
147             =back
148              
149             =head1 EXTENDS
150              
151             =over 4
152              
153             =item * L<Moose::Object>
154              
155             =back
156              
157             =head1 CONSUMES
158              
159             =over 4
160              
161             =item * L<HackaMol::Roles::ExeRole>
162              
163             =item * L<HackaMol::Roles::ExeRole|HackaMol::Roles::PathRole>
164              
165             =item * L<HackaMol::Roles::PathRole>
166              
167             =item * L<HackaMol::X::Roles::ExtensionRole>
168              
169             =back
170              
171             =head1 AUTHOR
172              
173             Demian Riccardi <demianriccardi@gmail.com>
174              
175             =head1 COPYRIGHT AND LICENSE
176              
177             This software is copyright (c) 2015 by Demian Riccardi.
178              
179             This is free software; you can redistribute it and/or modify it under
180             the same terms as the Perl 5 programming language system itself.
181              
182             =cut