Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TemperingBase.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 protocols/canonical_sampling/TemperingBaseMover.cc
11 /// @brief TemperingBase methods implemented
12 /// @author Oliver Lange ( oliver.lange@tum.de )
13 
14 
15 // Unit Headers
17 
18 // protocols headers
21 #include <protocols/moves/Mover.hh>
25 
27 
28 //#include <protocols/jd2/JobDistributor.hh>
29 #include <protocols/jd2/util.hh>
30 #include <protocols/jd2/Job.hh>
31 
32 // core headers
33 #include <basic/options/option_macros.hh>
34 #include <basic/options/keys/in.OptionKeys.gen.hh>
35 #include <basic/options/keys/packing.OptionKeys.gen.hh>
36 
37 #include <basic/Tracer.hh>
38 
41 
43 #include <core/types.hh>
44 
45 // numeric headers
46 #include <numeric/random/random.hh>
47 
48 // utility headers
49 #include <utility/file/file_sys_util.hh>
50 #include <utility/pointer/owning_ptr.hh>
51 #include <utility/tag/Tag.hh>
52 #include <utility/io/ozstream.hh>
53 #include <utility/io/izstream.hh>
54 
55 // C++ Headers
56 #include <cmath>
57 
58 // cmd-line options
59 OPT_2GRP_KEY( File, tempering, temp, file )
60 OPT_2GRP_KEY( Integer, tempering, temp, levels )
61 OPT_2GRP_KEY( Real, tempering, temp, low )
62 OPT_2GRP_KEY( Real, tempering, temp, high )
63 OPT_2GRP_KEY( RealVector, tempering, temp, range )
64 OPT_2GRP_KEY( File, tempering, stats, file )
65 OPT_2GRP_KEY( Boolean, tempering, stats, silent )
66 OPT_2GRP_KEY( Boolean, tempering, stats, line_output )
67 OPT_1GRP_KEY( Integer, tempering, stride )
68 
69 using basic::T;
70 using basic::Error;
71 using basic::Warning;
72 
73 static basic::Tracer tr( "protocols.canonical_sampling.TemperingBase" );
74 
75 
76 bool protocols::canonical_sampling::TemperingBase::options_registered_( false );
77 
78 //Mike: when you want to remove these Macros... leave them at least here as comment - since they provide documentation
79 void protocols::canonical_sampling::TemperingBase::register_options() {
80  if ( !options_registered_ ) {
81  options_registered_ = true;
82  NEW_OPT( tempering::temp::file, "file with temperature definitions for simulated tempering, including weights and counts","" );
83  NEW_OPT( tempering::temp::levels, "how many temp-levels, superseeded by -tempering::temp::file",10 );
84  NEW_OPT( tempering::temp::range, "min and max temperature, superseeded by -tempering::temp::file", 0 );
85  NEW_OPT( tempering::temp::high, "min and max temperature, superseeded by -tempering::temperatures or -tempering::temp::range", 3.0 );
86  NEW_OPT( tempering::temp::low, "min and max temperature, superseeded by -tempering::temperatures or -tempering::temp::range", 0.5 );
87 
88  NEW_OPT( tempering::stats::file, "filename (postfix) for output of tempering statistics (i.e, counts) <job>_.tempering.stats", "tempering.stats" );
89  NEW_OPT( tempering::stats::silent, "write all tempering information as lines into a single file -- similar to silent format", false );
90  NEW_OPT( tempering::stats::line_output, "choose line-output as in silent mode, even when using individual files", false );
91 
92  NEW_OPT( tempering::stride, "how often should a temperature switch be attempted", 1);
93  }
94 }
95 
96 namespace protocols {
97 namespace canonical_sampling {
98 using namespace core;
99 
101  instance_initialized_( false )
102 {
103  set_defaults();
104 }
105 
107  protocols::canonical_sampling::TemperatureController(other)
108 {
114  stats_file_ = other.stats_file_;
115 
116  job_ = other.job_;
120 }
121 
123 /// @brief callback executed before any Monte Carlo trials
124 /// use to fill count_ with 0
125 
128  return monte_carlo()->temperature();
129 }
130 
133  return temperatures_[ level ];
134 }
135 
138 {
139  return "TemperingBase";
140 }
141 
142 void
144  utility::tag::TagPtr const tag,
148  pose::Pose const &
149 ) {
150  //figure out temperatures...
151  std::string temp_file = tag->getOption< std::string >( "temp_file", "" );
152  bool success( false );
153  if ( temp_file.size() ) {
154  success=initialize_from_file( temp_file );
155  if ( !success ) tr.Info << "cannot read temperatures from file, will initialize from options... " << std::endl;
156  }
157  if ( !success ) {
158  Real temp_low = tag->getOption< Real >( "temp_low", 0.6 );
159  Real temp_high = tag->getOption< Real >( "temp_high", 3.0 );
160  Size temp_levels = tag->getOption< Size >( "temp_levels", 10 );
161  InterpolationType temp_interpolation = interpolation_type_string_to_enum(tag->getOption< std::string >( "temp_interpolation", "linear" ));
162  generate_temp_range( temp_low, temp_high, temp_levels, temp_interpolation );
163  }
164 
165  //simple options
166  temperature_stride_ = tag->getOption< Size >( "temp_stride", 100 );
167  trust_current_temp_ = tag->getOption< bool >( "trust_crurrent_temp", true );
168  stats_line_output_ = tag->getOption< bool >( "stats_line_output", false );
169  stats_silent_output_ = tag->getOption< bool >( "stats_silent_output", false );
170  stats_file_ = tag->getOption< std::string >( "stats_file", "" );
171  instance_initialized_ = true;
172 }
173 
174 
175 /// handling of options including command-line
177  //are we the only object controlling temperature in the MC object ?!
178  trust_current_temp_ = true;
179  //how often will no temperature jump occur
180 }
181 
182  /// @brief callback executed before any Monte Carlo trials
184  pose::Pose&,
186  core::Size //default=0; non-zero if trajectory is restarted
187 ) {
188  tr.Trace << "initialize Tempering Base... " << std::endl;
191  monte_carlo()->set_temperature( temperatures_[ current_temp_ ] );
192  if ( jd2::jd2_used() ) {
194  }
195  tr.Debug << std::setprecision(2);
196  if ( job_ ) {
197  job_->add_string_real_pair( "temperature", monte_carlo()->temperature() );
198  job_->add_string_real_pair( "temp_level", current_temp_ );
199  }
200  temp_trial_count_ = 0;
201  tr.Trace << "initialized Tempering Base!!!" << std::endl;
202 }
203 
204 void
206  core::pose::Pose & pose,
208  core::Size level,
209  core::Real temp_in,
210  core::Size cycle //default=0; non-zero if trajectory is restarted
211 ) {
212  initialize_simulation( pose, mh_mover,cycle );
213  set_current_temp( level );
214  if ( std::abs( temperature() - temp_in ) > 0.01 ) {
215  using namespace ObjexxFCL;
216  utility_exit_with_message( "inconsistent temperature when trying to restart Tempering temp_in: "
217  +string_of( temp_in )
218  +" temperature expected at level "+string_of( level )+ " is "+string_of( temperature() ) );
219  }
220 }
221 
222 void
224  pose::Pose&,
226 ) {
227  job_ = NULL;
228 }
229 
231  //we can trust our current temp variable or get it from protocols::moves::MonteCarlo object
232  if ( !trust_current_temp_ ) {
233  current_temp_=1;
234  Real const mc_temp( monte_carlo()->temperature() );
236  it != temperatures_.end(); ++it ) {
237  if ( *it > mc_temp ) break;
238  ++current_temp_;
239  }
240  if ( current_temp_ > temperatures_.size() ) current_temp_ = temperatures_.size();
241  } //if trusted
242  runtime_assert( monte_carlo()->temperature() == temperatures_[ current_temp_ ] );
243  return true;
244 }
245 
246 
247 /// @brief Assigns user specified values to primitive members using command line options
249  using namespace basic::options;
250  using namespace basic::options::OptionKeys;
251  using namespace core;
252 
253  if ( !options_registered_ ) {
254  utility_exit_with_message( "cannot call TemperingBase::init_from_options() unless TemperingBase::register_options() \
255  on application level before devel::init()" );
256  }
257 
258  bool success( false );
259  if ( option[ tempering::temp::file ].user() ) {
260  success=initialize_from_file( option[ tempering::temp::file ]() );
261  if ( !success ) tr.Info << "cannot read temperatures from file, will initialize from options... " << std::endl;
262  }
263  if ( !success ) {
264  Real temp_high, temp_low;
265  if ( option[ tempering::temp::range ].user() ) {
266  temp_low=option[ tempering::temp::range ]().front();
267  temp_high=option[ tempering::temp::range ]().back();
268  } else {
269  temp_low=option[ tempering::temp::low ]();
270  temp_high=option[ tempering::temp::high ]();
271  }
272  Size const n_levels( option[ tempering::temp::levels ]() );
273  generate_temp_range( temp_low, temp_high, n_levels );
274  }
275  stats_file_ = option[ tempering::stats::file ]();
276  stats_silent_output_ = option[ tempering::stats::silent ]();
277  stats_line_output_ = option[ tempering::stats::line_output ]() || stats_silent_output_;
278  temperature_stride_ = option[ tempering::stride ]();
279  instance_initialized_ = true;
280 }
281 
282 void TemperingBase::generate_temp_range( Real temp_low, Real temp_high, Size n_levels, InterpolationType interpolation /*= linear*/ ) {
283  temperatures_.clear();
284  runtime_assert( n_levels >= 2 );
285  tr.Info << "initializing temperatures from " << temp_low << " to " << temp_high << " with " << n_levels << " levels using "
286  << interpolation_type_enum_to_string(interpolation) << " interpolation." << std::endl;
287  if (interpolation == linear) {
288  Real const temp_step ( (temp_high-temp_low)/(n_levels-1) );
289  for ( Size ct=0; ct<n_levels; ++ct ) {
290  temperatures_.push_back( temp_low+ct*temp_step );
291  }
292  } else if (interpolation == exponential) {
293  Real next_temp( temp_low );
294  Real temp_factor( pow(temp_high/temp_low, 1/Real(n_levels-1)) );
295  for ( Size ct=1; ct<n_levels; ++ct ) {
296  temperatures_.push_back( next_temp );
297  next_temp *= temp_factor;
298  }
299  temperatures_.push_back( temp_high );
300  }
301 }
302 
304  clear();
305 
306  utility::io::izstream in( filename );
307  if ( !in.good() ) {
308  tr.Error << "cannot open file " << filename << std::endl;
309  return false;
310  }
311  std::string line;
312  getline( in, line );
313  std::istringstream line_stream( line );
314  std::string tag;
315  Size n_levels( 0 );
316 
317  line_stream >> tag >> n_levels;
318 
319  bool line_format( false );
320  if ( !line_stream.good() ) {
321  tr.Error << "format not recognized in temperature file: " << filename << " at line " << line << std::endl;
322  tr.Error << "excpected TEMPERING or TEMPERING_TABLE" << std::endl;
323  return false;
324  }
325 
326  if ( tag == "TEMPERING" ) {
327  line_format=true;
328  } else if ( tag == "TEMPERING_TABLE" ) {
329  line_format=false;
330  } else {
331  tr.Error << "format not recognized in temperature file: " << filename << " at line " << line << std::endl;
332  tr.Error << "excpected TEMPERING or TEMPERING_TABLE" << std::endl;
333  return false;
334  }
335 
336  Real temp;
337  if ( line_format ) {
338  for ( Size ct=1; ct <= n_levels; ++ct ) {
339  line_stream >> temp;
340  temperatures_.push_back( temp );
341  }
342  if ( !line_stream.good() ) {
343  tr.Error << "format not recognized in temperature file: " << filename << " at line " << line << std::endl;
344  tr.Error << "excpected TEMPERING N t1 t2 ... tN" << std::endl;
345  return false;
346  }
347  return true;
348  }
349 
350  // table format
351  while ( getline( in, line ) ) {
352  std::istringstream line_stream( line );
353  Real temp;
354  line_stream >> temp;
355  if ( !line_stream.good() ) tr.Error << "format error in temperature file: " << filename << " at line " << line << std::endl;
356  temperatures_.push_back( temp );
357  }
358  return true; //succesfully initialized
359 }
360 
362  temperatures_.clear();
363  instance_initialized_ = false;
364 }
365 
366 void TemperingBase::write_to_file( std::string const& file_in, std::string const& output_name, utility::vector1< Real > const& ) {
367 
368  //either write all these things to a single file, or write to <jobname>.file_in
369  std::string file;
370  if ( stats_silent_output_ ) {
371  file=file_in;
372  } else {
373  file=output_name+"."+file_in;
374  }
375 
376  //open file
377  utility::io::ozstream out( file, stats_line_output_ ? std::ios::app : std::ios::out );
378 
379  //write
380  if ( stats_line_output_ ) { //line format
381  out << "TEMPERING " << n_temp_levels();
382  for ( Size i=1; i <= n_temp_levels(); ++i ) {
383  out << " " << temperatures_[ i ];
384  }
385  out << " " << output_name << std::endl;
386  } else { //table format
387  out << std::setw( 10 );
388  out << "TEMPERING_TABLE " << n_temp_levels() << " " << output_name << std::endl;
389  for ( Size i=1; i <= n_temp_levels(); ++i ) {
390  out << temperatures_[ i ] << std::endl;
391  }
392  }
393 }
394 
396  temperatures_ = temps;
397  set_current_temp( temps.size() );
398 }
399 
400 
402  current_temp_ = new_temp;
403  Real real_temp = temperatures_[ current_temp_ ];
404  if ( monte_carlo() ) {
405  monte_carlo()->set_temperature( real_temp );
406  }
407  if ( job_ ) {
408  job_->add_string_real_pair( "temperature", real_temp );
409  job_->add_string_real_pair( "temp_level", new_temp );
410  }
411 }
412 
413 
414 } //moves
415 } //protocols
416