Tweaked .gitignore
[cluster_expansion.git] / LatticeParser.cpp
blob57b5122f2ee38ed92153fc2f08a5730d1437b41c
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 const char *tmp;
19 #define ASSIGN_KEY(key_name, name) \
20 tmp = _ini.GetValue("UnitCell", key_name); \
21 if (!tmp) \
22 throw runtime_error("UnitCell definition is missing " \
23 key_name); \
24 int name = atoi(tmp); \
26 ASSIGN_KEY("UnitCellSizeX", UnitCellSizeX);
27 ASSIGN_KEY("UnitCellSizeY", UnitCellSizeY);
28 ASSIGN_KEY("GranularityX", GranularityX);
29 ASSIGN_KEY("GranularityY", GranularityY);
30 #undef ASSIGN_KEY
32 if (UnitCellSizeX < 1 || UnitCellSizeY < 1 || GranularityX < 1 || GranularityY < 1)
33 throw runtime_error("Unit cell has unreasonable size.");
35 _lattice = new Lattice(UnitCellSizeX, UnitCellSizeY, GranularityX, GranularityY);
37 section = _ini.GetSection("Surface");
38 if (!section)
39 throw runtime_error("Ini file is missing 'Surface' section. ");
41 CSimpleIni::TNamesDepend Pd_positions;
42 _ini.GetAllValues("Surface", "Pd", Pd_positions);
43 for (CSimpleIni::TNamesDepend::const_iterator i = Pd_positions.begin();
44 i != Pd_positions.end(); ++i) {
45 double x_frac, y_frac;
46 if (sscanf(i->pItem, "%lf/%lf", &x_frac, &y_frac) != 2 ||
47 x_frac < 0 || x_frac >= 1 ||
48 y_frac < 0 || y_frac >= 1)
49 throw runtime_error("Skrewed coordinates in " +
50 string(i->pItem));
51 _lattice->surface((int)(_lattice->UnitCellSizeX * x_frac * _lattice->GranularityX),
52 (int)(_lattice->UnitCellSizeY * y_frac * _lattice->GranularityY)) = Pd;
55 // Parse Adsorbates
56 section = _ini.GetSection("Adsorbates");
57 if (!section)
58 throw runtime_error("Ini file is missing 'Adsorbates' section. ");
60 CSimpleIni::TNamesDepend CO_positions;
61 _ini.GetAllValues("Adsorbates", "CO", CO_positions);
62 for (CSimpleIni::TNamesDepend::const_iterator i = CO_positions.begin();
63 i != CO_positions.end(); ++i) {
64 double x_frac, y_frac;
65 if (sscanf(i->pItem, "%lf/%lf", &x_frac, &y_frac) != 2 ||
66 x_frac < 0 || x_frac >= 1 ||
67 y_frac < 0 || y_frac >= 1)
68 throw runtime_error("Skrewed coordinates in " +
69 string (i->pItem));
70 _lattice->adsorbates((int)(_lattice->UnitCellSizeX * x_frac * _lattice->GranularityX),
71 (int)(_lattice->UnitCellSizeY * y_frac * _lattice->GranularityY)) = CO;
75 // Parse Symmetry options
76 CSimpleIni::TNamesDepend Symmetry_Operations;
77 _ini.GetAllValues("Symmetry", "Sym_Operation", Symmetry_Operations);
78 for (CSimpleIni::TNamesDepend::const_iterator i = Symmetry_Operations.begin();
79 i != Symmetry_Operations.end(); ++i) {
80 double x_frac, y_frac;
81 if (sscanf(i->pItem, "%lf/%lf", &x_frac, &y_frac) != 2)
82 throw runtime_error("Skrewed coordinates in " +
83 string (i->pItem));
84 _lattice->_symmetryOperations.directions.push_back(Direction(x_frac, y_frac));
88 Lattice *LatticeParser::getLattice()
90 // FIXME: auto_ptr?
91 return _lattice;