-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsite.cpp
More file actions
130 lines (104 loc) · 3.17 KB
/
site.cpp
File metadata and controls
130 lines (104 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "pch.h"
#include "site.h"
#include "transporter.h"
#include "consts.h"
#include "IO.h"
site::site(double x, double y, double z, double E)
{
pos.X = x;
pos.Y = y;
pos.Z = z;
energy = E;
}
void site::addNeighbour(site* pSite, double J)
{
// Unless already present, add passed site as neighbour to 'this'
if (!this->hasNeighbour(pSite))
{
neighbour* n = new neighbour;
n->_pSite = pSite;
n->_J = J;
neighbours.push_back(n);
}
// Unless already present, add 'this' as neighbour to passed site
if (!pSite->hasNeighbour(this))
{
neighbour* th = new neighbour;
th->_pSite = this;
th->_J = J;
pSite->neighbours.push_back(th);
}
}
site::neighbour* site::hasNeighbour(site* pSite)
{
std::vector<neighbour*>::iterator it = neighbours.begin();
for (int i = 0; it != neighbours.end(); i++, it++)
if ((*it)->_pSite == pSite)
return *it;
return NULL;
}
site::~site()
{
std::vector<neighbour*>::iterator it = neighbours.begin();
for (int i = 0; it != neighbours.end(); i++, it++)
delete (*it);
neighbours.clear();
}
std::ostream& operator<<(std::ostream& os, const site& st)
{
os << "pos = (" << st.pos.X << "," << st.pos.Y << "," << st.pos.Z << "); E = " << st.energy << "; # neighbors = " << st.neighbours.size();
return os;
}
std::vector<site> CreateSites(char* XYZfile, char* EDGEfile)
{
std::vector<site> sites;
// Use contents of .xyz file to populate site vector
// Columns should be formatted as: x (float), y (float), z (float), site type (char, unused), site energy (float)
std::ifstream in;
open(XYZfile, in);
std::string line;
int linenum = 0;
while (std::getline(in, line))
{
linenum++;
std::stringstream linestream(line);
double x, y, z, E;
char s;
linestream >> x >> y >> z >> s >> E;
if (linestream.fail())
{
std::cout << "***ERROR***: Unexpected formatting of .xyz file at line " << linenum << ". Expected 5 columns with value types; float, float, float, char, float.\n";
in.close();
exit(-1);
}
sites.push_back(site(x, y, z, E));
}
in.close();
// Use contents of .edge file to set interacting neighbours
// Columns should be formatted as: site 1 (int), site 2 (int), J (float)
open(EDGEfile, in);
linenum = 0;
while (std::getline(in, line))
{
linenum++;
std::stringstream linestream(line);
int s1, s2;
double J;
linestream >> s1 >> s2 >> J;
if (linestream.fail())
{
std::cout << "***ERROR***: Unexpected formatting of .edge file at line " << linenum << ". Expected 3 columns with value types; int, int, float.\n";
in.close();
exit(-1);
}
if (s1 >= sites.size() || s2 >= sites.size())
{
std::cout << "***ERROR***: Site index out of range in .edge file at line " << linenum << "\n";
in.close();
exit(-1);
}
sites[s1].addNeighbour(&sites[s2], J);
}
in.close();
return sites;
}