test commit and push upstream to origin
[cluster_expansion_mc.git] / LatticeParser.cpp
blobf5e73aeffdc6e17bc18b82059e499eb7e16de783
1 #include <stdexcept>
3 #include "LatticeParser.h"
5 LatticeParser::LatticeParser(const char *ini_file) :
6 _ini(false, true, false) {
7 SI_Error rc = _ini.LoadFile(ini_file);
8 if (rc < 0)
9 throw runtime_error("Failed loading structure ini file: "
10 + CSimpleIni::str_SI_Error(rc));
12 // Parse UnitCell
13 const CSimpleIni::TKeyVal *section = _ini.GetSection("UnitCell");
14 if (!section)
15 throw runtime_error("Ini file is missing 'UnitCell' section.");
17 const char *tmp;
18 #define ASSIGN_KEY(key_name, name) \
19 tmp = _ini.GetValue("UnitCell", key_name); \
20 if (!tmp) \
21 throw runtime_error("UnitCell definition is missing " \
22 key_name); \
23 int name = atoi(tmp); \
25 ASSIGN_KEY("UnitCellSizeX", UnitCellSizeX);
26 ASSIGN_KEY("UnitCellSizeY", UnitCellSizeY);
27 ASSIGN_KEY("GranularityX", GranularityX);
28 ASSIGN_KEY("GranularityY", GranularityY);
29 #undef ASSIGN_KEY
31 if (UnitCellSizeX < 1 || UnitCellSizeY < 1 || GranularityX < 1
32 || GranularityY < 1)
33 throw runtime_error("Unit cell has unreasonable size.");
35 // increasing Granularity to get proper structure representation
36 // GranularityX = GranularityX + 1;
37 // GranularityY = GranularityY + 1;
39 _lattice
40 = new Lattice(UnitCellSizeX, UnitCellSizeY, GranularityX, GranularityY);
41 _lattice->surface = empty;
42 _lattice->adsorbates = empty;
44 section = _ini.GetSection("Surface");
45 if (!section)
46 throw runtime_error("Ini file is missing 'Surface' section. ");
48 CSimpleIni::TNamesDepend Pd_positions;
49 _ini.GetAllValues("Surface", "Pd", Pd_positions);
50 for (CSimpleIni::TNamesDepend::const_iterator i = Pd_positions.begin(); i
51 != Pd_positions.end(); ++i) {
52 double x_frac, y_frac;
53 if (sscanf(i->pItem, "%lf/%lf", &x_frac, &y_frac) != 2 || x_frac < 0
54 || x_frac >= 1 || y_frac < 0 || y_frac >= 1)
55 throw runtime_error("Skrewed coordinates in " + string(i->pItem));
56 int inter_x, inter_y;
57 inter_x = _lattice->UnitCellSizeX * _lattice->GranularityX;
58 inter_y = _lattice->UnitCellSizeY * _lattice->GranularityY;
59 double double_x, double_y;
60 double_x = inter_x * x_frac;
61 double_y = inter_y * y_frac;
62 int pos_x, pos_y;
63 pos_x = round(double_x);
64 pos_y = round(double_y);
65 cout << "filling (" << pos_x << "/" << pos_y << ") with Pd" << endl;
66 _lattice->surface(pos_x, pos_y) = Pd;
69 // Parse Adsorbates
70 section = _ini.GetSection("Adsorbates");
71 if (!section)
72 throw runtime_error("Ini file is missing 'Adsorbates' section. ");
74 CSimpleIni::TNamesDepend CO_positions;
75 _ini.GetAllValues("Adsorbates", "CO", CO_positions);
76 _lattice->nrCO = CO_positions.size();
77 for (CSimpleIni::TNamesDepend::const_iterator i = CO_positions.begin(); i
78 != CO_positions.end(); ++i) {
79 double x_frac, y_frac;
80 if (sscanf(i->pItem, "%lf/%lf", &x_frac, &y_frac) != 2 || x_frac < 0
81 || x_frac >= 1 || y_frac < 0 || y_frac >= 1)
82 throw runtime_error("Skrewed coordinates in " + string(i->pItem));
83 int inter_x, inter_y;
84 inter_x = _lattice->UnitCellSizeX * _lattice->GranularityX;
85 inter_y = _lattice->UnitCellSizeY * _lattice->GranularityY;
86 double double_x, double_y;
87 double_x = inter_x * x_frac;
88 double_y = inter_y * y_frac;
89 int pos_x, pos_y;
90 pos_x = round(double_x);
91 pos_y = round(double_y);
92 cout << "filling (" << pos_x << "/" << pos_y << ") with CO" << endl;
93 _lattice->adsorbates(pos_x, pos_y) = CO;
96 // Parse Symmetry options
98 CSimpleIni::TNamesDepend Symmetry_Operations;
99 _ini.GetAllValues("Symmetry", "Sym_Operation", Symmetry_Operations);
100 for (CSimpleIni::TNamesDepend::const_iterator i =
101 Symmetry_Operations.begin(); i != Symmetry_Operations.end(); ++i) {
102 double x_frac, y_frac;
103 if (sscanf(i->pItem, "%lf/%lf", &x_frac, &y_frac) != 2)
104 throw runtime_error("Skrewed coordinates in " + string(i->pItem));
105 _lattice->_symmetryOperations.directions.push_back(Direction(x_frac,
106 y_frac));
111 Lattice *LatticeParser::getLattice() {
112 // FIXME: auto_ptr?
113 return _lattice;