-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworldlc.cpp
162 lines (141 loc) · 5.34 KB
/
worldlc.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "worldlc.hpp"
#include "world.hpp"
#include "cell.hpp"
#include <stdexcept>
#include <sstream>
#include <map>
#include <cmath>
// ctor, which calls World::World()
WorldLC::WorldLC() : cell_r_cut(2.5) {
// we do need another mapOption
mapOptions["cell_r_cut"] = CELLRCUT;
}
void WorldLC::readParameter(const std::string &filename)
{
// call the base function
World::readParameter(filename);
// create input filestream
std::ifstream parfile(filename.c_str());
// check if file is open
if (!parfile.is_open())
throw std::runtime_error("readParameter(): Can't open file '" + filename + "' for reading.");
// helper strings
std::string line, option;
// read file till eof
while (parfile.good())
{
// read line from file
getline(parfile,line);
// create a string stream
std::stringstream strstr;
// put line into string stream
strstr << line;
// read option from stringstream
strstr >> option;
// TODO: Add eps and sigma!
// push next read value, with internal converter of string stream, into the propper place
if (mapOptions[option] == CELLRCUT)
strstr >> cell_r_cut;
}
// close file
parfile.close();
// #of all cells
int nCells = 1;
// Calc #cells
for (int d=0; d<DIM; d++)
{
// #cells in dimension = floor(length per cell-cutlength)
cell_N[d] = (int)(length[d]/cell_r_cut);
// cell length = world length per #cells
cell_length[d] = length[d]/cell_N[d];
nCells *= cell_N[d];
// DEBUG
// std::cout << "World.length[" << d << "]=" << length[d] << "\tcell_r_cut=" << cell_r_cut
// << "\t#Cells=" << cell_N[d] << "\tCelllength=" << cell_length[d] << std::endl;
}
// insert empty cells
for (int i=0; i<nCells; i++)
cells.push_back(Cell());
// DEBUG
// std::cout << "#Cells: " << nCells << "\t" << cells.size() <<std::endl << std::endl;
}
void WorldLC::readParticles(const std::string &filename)
{
// call the base function
World::readParticles(filename);
nParticles = particles.size();
// Write every particle into it's belonging cell
for (std::list<Particle>::iterator i = particles.begin(); i != particles.end(); i++)
{
std::cout << "Push and erase particles[" << i->ID << "] " << std::endl;
// add particle to right cell...
// getCellNumber(i) gives belonging cellnumber, push into this cell our actual particle i: particles[i-particles.begin()]
cells[getCellNumber(i)].particles.push_back(*i);
}
std::cout << "***************************************************" << std::endl
<< "FINISHED READING PARTICLES - NOW CLEAR PARTICLES" << std::endl
<< "*************************************************** \n" << std::endl;
// Clear all particles in our World Vector
particles.clear();
}
int WorldLC::getCellNumber(const std::list<Particle>::iterator i)
{
int tmp[3] = {0,0,0};
// // DEBUG Table
// std::cout << "Cell coordinate: " ;
for (int d=0; d<DIM; d++)
{
if (i->x[d] < 0)
return -1;
tmp[d] = (int) floor(i->x[d] * cell_N[d] / length[d]) % cell_N[d];
// // DEBUG
// std::cout << tmp[d] << "\t";
}
// //DEBUG FOR-LOOP
// std::cout << std::endl;
// for (int d=0; d<DIM; d++)
// std::cout << "x[" << d << "]: " << i->x[d] << "\t";
// std::cout << std::endl << "Corresponding Index: " << J(tmp,cell_N) << std::endl << std::endl;
return J(tmp,cell_N);
}
real WorldLC::calcBeta(int dimension)
{
real tmp = 0.0;
for (std::vector<Cell>::iterator cell = cells.begin(); cell != cells.end (); cell++)
for (std::list<Particle>::iterator i = cell->particles.begin (); i != cell->particles.end (); i++)
tmp += sqr(i->v[dimension]);
return sqrt(thermo_target_temp * (nParticles-1) / (24*tmp));
}
std::ostream& operator << (std::ostream& os, WorldLC& W)
{
// Get out some information about the world
os << W.name << " Dim=" << DIM << " t=" << W.t << " delta_t=" << W.delta_t << " t_end=" << W.t_end
<< " Number of Cells=" << W.cells.size() << " cell_r_cut=" << W.cell_r_cut << std::endl;
// roll over each Cell
for (std::vector<Cell>::iterator i = W.cells.begin(); i < W.cells.end(); i++)
{
// if there are some particles in this cell, show it off
if (W.cells[i-W.cells.begin()].particles.size() > 0)
{
// we are now in cell# x
os << "cell[" << i-W.cells.begin() << "] = " ;
// get out every particle in this cell
for (std::list<Particle>::iterator j = W.cells[i-W.cells.begin()].particles.begin();j != W.cells[i-W.cells.begin()].particles.end(); j++)
{
// Particle Number
os << j->ID << ": ";
// get out the particles location
for (unsigned int d=0; d<DIM; d++)
os << j->x[d] << "\t";
// tabulator after each particle
os << "\t";
}
// newline after each cell
os << std::endl;
}
// else observe next cell
else continue;
}
return os;
}
// vim:set et sts=4 ts=4 sw=4 ai ci cin: