Removed Parallel::Forkmanager in modelfit
[PsN.git] / lib / nonmem_subs.pm
blob21e1d01c513def6a53b0fc66c1bc5fc344a00bce
1 # {{{ include statements
3 start include statements
4 if( $0 =~ /nonmem.pm$/ ) {
5 use FindBin qw($Bin);
6 use lib "$Bin";
8 use File::Copy 'cp';
9 #use ext::Config::Tiny;
10 use ext::IPC::Run3;
11 use Config;
12 if( $0 =~ /nonmem.pm$/ ) {
13 require PsN;
14 my $nonmem = nonmem -> new( 'modelfile' => $ARGV[0],
15 'outputfile' => $ARGV[1],
16 'nice' => $ARGV[2],
17 'version' => $ARGV[3],
18 'fsubs' => [split( /,/ , $ARGV[6] )],
19 'nm_directory' => $ARGV[7],
20 'show_version' => 0);
22 my $compile = $ARGV[4];
23 my $execute = $ARGV[5];
25 if( $compile ){
26 unless( $nonmem -> compile() ){
27 debug -> die( message => $nonmem -> error_message );
31 if( $execute ){
32 $nonmem -> execute;
35 end include statements
37 # }}}
39 # {{{ new
41 start new
43 unless( defined $this -> {'outputfile'} ){
44 $this -> {'outputfile'} = $this -> {'modelfile'};
45 $this -> {'outputfile'} =~ s/\.mod$/\.lst/;
48 unless( defined $this -> {'nm_directory'} ){
49 my ($nmdir,$version) = split(/,/ , $PsN::config -> { 'nm_versions' } -> { $this -> {'version'} });
51 unless( defined $nmdir ){
52 print "Unknown NONMEM version ",$this -> {'version'}," specified.\n";
53 print "Configuration file is ",$PsN::config_file,"\n";
54 my @nmkeys = keys %{$PsN::config -> { 'nm_versions' }};
55 if ( $#nmkeys == 0 and
56 defined $nmkeys[0] and
57 defined $PsN::config -> { 'nm_versions' } -> {$nmkeys[0]} ) {
58 print "Using the only defined version $nmkeys[0] in ",$PsN::config -> { 'nm_versions' } -> {$nmkeys[0]}," instead\n";
59 my( $nmdir, $version ) = split(/,/,$PsN::config -> { 'nm_versions' } -> {$nmkeys[0]});;
63 $this -> {'nm_directory'} = $nmdir;
65 unless( defined $PsN::config -> {'compiler'} and defined $PsN::config -> {'compiler'} -> {'name'} ){
66 debug -> warn( level => 1,
67 message => "No compiler defined, assuming g77" );
68 $this -> {'compiler'} = 'g77';
69 } else {
70 $this -> {'compiler'} = $PsN::config -> {'compiler'} -> {'name'};
71 $this -> {'compiler_options'} = $PsN::config -> {'compiler'} -> {'options'};
75 end new
77 # }}}
79 # {{{ compile
81 start compile
83 my $version = $self -> {'version'};
84 my $nmdir = $self -> {'nm_directory'};
85 my $includes;
86 my $nmlink;
87 my $adaptive = $self -> {'adaptive'} ? '_adaptive' : '';
88 if( -e "$nmdir/util/nmlink5.exe" ){
89 $nmlink = "$nmdir/util/nmlink5.exe";
90 $self -> {'version'} = 5;
91 if( $self -> {'show_version'} ){
92 $version = $self -> {'version'};
93 } else {
94 $version = '';
96 } elsif ( -e "$nmdir/util/nmlink6.exe" ){
97 $self -> {'version'} = 6;
98 if( $self -> {'show_version'} ){
99 $version = $self -> {'version'};
100 } else {
101 $version = '';
103 $nmlink = "$nmdir/util/nmlink6.exe";
104 if( $Config{osname} eq 'MSWin32' ){
105 $includes = "-I$nmdir\\sizes";
106 } else {
107 $includes = "-I$nmdir/sizes";
109 } else {
110 my $err_version = ( defined $nmdir and $nmdir ne '' ) ? $nmdir : '[not configured]';
111 debug -> die( message => "Unable to find a supported version of NONMEM\n".
112 "The NONMEM installation directory is $err_version for version [".
113 $self -> {'version'}."] according to psn.conf." );
116 # first clean up from old compile
117 unlink( 'FLIB','FCON', 'FDATA', 'FREPORT','FSUBS', 'FSUBS.f','LINK.LNK','FSTREAM', 'PRDERR', 'nonmem.exe', 'nonmem', 'nonmem5', 'nonmem6', 'nonmem5_adaptive', 'nonmem6_adaptive' );
119 my $nm;
121 if( $Config{osname} eq 'MSWin32' ){
122 $nm="$nmdir\\nm";
123 } else {
124 $nm="$nmdir/nm";
127 my $modelfile = $self -> {'modelfile'};
129 run3( "nice -19 $nmdir/tr/nmtran.exe", $modelfile, \$self -> {'nmtran_message'}, \$self -> {'nmtran_message'} );
131 #$self -> {'nmtran_message'} = `nice -19 $nmdir/tr/nmtran.exe < $modelfile 2>&1`;
133 open( NMMSG, '>compilation_output.txt' );
134 print( NMMSG $self -> {'nmtran_message'}, "\n" );
136 unless(-e 'FREPORT'){
137 close(NMMSG);
138 $self -> {'error_message'} = "NMtran failed: \n" . $self -> {'nmtran_message'} ;
139 return 0;
142 my $nmlink_message;
144 run3( "$nmlink", undef, \$nmlink_message, \$nmlink_message );
146 print( NMMSG $nmlink_message, "\n" );
148 my $fsub;
150 if(-e 'FSUBS'){
151 if( $self -> {'compiler'} eq 'g77' ){
152 rename("FSUBS", "FSUBS.f");
153 $fsub='FSUBS.f';
154 } else {
155 rename("FSUBS", "FSUBS.for");
156 $fsub='FSUBS.for';
160 if( defined $self -> {'fsubs'} ){
161 foreach my $sub ( @{$self -> {'fsubs'}} ){
162 $fsub .= " $sub";
166 open( FH, "<", 'LINK.LNK' );
167 my @link;
168 while( <FH> ){
169 my $tmp = $_;
170 $tmp =~ s/^\s+//;
171 $tmp =~ s/\s+$//;
172 push(@link, $tmp);
174 close( FH );
176 my $compile_message;
177 my $compile_command;
179 if( $self -> {'compiler'} eq 'g77' ){
181 my @nmlib = ("$nm/NONMEM.o", "$nm/BLKDAT.o", "$nm/nonmem.a");
182 $compile_command = "g77 " . $self -> {'compiler_options'} . " -ononmem$version$adaptive $includes $fsub @link @nmlib 2>&1";
184 } elsif( $self -> {'compiler'} eq 'df' or $self -> {'compiler'} eq 'fl32' ){
186 my @nmlib = ("$nm\\nonmem.obj", "$nm\\blkdat.obj", "$nm\\nonmem.lib");
187 $compile_command = $self -> {'compiler'}." " . $self -> {'compiler_options'} . " /Fenonmem $fsub @nmlib @link";
189 } elsif( $self -> {'compiler'} eq 'ifort' ){
190 my @nmlib = ("$nm/NONMEM.o", "$nm/BLKDAT.o", "$nm/nonmem.a");
191 $compile_command = "ifort " . $self -> {'compiler_options'} . " -ononmem$version$adaptive $includes $fsub @link @nmlib 2>&1";
194 run3( "$compile_command", undef, \$compile_message, \$compile_message );
196 print( NMMSG $compile_message, "\n" );
199 unless(-e "nonmem.exe" or -e "nonmem$version$adaptive" ) {
200 $self -> {'error_message'} = "Fortran Compilation failed: \n" . $compile_message ;
201 close( NMMSG );
202 return 0;
203 } else {
204 print( NMMSG "Compilation done\n" );
205 close( NMMSG );
208 end compile
210 # }}}
212 # {{{ execute
214 start execute
216 my $version = $self -> {'version'};
217 my $nmdir = $self -> {'nm_directory'};
218 my $outputfile = $self -> {'outputfile'};
219 my $adaptive = $self -> {'adaptive'} ? '_adaptive' : '';
221 my ( $start_time, $fin_time );
222 if( $Config{osname} eq 'MSWin32' ){
223 $start_time = `date /T`;
224 chomp($start_time);
225 $start_time = $start_time.' '.`time /T`;
226 run3( "nonmem.exe", undef, \undef );
227 $fin_time = `date /T`;
228 chomp($fin_time);
229 $fin_time = $fin_time.' '.`time /T`;
230 if ( -e "output" ) {
231 cp( "output", $outputfile );
232 unlink( "output" );
233 } elsif ( -e "OUTPUT" ) {
234 cp( "OUTPUT", $outputfile );
235 unlink( "OUTPUT" );
237 } else {
238 unless( $self -> {'show_version'} ){
239 $version = '';
241 $start_time = `date`;
242 run3( "nice -n " . $self -> {'nice'} . " ./nonmem$version$adaptive", "FCON", $outputfile );
243 $fin_time = `date`;
245 open( OUT, '>>', $outputfile );
246 print( OUT "This file was created using the NONMEM version in directory $nmdir\n" );
247 print( OUT "Started $start_time" );
248 print( OUT "Finished $fin_time" );
249 close( OUT );
251 end execute
253 # }}}