added Structure.h file for Structure data structure
[cluster_expansion.git] / InteractionsParser.cpp
bloba630cccf748726817541e6575fad6c658bc8a9e0
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; // interactions may be given as absolute coordinates and scaled to unit cell size later!
54 string tmp = strtok.nextToken();
55 if (sscanf(tmp.c_str(), "%lf/%lf", &x_frac, &y_frac) != 2 ||
56 x_frac < 0 || y_frac < 0) {
57 // Treat last tuple as comment
58 if (i==cnt-1)
59 interaction.name = tmp;
60 else
61 throw runtime_error("Skrewed fraction coordinates: " +
62 string(tmp));
64 interaction.directions.push_back(
65 Direction(x_frac, y_frac));
67 // TODO: check cardinality (trio, etc...)
68 _interactions.push_back(interaction);
71 Interactions InteractionsParser::getInteractions()
73 // FIXME: auto_ptr
74 return _interactions;