CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
view.cc
Go to the documentation of this file.
5 #include "casm/casm_io/VaspIO.hh"
7 
9 
10 namespace CASM {
11 
12  namespace Completer {
13 
15 
20  m_desc.add_options()
21  ("relaxed,r", "Attempt to display corresponding relaxed structures to the given configurations.");
22  return;
23  }
24  }
25 
26  int view_command(const CommandArgs &args) {
27 
28  fs::path selection;
29  po::variables_map vm;
30  //bool force;
31  std::vector<std::string> confignames;
32 
33 
34 
35  // Set command line options using boost program_options
36  Completer::ViewOption view_opt;
37 
38  // allow confignames as positional options
39  po::positional_options_description p;
40  p.add("confignames", -1);
41 
42  try {
43  po::store(po::command_line_parser(args.argc - 1, args.argv + 1).options(view_opt.desc()).positional(p).run(), vm);
44  //po::store(po::parse_command_line(argc, argv, view_opt.desc()), vm); // can throw
45 
48  if(vm.count("help")) {
49  args.log << "\n";
50  args.log << view_opt.desc() << std::endl;
51 
52  return 0;
53  }
54 
55  if(vm.count("desc")) {
56  args.log << "\n";
57  args.log << view_opt.desc() << std::endl;
58  args.log << "This allows opening visualization programs directly from \n"
59  "CASM. It iterates over all selected configurations and \n"
60  "one by one writes a POSCAR and executes \n"
61  " '$VIEW_COMMAND /path/to/POSCAR' \n"
62  "where $VIEW_COMMAND is set via 'casm settings --set-view-command'.\n"
63  "A script 'casm.view' is included with can be used to run \n"
64  "a command and then pause 1s, which is useful for opening \n"
65  "POSCARs with VESTA. An example on Mac might look like: \n"
66  " casm settings --set-view-command 'casm.view \"open -a /Applications/VESTA/VESTA.app\"' \n\n";
67 
68  return 0;
69  }
70 
71  po::notify(vm); // throws on error, so do after help in case
72  // there are any problems
73 
74  selection = view_opt.selection_path().string();
75  confignames = view_opt.config_strs();
76  }
77  catch(po::error &e) {
78  args.err_log << "ERROR: " << e.what() << std::endl << std::endl;
79  args.err_log << view_opt.desc() << std::endl;
80  return ERR_INVALID_ARG;
81  }
82  catch(std::exception &e) {
83  args.err_log << "Unhandled Exception reached the top of main: "
84  << e.what() << ", application will now exit" << std::endl;
85  return ERR_UNKNOWN;
86 
87  }
88 
89  const fs::path &root = args.root;
90  if(root.empty()) {
91  args.err_log.error("No casm project found");
92  args.err_log << std::endl;
93  return ERR_NO_PROJ;
94  }
95 
96  DirectoryStructure dir(root);
97  ProjectSettings set(root);
98  if(set.view_command().empty()) {
99  args.err_log << "Error in 'casm view': No command set. Use 'casm settings "
100  "--set-view-command' to set the command to open visualization "
101  "software. It should take one argument, the path to a POSCAR "
102  "to be visualized. For example, to use VESTA on Mac: casm settings --set-view-command 'casm.view \"open -a /Applications/VESTA/VESTA.app\"'.\n";
103  return ERR_MISSING_DEPENDS;
104  }
105 
106  // If 'args.primclex', use that, else construct PrimClex in 'uniq_primclex'
107  // Then whichever exists, store reference in 'primclex'
108  std::unique_ptr<PrimClex> uniq_primclex;
109  PrimClex &primclex = make_primclex_if_not(args, uniq_primclex);
110 
111  ConfigSelection<false> config_select;
112  if(!vm.count("config")) {
113  config_select = ConfigSelection<false>(primclex, "NONE");
114  }
115  else if(selection == "MASTER") {
116  config_select = ConfigSelection<false>(primclex);
117  }
118  else {
119  config_select = ConfigSelection<false>(primclex, selection);
120  }
121 
122  // add --confignames (or positional) input
123  for(int i = 0; i < confignames.size(); i++) {
124  config_select.set_selected(confignames[i], true);
125  }
126 
127  fs::path tmp_dir = root / ".casm" / "tmp";
128  fs::create_directory(tmp_dir);
129 
130  // execute the 'casm view' command for each selected configuration
131  for(auto it = config_select.selected_config_cbegin(); it != config_select.selected_config_cend(); ++it) {
132  //for relaxed structure
133  if(vm.count("relaxed") && is_calculated(*it)) {
134  fs::ofstream file;
135  fs::path POSCARpath = tmp_dir / "POSCAR";
136 
137  //Make pos_path the path to properties.calc.json constructed from it
138  fs::path pos_path = it->calc_properties_path();
139  args.log << "Obtaining relaxed structure from:\n";
140  args.log << pos_path.string() << std::endl;
141  BasicStructure<Site> import_struc;
142  jsonParser datajson(pos_path);
143  from_json(simple_json(import_struc, "relaxed_"), datajson);
144 
145  file.open(POSCARpath);
146  VaspIO::PrintPOSCAR p(import_struc);
147  p.sort();
148  p.print(file);
149  file.close();
150 
151  args.log << it->name() << " relaxed" << ":\n";
152  Popen popen;
153  popen.popen(set.view_command() + " " + POSCARpath.string());
154  popen.print(std::cout);
155  }
156 
157  else {
158  // write '.casm/tmp/POSCAR'
159  fs::ofstream file;
160  fs::path POSCARpath = tmp_dir / "POSCAR";
161  file.open(POSCARpath);
162  VaspIO::PrintPOSCAR p(*it);
163  p.sort();
164  p.print(file);
165  file.close();
166  if(vm.count("relaxed")) {
167  args.log << "No relaxed structure found." << "\n";
168  }
169  args.log << it->name() << ":\n";
170  Popen popen;
171  popen.popen(set.view_command() + " " + POSCARpath.string());
172  popen.print(args.log);
173  }
174 
175  }
176 
177  return 0;
178  };
179 
180 }
Data structure holding basic CASM command info.
#define ERR_UNKNOWN
void from_json(ClexDescription &desc, const jsonParser &json)
const_iterator selected_config_cbegin() const
#define ERR_INVALID_ARG
Specification of CASM project directory structure.
PrimClex * primclex
Definition: settings.cc:101
void add_help_suboption()
Add a plain –help suboption.
Definition: Handlers.cc:276
int view_command(const CommandArgs &args)
Definition: view.cc:26
void popen(std::string _command)
Execute popen for a given command.
Definition: Popen.cc:19
void print(std::ostream &sout) const
Print the last command executed and the resulting stdout.
Definition: Popen.cc:49
Main CASM namespace.
Definition: complete.cpp:8
const fs::path & selection_path() const
Returns the string corresponding to add_config_suboption()
Definition: Handlers.cc:168
Remember how to use popen.
Definition: Popen.hh:12
ProjectSettings & set
Definition: settings.cc:103
void print(std::ostream &sout)
Print POSCAR to stream.
Definition: VaspIO.hh:509
void set_selected(const std::string &configname, bool is_selected)
void add_configlist_nodefault_suboption()
Add –config suboption (no default)
Definition: Handlers.cc:258
const po::options_description & desc()
Get the program options, filled with the initialized values.
Definition: Handlers.cc:160
Read/modify settings of an already existing CASM project.
void sort()
Default sort is by atom name.
Definition: VaspIO.hh:500
bool is_calculated(const Configuration &config)
Return true if all current properties have been been calculated for the configuration.
po::options_description m_desc
Boost program options. All the derived classes have them, but will fill them up themselves.
Definition: Handlers.hh:126
const std::vector< std::string > & config_strs() const
Returns the names of the supercells for add_configname_suboption(), for when multiple=false.
Definition: Handlers.cc:204
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:52
const_iterator selected_config_cend() const
SimpleJSonSiteStructure< true > simple_json(const BasicStructure< Site > &struc, const std::string &prefix)
Definition: jsonStruc.hh:110
void add_confignames_suboption()
Add a –confignames suboption.
Definition: Handlers.cc:365
DirectoryStructure & dir
Definition: settings.cc:102
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...
Print POSCAR with formating options.
Definition: VaspIO.hh:232
void error(const std::string &what)
Definition: Log.hh:86
void initialize() override
Fill in the options descriptions accordingly.
Definition: view.cc:16
std::string view_command() const
Get current command used by 'casm view'.
#define ERR_MISSING_DEPENDS
#define ERR_NO_PROJ