1 #include <boost/filesystem.hpp>
2 #include <boost/program_options.hpp>
24 sout <<
desc << std::endl;
29 sout <<
desc << std::endl;
31 sout <<
"DESCRIPTION\n"
32 <<
" Perform Monte Carlo calculations. \n\n"
35 " casm monte --settings input_file.json \n"
36 <<
" - Run Monte Carlo calculations given the input file \n"
38 <<
" - See 'casm format --monte' for a description of the \n"
39 <<
" Monte Carlo input file. \n\n"
42 " casm monte --settings input_file.json --initial-POSCAR 3 \n"
43 <<
" - Write a POSCAR.initial file containing the initial state of\n"
44 <<
" the Monte Carlo calculation. The argument is a condition\n"
45 <<
" index specifying which run is being requested.\n"
47 "output_directory/conditions.3/trajectory/POSCAR.initial\n\n"
50 " casm monte --settings input_file.json --final-POSCAR 3 \n"
51 <<
" - Write a POSCAR.final file containing the final state of\n"
52 <<
" the Monte Carlo calculation. The argument is a condition\n"
53 <<
" index specifying which run is being requested.\n"
55 "output_directory/conditions.3/trajectory/POSCAR.final\n\n"
58 " casm monte --settings input_file.json --traj-POSCAR 5 \n"
59 <<
" - Write the Monte Carlo calculation trajectory as a \n"
60 <<
" series of POSCAR files containing the state of the \n"
61 <<
" Monte Carlo calculation every time a sample was taken.\n"
62 <<
" The argument is a condition index specifying which run\n"
63 <<
" is being requested. \n"
64 <<
" - The trajectory file must exist. This is generated when\n"
65 <<
" using input option \"data\"/\"storage\"/\"write_trajectory\" "
68 "output_directory/conditions.5/trajectory/POSCAR.i,\n"
69 <<
" where i is the sample index. \n\n";
82 "Given the condition index, print a POSCAR for the "
83 "initial state of a monte carlo run.")(
85 "Given the condition index, print a POSCAR for the final state of a "
88 "Given the condition index, print POSCARs for the state at every sample "
89 "of monte carlo run. Requires an existing trajectory file.");
97 template <
typename MCType>
101 template <
typename MCType>
105 template <
typename MCType>
109 template <
typename MCType>
124 fs::path settings_path;
125 std::string verbosity_str;
129 po::variables_map &vm = monte_opt.
vm();
133 po::parse_command_line(args.
argc(), args.
argv(), monte_opt.
desc()),
138 if (vm.count(
"help")) {
143 if (vm.count(
"desc")) {
154 if (vm.count(
"verbosity")) {
158 err_log() <<
"Expected: 'none', 'quiet', 'standard', 'verbose', "
159 "'debug', or an integer 0-100 (0: none, 100: all)"
167 }
catch (po::error &e) {
168 err_log() <<
"ERROR: " << e.what() << std::endl << std::endl;
171 }
catch (std::exception &e) {
172 err_log() <<
"Unhandled Exception reached the top of main: " << e.what()
173 <<
", application will now exit" << std::endl;
177 const fs::path &root = args.
root;
186 std::unique_ptr<PrimClex> uniq_primclex;
190 settings_path = fs::absolute(settings_path);
200 log().
read(
"Monte Carlo settings");
201 log() <<
"from: " << settings_path <<
"\n";
203 log() <<
"ensemble: " << monte_settings.
ensemble() <<
"\n";
204 log() <<
"method: " << monte_settings.
method() <<
"\n";
205 if (
log().verbosity() == 100) {
208 if (monte_settings.
debug()) {
209 log() <<
"debug: " << monte_settings.
debug() <<
"\n";
213 }
catch (std::exception &e) {
214 err_log() <<
"ERROR reading Monte Carlo settings.\n\n";
215 err_log() << e.what() << std::endl;
228 template <
typename MCType>
232 typename MCType::SettingsType mc_settings(
primclex,
240 }
catch (std::exception &e) {
242 <<
" Monte Carlo initial snapshot for condition: "
244 err_log() << e.what() << std::endl;
249 template <
typename MCType>
253 typename MCType::SettingsType mc_settings(
primclex,
261 }
catch (std::exception &e) {
263 <<
" Monte Carlo initial snapshot for condition: "
265 err_log() << e.what() << std::endl;
270 template <
typename MCType>
274 typename MCType::SettingsType mc_settings(
primclex,
282 }
catch (std::exception &e) {
284 <<
" Monte Carlo path snapshots for condition: "
286 err_log() << e.what() << std::endl;
291 template <
typename MCType>
295 typename MCType::SettingsType mc_settings(
primclex,
300 }
catch (std::exception &e) {
302 <<
" Monte Carlo.\n\n";
303 err_log() << e.what() << std::endl;
313 const po::variables_map &vm = monte_opt.
vm();
315 if (vm.count(
"initial-POSCAR")) {
316 return _initial_POSCAR<MCType>(
primclex, args, monte_opt);
317 }
else if (vm.count(
"final-POSCAR")) {
318 return _final_POSCAR<MCType>(
primclex, args, monte_opt);
319 }
else if (vm.count(
"traj-POSCAR")) {
320 return _traj_POSCAR<MCType>(
primclex, args, monte_opt);
327 throw std::invalid_argument(
328 "ERROR in LTE1 calculation: dependents_runs must be false");
339 throw std::invalid_argument(
340 "ERROR in LTE1 calculation: must use one of\n"
341 " \"driver\"/\"motif\"/\"configname\": \"auto\"\n"
342 " \"driver\"/\"motif\"/\"configname\": \"restricted_auto\"");
347 if (fs::exists(
dir.results_csv())) {
348 err_log() <<
"Existing file at: " <<
dir.results_csv() << std::endl;
349 err_log() <<
" Exiting..." << std::endl;
354 if (fs::exists(
dir.results_json())) {
355 err_log() <<
"Existing file at: " <<
dir.results_json() << std::endl;
356 err_log() <<
" Exiting..." << std::endl;
365 log() <<
"Phi_LTE(1) = potential_energy_gs - kT*ln(Z'(1))/N" << std::endl;
366 log() <<
"Z'(1) = sum_i(exp(-dPE_i/kT), summing over ground state and "
369 log() <<
"dPE_i: (potential_energy_i - potential_energy_gs)*N"
375 int num_conditions = 1;
380 num_conditions = (
final - init) / incr + 1;
386 for (
int index = 0; index < num_conditions; ++index) {
391 log() <<
"formation_energy: " << std::setprecision(12)
393 log() <<
" components: "
396 log() <<
" comp_n: " << gc.
comp_n().transpose() << std::endl;
397 log() <<
" param_chem_pot: "
400 << comp_converter.param_composition(gc.
comp_n()).transpose()
402 log() <<
"potential energy: " << std::setprecision(12)
419 }
catch (std::exception &e) {
420 err_log() <<
"ERROR calculating single spin flip LTE grand canonical "
422 err_log() << e.what() << std::endl;
426 return _driver<Monte::GrandCanonical>(
primclex, args, monte_opt);
429 <<
" Monte Carlo. No valid option given.\n\n";
439 const po::variables_map &vm = monte_opt.
vm();
441 if (vm.count(
"initial-POSCAR")) {
442 return _initial_POSCAR<MCType>(
primclex, args, monte_opt);
443 }
else if (vm.count(
"final-POSCAR")) {
444 return _final_POSCAR<MCType>(
primclex, args, monte_opt);
445 }
else if (vm.count(
"traj-POSCAR")) {
446 return _traj_POSCAR<MCType>(
primclex, args, monte_opt);
448 return _driver<MCType>(
primclex, args, monte_opt);
451 <<
" Monte Carlo. No valid option given.\n\n";
#define ERR_EXISTING_FILE
#define ERR_INVALID_INPUT_FILE
void initialize() override
Fill in the options descriptions accordingly.
const fs::path settings_path() const
Returns the path corresponding to add_settings_suboption.
Index condition_index() const
const std::string & verbosity_str() const
Returns the string corresponding to add_verbosity_suboption()
const po::options_description & desc()
Get the program options, filled with the initialized values.
po::variables_map & vm()
Get the variables map.
void add_settings_suboption(bool required=true)
void add_help_suboption()
Add a plain –help and –desc suboptions.
po::options_description m_desc
void add_verbosity_suboption()
std::vector< std::string > components() const
The order of components in mol composition vectors.
void read(const std::string &what)
void set_verbosity(int _verbosity)
void custom(const std::string &what)
void error(const std::string &what)
static std::pair< bool, int > verbosity_level(std::string s)
Read verbosity level from a string.
void write(const std::string &what)
static const ENSEMBLE ensemble
Eigen::VectorXd param_chem_pot() const
parametric chemical potential: dg/dcomp_x
std::pair< ConfigDoF, std::string > set_state(const GrandCanonicalConditions &new_conditions, const GrandCanonicalSettings &settings)
Set configdof and conditions and clear previously collected data.
double lte_grand_canonical_free_energy() const
Calculate the single spin flip low temperature expansion of the grand canonical potential.
const double & formation_energy() const
Formation energy, normalized per primitive cell.
const double & potential_energy() const
Potential energy, normalized per primitive cell.
const CondType & conditions() const
Return current conditions.
static const ENSEMBLE ensemble
const Eigen::VectorXd & comp_n() const
Number of atoms of each type, normalized per primitive cell.
GrandCanonicalConditions initial_conditions() const
Expects initial_conditions.
GrandCanonicalConditions final_conditions() const
Expects final_conditions.
GrandCanonicalConditions incremental_conditions() const
Expects incremental_conditions.
const PrimClex & primclex() const
const Access the PrimClex that *this is based on
bool debug() const
return true if running in debug mode
void run()
Run everything requested by the MonteSettings.
Settings for Monte Carlo calculations.
bool dependent_runs() const
If dependent runs, start subsequent calculations with the final state of the previous calculation....
Monte::METHOD method() const
Return type of Monte Carlo method.
bool debug() const
Run in debug mode?
const fs::path output_directory() const
Directory where output should go.
std::string motif_configname() const
Configname of configuration to use as starting motif.
virtual const Monte::DRIVE_MODE drive_mode() const
Given a settings jsonParser figure out the drive mode. Expects drive_mode/incremental,...
bool is_motif_configname() const
Returns true if configname of configuration to use as starting motif has been specified.
bool write_csv() const
Write csv versions of files? (csv is the default format if no 'output_format' given)
void set_debug(bool _debug)
Set debug mode.
bool write_json() const
Write json versions of files?
Monte::ENSEMBLE ensemble() const
Return type of Monte Carlo ensemble.
PrimClex is the top-level data structure for a CASM project.
PrimClex & make_primclex_if_not(const CommandArgs &args, std::unique_ptr< PrimClex > &uniq_primclex)
If !_primclex, construct new PrimClex stored in uniq_primclex, then return reference to existing or c...
std::string to_string(ENUM val)
Return string representation of enum class.
const CompositionConverter & composition_axes() const
const Access CompositionConverter object
ConfigIO::GenericConfigFormatter< std::string > configname()
Constructs DataFormmaterDictionary containing all Configuration DatumFormatters.
void write_POSCAR_initial(const MonteCarlo &mc, size_type cond_index, Log &_log)
For the initial state, write a POSCAR file.
void write_POSCAR_final(const MonteCarlo &mc, size_type cond_index, Log &_log)
For the final state, write a POSCAR file.
void write_lte_results(const MonteSettings &settings, const GrandCanonical &mc, const double &phi_LTE1, const std::string &configname, Log &_log)
Will create new file or append to existing results file the results of the latest run.
void write_POSCAR_trajectory(const MonteCarlo &mc, size_type cond_index, Log &_log)
For every snapshot taken, write a POSCAR file.
void print_monte_desc(std::ostream &sout, const po::options_description &desc)
int _driver(PrimClex &primclex, const CommandArgs &args, const Completer::MonteOption &monte_opt)
int _run_Canonical(PrimClex &primclex, const Monte::MonteSettings &monte_settings, const CommandArgs &args, const Completer::MonteOption &monte_opt)
int _initial_POSCAR(PrimClex &primclex, const CommandArgs &args, const Completer::MonteOption &monte_opt)
void print_monte_help(std::ostream &sout, const po::options_description &desc)
int _final_POSCAR(PrimClex &primclex, const CommandArgs &args, const Completer::MonteOption &monte_opt)
int _traj_POSCAR(PrimClex &primclex, const CommandArgs &args, const Completer::MonteOption &monte_opt)
INDEX_TYPE Index
For long integer indexing:
int monte_command(const CommandArgs &args)
int _run_GrandCanonical(PrimClex &primclex, const Monte::MonteSettings &monte_settings, const CommandArgs &args, const Completer::MonteOption &monte_opt)
DirectoryStructure const & dir
Data structure holding basic CASM command info.