moved checkInteractions from InteractionsParser to Lattice
[cluster_expansion.git] / InteractionsParser.cpp
blobe48388b728b2b0b48d4d293e5aef9353e67b0813
1 #include "InteractionsParser.h"
2 #include "StringTokenizer.h"
4 #include <algorithm>
5 #include <iostream>
6 #include <stdexcept>
8 InteractionsParser::InteractionsParser(const char *ini_filename)
9 : _ini(false, true, false)
11 SI_Error rc = _ini.LoadFile(ini_filename);
12 if (rc < 0)
13 throw runtime_error("Failed loading interaction ini file: " +
14 CSimpleIni::str_SI_Error(rc));
16 const CSimpleIni::TKeyVal *section = _ini.GetSection("Interactions");
17 if (!section)
18 throw runtime_error("Interactions ini file is missing "
19 "'Interactions' section.");
21 CSimpleIni::TNamesDepend interactionTypes;
22 _ini.GetAllKeys("Interactions", interactionTypes);
24 for (CSimpleIni::TNamesDepend::const_iterator key =
25 interactionTypes.begin();
26 key != interactionTypes.end(); ++key) {
28 CSimpleIni::TNamesDepend interactions;
29 _ini.GetAllValues("Interactions", key->pItem, interactions);
31 for (CSimpleIni::TNamesDepend::const_iterator value =
32 interactions.begin();
33 value != interactions.end(); ++value) {
34 parseInteraction(key, value);
40 void InteractionsParser::parseInteraction(
41 CSimpleIni::TNamesDepend::const_iterator key,
42 CSimpleIni::TNamesDepend::const_iterator value)
44 StringTokenizer strtok = StringTokenizer(value->pItem, ",");
45 int cnt = strtok.countTokens();
46 if (cnt == 0)
47 throw runtime_error("Empty \"" + string(key->pItem)
48 + "\" key.");
49 Interaction interaction;
50 interaction.name = "";
52 for (int i = 0; i < cnt; i++) {
53 double x_frac, y_frac;
54 string tmp = strtok.nextToken();
55 if (sscanf(tmp.c_str(), "%lf/%lf", &x_frac, &y_frac) != 2 ||
56 x_frac < 0 || x_frac >= 1 ||
57 y_frac < 0 || y_frac >= 1) {
58 // Treat last tuple as comment
59 if (i==cnt-1)
60 interaction.name = tmp;
61 else
62 throw runtime_error("Skrewed fraction coordinates: " +
63 string(tmp));
65 interaction.directions.push_back(
66 Direction(x_frac, y_frac));
68 // TODO: check cardinality (trio, etc...)
69 _interactions.push_back(interaction);
72 Interactions InteractionsParser::getInteractions()
74 // FIXME: auto_ptr
75 return _interactions;