start of adding symmetry parsing to LatticeParser.cpp
[cluster_expansion.git] / LatticeParser.cpp
blob7ec4beb5fc4bb48f8943a43664888539c564b117
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.adsorbates = empty;
40 _lattice.surface.resize(_lattice.adsorbates.shape());
41 _lattice.surface = empty;
43 section = _ini.GetSection("Surface");
44 if (!section)
45 throw runtime_error("Ini file is missing 'Surface' section. "
46 "Maybe meaningful??");
48 CSimpleIni::TNamesDepend Pd_positions;
49 _ini.GetAllValues("Surface", "Pd", Pd_positions);
50 for (CSimpleIni::TNamesDepend::const_iterator i = Pd_positions.begin();
51 i != Pd_positions.end(); ++i) {
52 double x_frac, y_frac;
53 if (sscanf(i->pItem, "%lf/%lf", &x_frac, &y_frac) != 2 ||
54 x_frac < 0 || x_frac >= 1 ||
55 y_frac < 0 || y_frac >= 1)
56 throw runtime_error("Skrewed coordinates in " +
57 string(i->pItem));
58 _lattice.surface((int)(_lattice.UnitCellSizeX * x_frac),
59 (int)(_lattice.UnitCellSizeY * y_frac)) = Pd;
62 // Parse Adsorbates
63 section = _ini.GetSection("Adsorbates");
64 if (!section)
65 throw runtime_error("Ini file is missing 'Adsorbates' section. "
66 "Maybe meaningful??");
68 CSimpleIni::TNamesDepend CO_positions;
69 _ini.GetAllValues("Adsorbates", "CO", CO_positions);
70 for (CSimpleIni::TNamesDepend::const_iterator i = CO_positions.begin();
71 i != CO_positions.end(); ++i) {
72 double x_frac, y_frac;
73 if (sscanf(i->pItem, "%lf/%lf", &x_frac, &y_frac) != 2 ||
74 x_frac < 0 || x_frac >= 1 ||
75 y_frac < 0 || y_frac >= 1)
76 throw runtime_error("Skrewed coordinates in " +
77 string (i->pItem));
78 _lattice.adsorbates((int)(_lattice.UnitCellSizeX * x_frac),
79 (int)(_lattice.UnitCellSizeY * y_frac)) = CO;
82 // Parse Symmetry options
85 _lattice.extendUnitCell();
88 Lattice LatticeParser::getLattice()
90 // FIXME: auto_ptr?
91 return _lattice;