Tweaked .gitignore
[cluster_expansion.git] / StructureParser.cpp
blob0239755f8269ce0331a5211cbc67539d6d3f0ee9
1 #include <stdexcept>
2 #include "StringTokenizer.h"
4 #include <algorithm>
5 #include <iostream>
6 #include <stdexcept>
8 #include "StructureParser.h"
13 StructureParser::StructureParser(const char *ini_file_structure, const char *ini_file_interactions)
14 : _ini_structure(false, true, false)
17 // Parse Lattice Structure
18 LatticeParser latticeParser(ini_file_structure);
19 _structure.lattice = latticeParser.getLattice();
20 cout << _structure.lattice;
22 // Parse Interactions
23 InteractionsParser interactionsParser(ini_file_interactions);
24 _structure.interactions = interactionsParser.getInteractions();
26 cout << "interactions (before assessment):" << endl << _structure.interactions << endl;
27 _structure.lattice.assessInteractions(_structure.interactions);
28 cout << "interactions (after assessment):" << endl << _structure.interactions;
30 // Parse Structure Energetics
31 SI_Error rc = _ini_structure.LoadFile(ini_file_structure);
32 if (rc < 0)
33 throw runtime_error("Failed loading structure ini file: " +
34 CSimpleIni::str_SI_Error(rc));
36 double energy_value = 0.0;
37 CSimpleIni::TNamesDepend energies;
38 _ini_structure.GetAllKeys("OnSiteEnergies", energies);
39 for (CSimpleIni::TNamesDepend::const_iterator i = energies.begin();
40 i != energies.end(); ++i) {
41 CSimpleIni::TNamesDepend values;
42 _ini_structure.GetAllValues("OnSiteEnergies", i->pItem, values);
43 for (CSimpleIni::TNamesDepend::const_iterator k = values.begin();
44 k != values.end(); ++k) {
45 string species = "";
46 StringTokenizer strtok = StringTokenizer(k->pItem, ",");
47 int cnt = strtok.countTokens();
48 if (cnt == 0)
49 throw runtime_error("Empty \"" + string(k->pItem)
50 + "\" key.");
51 for (int i = 0; i < cnt; i++) {
52 string tmp = strtok.nextToken();
53 if (sscanf(tmp.c_str(), "%lf", &energy_value) != 1) {
54 // Treat last tuple as Occupation specifier
55 if (i==cnt-1)
56 species = tmp;
57 else
58 throw runtime_error("On Site Energy definition is wrong in: " +
59 string(tmp));
63 // FIXME : checking for different adsorbate species, defined by name
64 _structure.onSiteEnergy = energy_value;
67 CSimpleIni::TNamesDepend dft_energies;
68 if (!(_ini_structure.GetAllValues("DFT_Energy", "E0", dft_energies)))
69 _structure.newStructure = true;
70 else {
71 double dft_energy = 0.0;
72 for (CSimpleIni::TNamesDepend::const_iterator l = dft_energies.begin();
73 l != dft_energies.end(); ++l) {
74 if (sscanf(l->pItem, "%lf", &dft_energy) != 1)
75 throw runtime_error("DFT Energy definition is wrong in: " + string(l->pItem));
76 _structure.dftEnergy = dft_energy;
82 Structure StructureParser::getStructure()
84 // FIXME: auto_ptr?
85 return _structure;