test commit and push upstream to origin
[cluster_expansion_mc.git] / InteractionsParser.cpp
blobd31d94b751b8e1f69def7fc9f692dfd1cde9b908
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) {
10 SI_Error rc = _ini.LoadFile(ini_filename);
11 if (rc < 0)
12 throw runtime_error("Failed loading interaction ini file: "
13 + CSimpleIni::str_SI_Error(rc));
15 const CSimpleIni::TKeyVal *section = _ini.GetSection("Interactions");
16 if (!section)
17 throw runtime_error("Interactions ini file is missing "
18 "'Interactions' section.");
20 CSimpleIni::TNamesDepend interactionTypes;
21 _ini.GetAllKeys("Interactions", interactionTypes);
23 for (CSimpleIni::TNamesDepend::const_iterator key =
24 interactionTypes.begin(); key != interactionTypes.end(); ++key) {
26 CSimpleIni::TNamesDepend interactions;
27 _ini.GetAllValues("Interactions", key->pItem, interactions);
29 for (CSimpleIni::TNamesDepend::const_iterator value =
30 interactions.begin(); value != interactions.end(); ++value) {
31 parseInteraction(key, value);
37 void InteractionsParser::parseInteraction(
38 CSimpleIni::TNamesDepend::const_iterator key,
39 CSimpleIni::TNamesDepend::const_iterator value) {
40 StringTokenizer strtok = StringTokenizer(value->pItem, ",");
41 int cnt = strtok.countTokens();
42 // cout << "cnt is " << cnt << endl;
43 if (cnt == 0)
44 throw runtime_error("Empty \"" + string(key->pItem) + "\" key.");
45 Interaction interaction;
46 interaction.name = "";
48 for (int i = 0; i < cnt; i++) {
49 double x_frac, y_frac;
50 double energyValue = 0.0;
51 string tmp = strtok.nextToken();
52 cout << "tmp is: " << tmp << endl;
53 string::size_type loc = tmp.find( "/", 0 );
54 if (sscanf(tmp.c_str(), "%lf/%lf", &x_frac, &y_frac) != 2) // || x_frac < 0 || y_frac < 0) // interactions may have negative vectors!
56 if (sscanf(tmp.c_str(), "%lf", &energyValue) == 1 && i == cnt - 2)
57 interaction.energy = energyValue;
58 // Treat last tuple as comment
59 else if (i == cnt -1 ) {
60 interaction.name = tmp;
61 break;
63 else
64 throw runtime_error("Skrewed interaction definition in: "
65 + string(tmp));
67 if (i != cnt and (loc != string::npos)) {
68 cout << "pushing x: " << x_frac << " and y: " << y_frac << endl;
69 interaction.directions.push_back(Direction(x_frac, y_frac));
72 // TODO: check cardinality (trio, etc...)
74 cout << "read the following values: " << endl;
75 cout << "name: " << interaction.name << endl;
76 cout << "directions: "<< endl;
77 int counter = 0;
78 for (Directions::iterator i = interaction.directions.begin(); i != interaction.directions.end(); i++){
79 cout << counter << "th direction has x= " << i->x << " and y=" << i->y << endl;
80 counter++;
83 _interactions.push_back(interaction);
86 Interactions InteractionsParser::getInteractions() {
87 // TODO: auto_ptr
89 /* DEBUG
90 cout << "before sorting " << endl;
91 for (Interactions::iterator i = _interactions.begin(); i != _interactions.end(); i++){
92 cout << i->name << endl;
95 // automatically sorting interactions lexicographically to given name from interactions.ini
96 sort(_interactions.begin(), _interactions.end(), Interactions::compareInteraction);
98 cout << "after sorting " << endl;
99 for (Interactions::iterator i = _interactions.begin(); i != _interactions.end(); i++){
100 cout << i->name << endl;
104 return _interactions;