added includes for proper compiling under linux
[cluster_expansion.git] / main.cpp
blobaa5df4b77379afeba781ba729d261de74af6a9f1
1 /*
2 * Determination of lateral interaction parameters on a c(2x2) square lattice
3 * using linear combinations to express each interaction and then counting
4 * them in terms of cluster-expansion coefficients
6 * Michael Rieger, FHI, 2008
8 */
10 #include <iostream>
11 #include <string>
12 #include <sstream>
13 #include <stdio.h>
14 #include <stdarg.h>
16 #include <blitz/array.h> // http://www.oonumerics.org/blitz/
17 #include <random/uniform.h> // blitz Random Number Generator - Mersenne Twister type
18 #include "SimpleIni.h" // Ini File Parser
20 using namespace std;
21 using namespace blitz;
22 using namespace ranlib;
25 enum Occupation {
26 empty = 0,
27 PD,
31 class Lattice {
32 public:
33 Array<Occupation, 2> sites;
34 int Latitude, Longitude;
35 int UnitCellSizeX, UnitCellSizeY;
38 void usage(const char *prog)
40 fprintf(stderr, "usage: %s ini-file\n", prog);
41 exit(EXIT_FAILURE);
44 void panic(char *msg, ...)
46 va_list vargs;
48 va_start(vargs, msg);
49 fprintf(stderr, "FATAL: ");
50 vfprintf(stderr, msg, vargs);
51 fprintf(stderr, "\n");
52 exit(EXIT_FAILURE);
55 const char *str_SI_Error(SI_Error rc)
57 switch (rc) {
58 case SI_OK: return "no error";
59 case SI_UPDATED: return "existing value updated";
60 case SI_INSERTED: return "new value inserted";
61 case SI_FAIL: return "generic failure";
62 case SI_NOMEM: return "out of memory";
63 case SI_FILE: return "file error";
64 default: return "(invalid error)";
68 void read_ini_structure(const char *input_file_name, Lattice &lattice)
70 CSimpleIni ini(false, true, false);
71 const char *tmp;
73 SI_Error rc = ini.LoadFile(input_file_name);
74 if (rc < 0)
75 panic("Failed loading ini file: %s", str_SI_Error(rc));
77 // Parse UnitCell
78 const CSimpleIniA::TKeyVal * section = ini.GetSection("UnitCell");
79 if (!section)
80 panic("Ini file is missing 'UnitCell' section.");
82 #define ASSIGN_KEY(section, key_name, key_member) \
83 tmp = ini.GetValue(section, key_name); \
84 if (!tmp) \
85 panic("%s definition is missing '%s'", section, key_name); \
86 lattice.key_member = atoi(tmp)
88 ASSIGN_KEY("UnitCell", "SizeX", UnitCellSizeX);
89 ASSIGN_KEY("UnitCell", "SizeY", UnitCellSizeY);
90 ASSIGN_KEY("UnitCell", "Longitude", Longitude);
91 ASSIGN_KEY("UnitCell", "Latitude", Latitude);
92 #undef ASSIGN_KEY
94 if (lattice.UnitCellSizeX < 1 || lattice.UnitCellSizeY < 1 ||
95 lattice.Longitude < 1 || lattice.Latitude < 1)
96 panic("Unit cell has unreasonable size.");
98 lattice.sites.resize(lattice.Longitude * lattice.UnitCellSizeX,
99 lattice.Latitude * lattice.UnitCellSizeY);
100 lattice.sites = empty;
102 // Parse Adsorbates
103 section = ini.GetSection("Adsorbates");
104 if (!section)
105 panic("Ini file is missing 'Adsorbates' section. Maybe meaningful??");
107 CSimpleIniA::TNamesDepend CO_positions;
108 ini.GetAllValues("Adsorbates", "CO", CO_positions);
109 for (CSimpleIniA::TNamesDepend::const_iterator i = CO_positions.begin();
110 i != CO_positions.end(); ++i) {
111 double x_frac, y_frac;
112 if (sscanf(i->pItem, "%lf/%lf", &x_frac, &y_frac) != 2 ||
113 x_frac < 0 || x_frac >= 1 ||
114 y_frac < 0 || y_frac >= 1)
115 panic("Skrewed coordinates in '%s'", i->pItem);
116 lattice.sites((int)(lattice.UnitCellSizeX * x_frac),
117 (int)(lattice.UnitCellSizeY * y_frac)) = CO;
120 // Replicate unit cell to full dimension
121 for (int x = 0; x < lattice.Longitude * lattice.UnitCellSizeX; ++x)
122 for (int y = 0; y < lattice.Latitude * lattice.UnitCellSizeY; ++y)
123 lattice.sites(x, y) =
124 lattice.sites((x % lattice.UnitCellSizeX),
125 (y % lattice.UnitCellSizeY));
128 int main(int argc, char* argv[])
130 Lattice lattice;
131 if (argc != 2)
132 usage(*argv);
133 read_ini_structure(argv[1], lattice);
135 cout << "lattice:" << endl << lattice.sites;
137 exit(EXIT_SUCCESS);