deleted comment line :)
[cluster_expansion.git] / LatticeParser.cpp
blob04ef7e123e105e5390b117c7cd0af0aaee6ff9e7
1 #include <stdexcept>
3 #include "LatticeParser.h"
5 LatticeParser::LatticeParser(const char *ini_file)
6 : _ini(false, true, false)
8 SI_Error rc = _ini.LoadFile(ini_file);
9 if (rc < 0)
10 throw runtime_error("Failed loading structure ini file: " +
11 CSimpleIni::str_SI_Error(rc));
13 // Parse UnitCell
14 const CSimpleIni::TKeyVal *section = _ini.GetSection("UnitCell");
15 if (!section)
16 throw runtime_error("Ini file is missing 'UnitCell' section.");
18 #define ASSIGN_KEY(key_name, key_member) \
19 do { \
20 const char *tmp = _ini.GetValue("UnitCell", key_name); \
21 if (!tmp) \
22 throw runtime_error("UnitCell definition is missing " \
23 key_name); \
24 _lattice.key_member = atoi(tmp); \
25 } while (0)
27 ASSIGN_KEY("SizeX", UnitCellSizeX);
28 ASSIGN_KEY("SizeY", UnitCellSizeY);
29 ASSIGN_KEY("Longitude", Longitude);
30 ASSIGN_KEY("Latitude", Latitude);
31 #undef ASSIGN_KEY
33 if (_lattice.UnitCellSizeX < 1 || _lattice.UnitCellSizeY < 1 ||
34 _lattice.Longitude < 1 || _lattice.Latitude < 1)
35 throw runtime_error("Unit cell has unreasonable size.");
37 _lattice.adsorbates.resize(_lattice.Longitude * _lattice.UnitCellSizeX + 1,
38 _lattice.Latitude * _lattice.UnitCellSizeY + 1);
39 _lattice.surface.resize(_lattice.adsorbates.shape());
40 _lattice.surface = empty;
42 section = _ini.GetSection("Surface");
43 if (!section)
44 throw runtime_error("Ini file is missing 'Surface' section. "
45 "Maybe meaningful??");
47 CSimpleIni::TNamesDepend Pd_positions;
48 _ini.GetAllValues("Surface", "Pd", Pd_positions);
49 for (CSimpleIni::TNamesDepend::const_iterator i = Pd_positions.begin();
50 i != Pd_positions.end(); ++i) {
51 double x_frac, y_frac;
52 if (sscanf(i->pItem, "%lf/%lf", &x_frac, &y_frac) != 2 ||
53 x_frac < 0 || x_frac >= 1 ||
54 y_frac < 0 || y_frac >= 1)
55 throw runtime_error("Skrewed coordinates in " +
56 string(i->pItem));
57 _lattice.surface((int)(_lattice.UnitCellSizeX * x_frac),
58 (int)(_lattice.UnitCellSizeY * y_frac)) = Pd;
61 // Parse Adsorbates
62 section = _ini.GetSection("Adsorbates");
63 if (!section)
64 throw runtime_error("Ini file is missing 'Adsorbates' section. "
65 "Maybe meaningful??");
67 CSimpleIni::TNamesDepend CO_positions;
68 _ini.GetAllValues("Adsorbates", "CO", CO_positions);
69 for (CSimpleIni::TNamesDepend::const_iterator i = CO_positions.begin();
70 i != CO_positions.end(); ++i) {
71 double x_frac, y_frac;
72 if (sscanf(i->pItem, "%lf/%lf", &x_frac, &y_frac) != 2 ||
73 x_frac < 0 || x_frac >= 1 ||
74 y_frac < 0 || y_frac >= 1)
75 throw runtime_error("Skrewed coordinates in " +
76 string (i->pItem));
77 _lattice.adsorbates((int)(_lattice.UnitCellSizeX * x_frac),
78 (int)(_lattice.UnitCellSizeY * y_frac)) = CO;
82 // Parse Symmetry options
83 CSimpleIni::TNamesDepend Symmetry_Operations;
84 _ini.GetAllValues("Symmetry", "Sym_Operation", Symmetry_Operations);
85 for (CSimpleIni::TNamesDepend::const_iterator i = Symmetry_Operations.begin();
86 i != Symmetry_Operations.end(); ++i) {
87 double x_frac, y_frac;
88 if (sscanf(i->pItem, "%lf/%lf", &x_frac, &y_frac) != 2)
89 throw runtime_error("Skrewed coordinates in " +
90 string (i->pItem));
91 _lattice.symmetry.directions.push_back(Direction(x_frac, y_frac));
94 _lattice.extendUnitCell();
97 Lattice LatticeParser::getLattice()
99 // FIXME: auto_ptr?
100 return _lattice;