Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ExternalEvaluator.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file PoseEvaluator
11 /// @brief PoseEvaluator
12 /// @detailed
13 ///
14 ///
15 /// @author Oliver Lange
16 #ifdef USEMPI
17 #include <mpi.h> //keep this first
18 #endif
19 
20 
21 // Unit Headers
23 #include <basic/options/option.hh>
24 
25 // Package Headers
26 
27 // Project Headers
29 #include <core/pose/Pose.hh>
30 
31 // ObjexxFCL Headers
32 #include <ObjexxFCL/string.functions.hh>
33 
34 // Utility headers
35 #include <basic/Tracer.hh>
36 #include <utility/io/izstream.hh>
37 // AUTO-REMOVED #include <utility/io/ozstream.hh>
38 // AUTO-REMOVED #include <utility/io/util.hh>
39 #include <utility/file/file_sys_util.hh>
40 #include <numeric/random/random.hh>
41 
42 // C++ headers
43 #include <cstdlib>
44 #include <string>
45 #include <vector>
46 #include <sys/stat.h>
47 #include <iostream>
48 // option key includes
49 
50 #include <basic/options/keys/out.OptionKeys.gen.hh>
51 
52 #include <utility/vector1.hh>
53 
54 
55 
56 #ifdef __native_client__
57 #define system(a) 1
58 #endif
59 
60 // C++ headers
61 
62 static basic::Tracer tr("protocols.simple_filter.ExternalEvaluator");
63 
64 static numeric::random::RandomGenerator RG(5512489); // <- Magic number, do not change it!
65 
66 namespace protocols {
67 namespace simple_filters {
68 
69 using namespace core;
70 using namespace std;
71 
72 
74  : evaluation::SingleValuePoseEvaluator<core::Real>( tag ),
75  command_( command )
76 {
77 // this probably shouldn't go on BOINC
78 #ifndef WIN32
79 #ifndef BOINC
80 #ifndef __native_client__
81 
82  using namespace basic::options;
83  if ( !option[ OptionKeys::out::path::scratch ].user() ) {
84  tr.Warning << "******************************************************************************************************\n"
85  << " no scratch dir defined \n" << " use -out:path:scratch \n"
86  << "******************************************************************************************************\n" << endl;
87  utility_exit_with_message(" define your scratch dir with -out:path:scratch " );
88  scratch_dir_ = "/scratch/USERS/";
89  } else {
90  scratch_dir_ = option[ OptionKeys::out::path::scratch ]();
91  }
92  if ( !utility::file::file_exists( scratch_dir_.c_str() ) ) mkdir( scratch_dir_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
93 
94  string const tmp_file_name ("_ExternalEvaluator_"+name( 1 ));
95 
96  string dir = "./";
97  if ( option[ OptionKeys::out::file::silent ].user() ) {
98  dir = option[ OptionKeys::out::file::silent ]();
99  }
100 
101  //also use random number!
102 
103  string sub_work_dir;
104  {// if processes write silent files to subdirectory ... get this path
105  string dircmd( "echo `dirname "+dir+"` | sed s@/@_@g");
106  FILE* get_dir = popen(dircmd.c_str(),"r");
107  char buf[500];
108  fgets(buf,500,get_dir);
109  buf[strlen(buf)-1]='\0';
110  sub_work_dir = string( buf )+"_"+tmp_file_name; //get rid of newline
111  pclose( get_dir );
112  }
113  sub_work_dir = sub_work_dir+"_"+ ObjexxFCL::string_of( RG.random_range(0, 999999) );
114  // set npes and rank based on whether we are using MPI or not
115 #ifdef USEMPI
116  int rank_;
117  MPI_Comm_rank( MPI_COMM_WORLD, ( int* )( &rank_ ) );
118  sub_work_dir = "mpi_" + ObjexxFCL::string_of( rank_ ) + sub_work_dir;
119 #endif
120 
121 
122  { // get our working path
123  string dircmd( "pwd | sed s@/@_@g");
124  FILE* get_dir = popen(dircmd.c_str(),"r");
125  char buf[500];
126  fgets(buf,500,get_dir);
127  buf[strlen(buf)-1]='\0';
128  string tmp_id = string( buf );
129  work_dir_ = tmp_id.substr( max(0, (int)tmp_id.size()-40) );
130  pclose( get_dir );
131  }
132 
133 
135  tr.Info << "create scratch space... : " << work_dir_ << std::endl;
136  if ( !utility::file::file_exists( work_dir_.c_str() ) ) mkdir(work_dir_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
137  work_dir_ = work_dir_+"/"+sub_work_dir;
138  tr.Info << "create scratch space... : " << work_dir_ << std::endl;
139  if ( !utility::file::file_exists( work_dir_.c_str() ) ) mkdir(work_dir_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
140 #endif
141 #endif
142 #endif
143 }
144 
145 
147  using namespace basic::options;
148 
149  if ( !applicable( pose ) ) return 99999;
150 
151  string const command_buf( "DIR="+work_dir_+"; cd $DIR; "+command_+"; cd -");
152 
153  string const pose_file_name( work_dir_+string("/__POSE.pdb") );
154  string const result_file_name( work_dir_+string("/__RESULT") );
155  std::ofstream pose_stream(pose_file_name.c_str() ); //make sure not the MPI FileBuf is used !
156  if ( !pose_stream ) tr.Error << "can't write pose to file " << pose_file_name << std::endl;
157  pose.dump_pdb( pose_stream, pose_file_name );
158  pose_stream.close();
159  tr.Info << "write pose : " << pose_file_name << endl;
160  tr.Info << "execute command: "<< command_buf << endl;
161  int ret(system(command_buf.c_str()));
162  if(ret){
163  tr.Warning << "Applying the external evaluator failed!" << endl;
164  }
165 
166  //execl("/bin/bash","bash","-c",command_buf.c_str(), (char *)0);
167 
168  core::Real result;
169  {
170  utility::io::izstream result_file( result_file_name );
171  result_file >> result;
172  }
173  std::string const delete_result_cmd( "rm -f "+work_dir_+"/__RESULT" );
174  ret = system( delete_result_cmd.c_str() );
175  if(ret){
176  tr.Warning << "Deleting the file '" << work_dir_ << "/__RESULT' failed!" << endl;
177  }
178 
179  tr.Debug << "obtained result: " << result << endl;
180  return result;
181 }
182 
184  std::string command = "rm -Rf "+work_dir_;
185  int const ret(system(command.c_str()));
186  if(ret){
187  tr.Warning << "Deleting work directory '" << work_dir_ << "' failed!" << endl;
188  }
189  //clean up
190 }
191 
192 // core::Real ExternalEvaluator::apply( core::pose::Pose& pose_in ) const {
193 // pose::Pose pose( pose_in );
194 
195 // runtime_assert( constraints_ );
196 // pose.constraint_set( constraints_ );
197 
198 // ScoreFunction scfxn;
199 // scfxn.set_weight( atom_pair_constraint, 1.0 );
200 // return scfxn( pose );
201 
202 // }
203 
204 
205 }
206 }