1 #ifndef CASM_MonteIO_impl_HH
2 #define CASM_MonteIO_impl_HH
4 #include <boost/filesystem.hpp>
5 #include <boost/filesystem/fstream.hpp>
23 template <
typename MonteType>
27 return static_cast<const MonteType *
>(ptr)->conditions().temperature();
35 template <
typename MonteType>
39 return static_cast<const MonteType *
>(ptr)->conditions().beta();
47 template <
typename MonteType>
52 return static_cast<const MonteType *
>(ptr)->conditions().param_chem_pot()(
55 std::string header = std::string(
"param_chem_pot(") +
63 template <
typename MonteType>
65 const MonteType &mc,
int index) {
68 return static_cast<const MonteType *
>(ptr)->conditions().chem_pot(index);
70 std::string header = std::string(
"chem_pot(") +
78 template <
typename MonteType>
80 const MonteType &mc,
int index) {
83 return static_cast<const MonteType *
>(ptr)
85 .param_composition()(index);
95 template <
typename MonteType>
97 const MonteType &mc,
int index) {
100 return static_cast<const MonteType *
>(ptr)->conditions().mol_composition()(
103 std::string header = std::string(
"comp_n(") +
112 template <
typename MonteType>
114 const MonteType &mc,
int index) {
116 const MonteType *mc_ptr =
static_cast<const MonteType *
>(mc);
121 std::string header = std::string(
"site_frac(") +
129 template <
typename MonteType>
131 const MonteType &mc,
int index) {
133 const MonteType *mc_ptr =
static_cast<const MonteType *
>(mc);
135 if (mc->primclex().vacancy_allowed()) {
136 comp_n(mc->primclex().vacancy_index()) = 0.0;
139 return atom_frac[index];
141 std::string header = std::string(
"atom_frac(") +
151 template <
typename MonteType>
155 CovEvaluator cov_evaluator(
"potential_energy",
"potential_energy");
156 auto N = mc->supercell().volume();
158 auto T =
static_cast<const MonteType *
>(ptr)->conditions().temperature();
159 return cov_evaluator(mc) * N / (
KB * T * T);
163 return mc->is_equilibrated().first;
166 std::string header = std::string(
"heat_capacity");
169 header, header, evaluator, validator);
178 template <
typename MonteType>
180 std::string comp_var_i, std::string comp_var_j) {
181 std::string name_i =
"comp(" + comp_var_i +
")";
182 std::string name_j =
"comp(" + comp_var_j +
")";
186 auto N = mc->supercell().volume();
188 auto T =
static_cast<const MonteType *
>(ptr)->conditions().temperature();
189 return cov_evaluator(mc) * N / (
KB * T);
193 return mc->is_equilibrated().first;
197 std::string(
"susc_x(" + comp_var_i +
"," + comp_var_j +
")");
200 header, header, evaluator, validator);
209 template <
typename MonteType>
211 std::string species_i, std::string species_j) {
212 std::string name_i =
"comp_n(" + species_i +
")";
213 std::string name_j =
"comp_n(" + species_j +
")";
217 auto N = mc->supercell().volume();
219 auto T =
static_cast<const MonteType *
>(ptr)->conditions().temperature();
220 return cov_evaluator(mc) * N / (
KB * T);
224 return mc->is_equilibrated().first;
228 std::string(
"susc_n(" + species_i +
"," + species_j +
")");
231 header, header, evaluator, validator);
240 template <
typename MonteType>
243 std::string name_i =
"comp(" + comp_var_i +
")";
247 auto N = mc->supercell().volume();
249 auto T =
static_cast<const MonteType *
>(ptr)->conditions().temperature();
250 return cov_evaluator(mc) * N / (
KB * T);
254 return mc->is_equilibrated().first;
257 std::string header = std::string(
"susc_x(S," + comp_var_i +
")");
260 header, header, evaluator, validator);
264 template <
typename MonteType>
267 std::string name_i =
"comp_n(" + species_i +
")";
271 auto N = mc->supercell().volume();
273 auto T =
static_cast<const MonteType *
>(ptr)->conditions().temperature();
274 return cov_evaluator(mc) * N / (
KB * T);
278 return mc->is_equilibrated().first;
281 std::string header = std::string(
"susc_n(S," + species_i +
")");
284 header, header, evaluator, validator);
289 template <
typename MonteType>
299 _log <<
"write: " <<
dir.results_csv() <<
"\n";
300 fs::path file =
dir.results_csv();
303 if (!fs::exists(file)) {
305 formatter.print_header(&mc, sout);
307 sout.open(file, std::ios::app);
310 formatter.print(&mc, sout);
317 _log <<
"write: " <<
dir.results_json() <<
"\n";
318 fs::path file =
dir.results_json();
321 if (fs::exists(file)) {
327 formatter.to_json_arrays(&mc, results);
331 std::cerr <<
"ERROR writing results" << std::endl;
337 template <
typename MonteType>
342 fs::create_directories(
dir.conditions_dir(cond_index));
344 to_json(mc.conditions(), json);
345 _log <<
"write: " <<
dir.conditions_json(cond_index) <<
"\n";
346 json.
write(
dir.conditions_json(cond_index));
348 std::cerr <<
"ERROR writing conditions.json" << std::endl;
static std::string comp_var(size_type i)
Composition variable names: "a", "b", ...
Interface base class for all types of Monte Carlo simulations (not meant to be used polymorphically)
Settings for Monte Carlo calculations.
const fs::path output_directory() const
Directory where output should go.
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?
static jsonParser object()
Returns an empty json object.
bool read(std::istream &stream)
Reads json from the stream.
void write(const std::string &file_name, unsigned int indent=2, unsigned int prec=12) const
Write json to file.
Eigen::VectorXd comp_n(const Configuration &config)
Returns the composition, as number of each species per unit cell.
Eigen::VectorXd site_frac(const Configuration &config)
Returns the composition as site fraction, in the order of Structure::get_struc_molecule.
std::vector< std::string > struc_molecule_name(BasicStructure const &_struc)
Returns an Array of each possible Molecule in this Structure.
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloSiteFracFormatter(const MonteType &mc, int index)
Print site_frac(x) for any class MonteType with valid 'Eigen::VectorXd MonteType::comp_n'.
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloSuscXFormatter(std::string comp_var_i, std::string comp_var_j)
Print parametric susceptibility, 'susc_x(a,b)'.
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloParamChemPotFormatter(const MonteType &mc, int index)
Print param_chem_pot(x)
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloChemPotFormatter(const MonteType &mc, int index)
Print chem_pot(N)
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloTFormatter()
Print Temperature.
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloAtomFracFormatter(const MonteType &mc, int index)
Print atom_frac(X) for any class MonteType with valid 'Eigen::VectorXd MonteType::comp_n'.
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloThermoChemSuscNFormatter(std::string species_i)
Print thermo-chemical susceptibility, 'susc_n(S,A)'.
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloSuscNFormatter(std::string species_i, std::string species_j)
Print susceptibility, 'susc_n(A,B)'.
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloBetaFormatter()
Print Beta.
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloThermoChemSuscXFormatter(std::string comp_var_i)
Print parametric thermo-chemical susceptibility, 'susc_x(S,a)'.
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloCompNFormatter(const MonteType &mc, int index)
Print comp_n(N)
void write_results(const MonteSettings &settings, const MonteType &mc, Log &_log)
Will create new file or append to existing file results of the latest run.
void write_conditions_json(const MonteSettings &settings, const MonteType &mc, size_type cond_index, Log &_log)
Write conditions to conditions.cond_index directory.
jsonParser & to_json(const CanonicalConditions &conditions, jsonParser &json)
Store CanonicalConditions in JSON format.
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloHeatCapacityFormatter()
Print heat capacity, 'heat_capacity'.
GenericDatumFormatter< double, ConstMonteCarloPtr > MonteCarloCompFormatter(const MonteType &mc, int index)
Print comp(x)
DataFormatter< ConstMonteCarloPtr > make_results_formatter(const Canonical &mc)
Make a LTE results formatter.
DirectoryStructure const & dir
Functor to help evaluate covariance.