3 #include <boost/filesystem.hpp>
4 #include <boost/filesystem/fstream.hpp>
16 #include "casm/external/gzstream/gzstream.h"
22 template class DataFormatter<Monte::MonteCarlo const *>;
23 template class DataFormatter<std::pair<Monte::MonteCarlo const *, Index> >;
28 : m_output_dir(
fs::absolute(output_dir)) {}
32 std::string prop_name) {
34 return mc->samplers().find(prop_name)->second->mean(
35 mc->is_equilibrated().second);
39 return mc->is_equilibrated().first;
42 std::string header = std::string(
"<") + prop_name +
">";
45 header, header, evaluator, validator);
50 std::string prop_name) {
52 return mc->samplers().find(prop_name)->second->calculated_precision(
53 mc->is_equilibrated().second);
57 return mc->is_equilibrated().first;
60 std::string header = std::string(
"prec(<") + prop_name +
">)";
63 header, header, evaluator, validator);
73 obs1.segment(equil.second, obs1.size() - equil.second);
78 obs2.segment(equil.second, obs2.size() - equil.second);
92 return (XYsum - Xsum * Ysum / N) / N;
97 std::string prop_name1, std::string prop_name2) {
101 return mc->is_equilibrated().first;
105 std::string(
"cov(") + prop_name1 +
"," + prop_name2 +
")";
108 header, header, evaluator, validator);
116 return mc->is_equilibrated().first;
120 "is_equilibrated",
"is_equilibrated", evaluator);
127 return mc->is_converged();
131 "is_converged",
"is_converged", evaluator);
139 return mc->is_equilibrated().second;
143 "N_equil_samples",
"N_equil_samples", evaluator);
150 return mc->sample_times().size() - mc->is_equilibrated().second;
154 "N_avg_samples",
"N_avg_samples", evaluator);
159 std::pair<ConstMonteCarloPtr, size_type> >
161 auto evaluator = [=](
const std::pair<ConstMonteCarloPtr, size_type> &obs) {
162 return obs.first->sample_times()[obs.second].first;
166 std::pair<ConstMonteCarloPtr, size_type> >(
167 "Pass",
"Pass", evaluator);
173 auto evaluator = [=](
const std::pair<ConstMonteCarloPtr, size_type> &obs) {
174 return obs.first->sample_times()[obs.second].second;
178 std::pair<ConstMonteCarloPtr, size_type> >(
179 "Step",
"Step", evaluator);
185 auto evaluator = [=](
const std::pair<ConstMonteCarloPtr, size_type> &obs) {
187 *(obs.first->samplers().find(prop_name)->second);
192 std::pair<ConstMonteCarloPtr, size_type> >(
193 prop_name, prop_name, evaluator);
200 [=](
const std::pair<ConstMonteCarloPtr, size_type> &site) ->
int {
201 return site.first->trajectory()[site.second].occ(occ_index);
204 std::string header = std::string(
"occ(") +
std::to_string(occ_index) +
")";
207 header, header, evaluator);
268 fs::create_directories(
dir.conditions_dir(cond_index));
271 std::vector<std::pair<ConstMonteCarloPtr, size_type> > observations;
274 observations.push_back(std::make_pair(ptr, i));
279 (
dir.observations_csv(cond_index).string() +
".gz").c_str());
281 << fs::path(
dir.observations_csv(cond_index).string() +
".gz")
283 sout << formatter(observations.cbegin(), observations.cend());
289 (
dir.observations_json(cond_index).string() +
".gz").c_str());
291 << fs::path(
dir.observations_json(cond_index).string() +
".gz")
294 formatter(observations.cbegin(), observations.cend())
295 .to_json_arrays(json);
301 std::cerr <<
"ERROR writing observations." << std::endl;
333 fs::create_directories(
dir.conditions_dir(cond_index));
337 std::vector<std::pair<ConstMonteCarloPtr, size_type> > observations;
340 observations.push_back(std::make_pair(ptr, i));
345 (
dir.trajectory_csv(cond_index).string() +
".gz").c_str());
347 << fs::path(
dir.trajectory_csv(cond_index).string() +
".gz") <<
"\n";
348 sout << formatter(observations.cbegin(), observations.cend());
352 for (
int i = 0; i < prim.
basis().size(); i++) {
353 if (prim.
basis()[i].allowed_occupants().size() > max_allowed) {
354 max_allowed = prim.
basis()[i].allowed_occupants().size();
364 fs::ofstream keyout(
dir.occupation_key_csv());
365 _log <<
"write: " <<
dir.occupation_key_csv() <<
"\n";
366 keyout <<
"site_index";
367 for (
int i = 0; i < max_allowed; i++) {
368 keyout <<
"\tocc_index_" << i;
372 for (
int i = 0; i < prim.
basis().size(); i++) {
374 for (
int j = 0; j < max_allowed; j++) {
375 if (j < prim.
basis()[i].allowed_occupants().size()) {
376 keyout <<
"\t" << prim.
basis()[i].allowed_occupants()[j];
401 (
dir.trajectory_json(cond_index).string() +
".gz").c_str());
403 << fs::path(
dir.trajectory_json(cond_index).string() +
".gz")
413 for (
int i = 0; i < prim.
basis().size(); i++) {
416 key.
write(
dir.occupation_key_json());
417 _log <<
"write: " <<
dir.occupation_key_json() <<
"\n";
421 std::cerr <<
"ERROR writing observations." << std::endl;
432 fs::create_directories(
dir.trajectory_dir(cond_index));
439 if (!fs::exists(
dir.initial_state_json(cond_index))) {
440 throw std::runtime_error(
441 std::string(
"ERROR in 'write_POSCAR_initial(const MonteCarlo &mc, "
442 "size_type cond_index)'\n") +
443 " File not found: " +
dir.initial_state_json(cond_index).string());
447 fs::ofstream sout(
dir.POSCAR_initial(cond_index));
448 _log <<
"write: " <<
dir.POSCAR_initial(cond_index) <<
"\n";
460 fs::create_directories(
dir.trajectory_dir(cond_index));
467 if (!fs::exists(
dir.final_state_json(cond_index))) {
468 throw std::runtime_error(
469 std::string(
"ERROR in 'write_POSCAR_final(const MonteCarlo &mc, "
470 "size_type cond_index)'\n") +
471 " File not found: " +
dir.final_state_json(cond_index).string());
475 fs::ofstream sout(
dir.POSCAR_final(cond_index));
476 _log <<
"write: " <<
dir.POSCAR_final(cond_index) <<
"\n";
490 fs::create_directories(
dir.trajectory_dir(cond_index));
492 std::vector<size_type> pass;
493 std::vector<size_type> step;
494 std::vector<ConfigDoF> trajectory;
497 std::string filename =
dir.trajectory_json(cond_index).string() +
".gz";
499 if (!fs::exists(filename)) {
500 throw std::runtime_error(
501 std::string(
"ERROR in 'write_POSCAR_trajectory(const MonteCarlo &mc, "
502 "size_type cond_index)'\n") +
503 " File not found: " + filename);
506 gz::igzstream sin(filename.c_str());
508 for (
auto it = json[
"Pass"].cbegin(); it != json[
"Pass"].
cend(); ++it) {
511 for (
auto it = json[
"Step"].cbegin(); it != json[
"Step"].
cend(); ++it) {
514 for (
auto it = json[
"DoF"].cbegin(); it != json[
"DoF"].
cend(); ++it) {
515 trajectory.push_back(
520 std::string filename =
dir.trajectory_csv(cond_index).string() +
".gz";
522 if (!fs::exists(filename)) {
523 throw std::runtime_error(
524 std::string(
"ERROR in 'write_POSCAR_trajectory(const MonteCarlo &mc, "
525 "size_type cond_index)'\n") +
526 " File not found: " + filename);
529 gz::igzstream sin(filename.c_str());
538 sin >> _pass >> _step;
539 pass.push_back(_pass);
540 step.push_back(_step);
542 sin >> config_dof.
occ(i);
544 trajectory.push_back(config_dof);
548 for (
size_type i = 0; i < trajectory.size(); i++) {
550 std::stringstream ss;
551 ss <<
"Sample: " << i <<
" Pass: " << pass[i] <<
" Step: " << step[i];
554 fs::ofstream sout(
dir.POSCAR_snapshot(cond_index, i));
555 _log <<
"write: " <<
dir.POSCAR_snapshot(cond_index, i) <<
"\n";
Index size() const
Number of sites in the ConfigDoF.
int & occ(Index i)
Reference occupation value on site i.
const ConfigDoF & configdof() const
const Access the DoF
Eigen::VectorBlock< const Eigen::VectorXd > observations() const
Return all observations.
MonteCarloDirectoryStructure(fs::path output_dir)
Interface base class for all types of Monte Carlo simulations (not meant to be used polymorphically)
std::pair< bool, size_type > is_equilibrated() const
Returns pair(true, equil_samples) if required equilibration has occured for all samplers that must co...
const Supercell & supercell() const
const Access the Supercell that *this is based on
const MonteSettings & settings() const
const Access settings used for construction
const PrimClex & primclex() const
const Access the PrimClex that *this is based on
const std::vector< ConfigDoF > & trajectory() const
const Access snapshots of the Monte Carlo calculation
const ConfigDoF & configdof() const
const Access current microstate
const SamplerMap & samplers() const
const Access sampler map
const SampleTimes & sample_times() const
const Access a vector of std::pair<pass, step> indicating when samples were taken
An abstract base class for sampling and storing data observations.
const MCData & data() const
const Access the raw data observation container
Settings for Monte Carlo calculations.
const fs::path output_directory() const
Directory where output should go.
bool write_trajectory() const
Returns true if snapshots are requested.
bool write_observations() const
Writes all observations.
bool write_csv() const
Write csv versions of files? (csv is the default format if no 'output_format' given)
bool write_json() const
Write json versions of files?
Structure specifies the lattice and atomic basis of a crystal.
const std::vector< xtal::Site > & basis() const
Index volume() const
Return number of primitive cells that fit inside of *this.
const Structure & prim() const
Print POSCAR with formating options.
void set_title(std::string title)
Set title.
void sort()
Default sort is by atom name.
void print(std::ostream &sout) const
Print POSCAR to stream.
static jsonParser object()
Returns an empty json object.
static jsonParser array()
Returns an empty json array.
const_iterator cend() const
Returns const_iterator to end of JSON object or JSON array.
void write(const std::string &file_name, unsigned int indent=2, unsigned int prec=12) const
Write json to file.
std::string to_string(ENUM val)
Return string representation of enum class.
const PrimType & prim() const
const Access to primitive Structure
jsonParser & push_back(const T &value, Args &&... args)
T get(Args &&... args) const
Get data from json, using one of several alternatives.
DoF_impl::OccupationDoFTraits occupation()
GenericDatumFormatter< double, std::pair< ConstMonteCarloPtr, size_type > > MonteCarloObservationFormatter(std::string prop_name)
Print value of observation.
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloPrecFormatter(std::string prop_name)
Print calculated precision of property values: prec(<prop_name>)
void write_observations(const MonteSettings &settings, const MonteCarlo &mc, size_type cond_index, Log &_log)
Will create (and possibly overwrite) new file with all observations from run with conditions....
void write_POSCAR_initial(const MonteCarlo &mc, size_type cond_index, Log &_log)
For the initial state, write a POSCAR file.
GenericDatumFormatter< size_type, std::pair< ConstMonteCarloPtr, size_type > > MonteCarloStepFormatter()
Print Step number of observation.
void write_POSCAR_final(const MonteCarlo &mc, size_type cond_index, Log &_log)
For the final state, write a POSCAR file.
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloMeanFormatter(std::string prop_name)
Print mean property values: <prop_name>
DataFormatter< std::pair< ConstMonteCarloPtr, size_type > > make_observation_formatter(const MonteCarlo &mc)
Make a observation formatter.
GenericDatumFormatter< size_type, ConstMonteCarloPtr > MonteCarloNAvgSamplesFormatter()
Print number of samples used in calculating means.
GenericDatumFormatter< bool, ConstMonteCarloPtr > MonteCarloIsConvergedFormatter()
Print if converged.
void write_trajectory(const MonteSettings &settings, const MonteCarlo &mc, size_type cond_index, Log &_log)
Will create (and possibly overwrite) new file with all observations from run with conditions....
GenericDatumFormatter< int, std::pair< ConstMonteCarloPtr, size_type > > MonteCarloOccFormatter(size_type occ_index)
Print value of a particular occupation variable.
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloCovFormatter(std::string prop_name1, std::string prop_name2)
Print covariance: cov(prop_name1, prop_name2)
GenericDatumFormatter< bool, ConstMonteCarloPtr > MonteCarloIsEquilibratedFormatter()
Print if equilibrated (not counting explicitly requested equilibration)
GenericDatumFormatter< size_type, ConstMonteCarloPtr > MonteCarloNEquilSamplesFormatter()
Print number of samples used for equilibration (not counting explicitly requested equilibration)
void write_POSCAR_trajectory(const MonteCarlo &mc, size_type cond_index, Log &_log)
For every snapshot taken, write a POSCAR file.
GenericDatumFormatter< size_type, std::pair< ConstMonteCarloPtr, size_type > > MonteCarloPassFormatter()
Print Pass number of observation.
DataFormatter< std::pair< ConstMonteCarloPtr, size_type > > make_trajectory_formatter(const MonteCarlo &mc)
Make a trajectory formatter.
xtal::SimpleStructure make_simple_structure(Supercell const &_scel, ConfigDoF const &_dof, std::vector< DoFKey > const &_which_dofs={})
Construct from ConfigDoF _dof belonging to provided Supercell _scel.
T max(const T &A, const T &B)
DirectoryStructure const & dir
Functor to help evaluate covariance.
double operator()(const ConstMonteCarloPtr &mc)