forked from lhayward/ON_Model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ON_Model.cpp
222 lines (190 loc) · 6.56 KB
/
ON_Model.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**********************************************************************************************
************************************ OPE COEFFICIENTS CODE ************************************
***********************************************************************************************
* Lauren E. Hayward Sierens
***********************************************************************************************
* File: ON_Model.cpp (Abstract Class)
**********************************************************************************************/
#include <fstream>
#include <iostream>
#include <string>
#include "FileReading.h"
#include "ON_Model.h"
/************* ON_Model(std::ifstream* fin, std::string outFileName, ... **************
************** std::string spinConfigFileName, Hyperrectangle* lattice) **************
********** (constructor) ********/
ON_Model::ON_Model(std::ifstream* fin, std::string outFileName, std::string spinConfigFileName,
Hyperrectangle* lattice)
{
const char EQUALS_CHAR = '=';
D_ = 0;
L_ = 0;
N_ = 0;
if( fin!=NULL && fin->is_open() )
{
J_ = FileReading::readDouble(fin, EQUALS_CHAR);
h_ = FileReading::readDouble(fin, EQUALS_CHAR);
}
else
{
std::cout << "ERROR in ON_Model constructor: could not read from input file\n" << std::endl;
}
if( lattice != NULL )
{
hrect_ = lattice;
D_ = hrect_->getD();
L_ = hrect_->getL();
N_ = hrect_->getN();
} //if for non-null Lattice object
else
{
std::cout << "ERROR in ON_Model constructor: The passed Hyperrectangle object is not "
<< "valid\n" << std::endl;
}
fout.open(outFileName.c_str());
fout.precision(15);
fout_spins.open(spinConfigFileName.c_str());
fout_spins.precision(15);
warmupDone = false;
//initialize the temperature (should be changed by user to desired temperature before
//starting the simulation):
T_ = 1.0;
inCluster_ = new bool[N_];
for( uint i=0; i<N_; i++ )
{ inCluster_[i] = 0; }
//Add measurement names to Measure object:
measures.insert("E");
measures.insert("ESq");
measures.insert("AccRate_local");
measures.insert("AccRate_clust");
numAccept_local_ = 0;
numAccept_clust_ = 0;
//if we will track cluster sizes, initialize the corresponding arrays:
if( writeClusts_ )
{
clustSizes_ = new uint[N_];
clustSizes_accepted_ = new uint[N_];
clustSizes_rejected_ = new uint[N_];
for( uint i=0; i<N_; i++ )
{
clustSizes_[i] = 0;
clustSizes_accepted_[i] = 0;
clustSizes_rejected_[i] = 0;
}
} //if for writeClusts_
}
/********************************** ~ON_Model() (destructor) *********************************/
ON_Model::~ON_Model()
{
fout.close();
fout_spins.close();
if( inCluster_ != NULL )
{ delete[] inCluster_; }
inCluster_ = NULL;
if( writeClusts_ )
{
if( clustSizes_ != NULL )
{ delete[] clustSizes_; }
clustSizes_ = NULL;
if( clustSizes_accepted_ != NULL )
{ delete[] clustSizes_accepted_; }
clustSizes_accepted_ = NULL;
if( clustSizes_rejected_ != NULL )
{ delete[] clustSizes_rejected_; }
clustSizes_rejected_ = NULL;
}
}
/************************************ changeT(double newT) ***********************************/
void ON_Model::changeT(double newT)
{
T_ = newT;
warmupDone = false;
}
/*************************************** clearCluster ***************************************/
void ON_Model::clearCluster(std::vector<uint> &cluster)
{
uint clustSize = (uint)cluster.size();
for( uint i=0; i<clustSize; i++ )
{ inCluster_[cluster[i]]=0; }
//test to make sure the inCluster_ array was properly cleared (for testing purposes only):
/*for( uint i=0; i<N_; i++ )
{
if( inCluster_[i]==1 )
{ std::cout << "*** 1 ***" << std::endl; }
}*/
} //clearCluster
/************************************** markWarmupDone() *************************************/
void ON_Model::markWarmupDone()
{
warmupDone = true;
//if we will track cluster sizes, set all stored sizes to zero (since warm-up has just
//finished and the user can start tracking measurements):
if( writeClusts_ )
{
for( uint i=0; i<N_; i++ )
{
clustSizes_[i] = 0;
clustSizes_accepted_[i] = 0;
clustSizes_rejected_[i] = 0;
}
}
}
/*************************************** printParams() ***************************************/
void ON_Model::printParams()
{
std::cout << "O(" << spinDim_ << ") Model Parameters:\n"
<< "---------------------" << std::endl;
std::cout << " J = " << J_ << "\n"
<< " h = " << h_ << "\n";
}
/************************* writeClustHistoData(std::string fileName) *************************/
void ON_Model::writeClustHistoData(std::string fileName)
{
std::ofstream fout_clust;
if( writeClusts_ )
{
fout_clust.open(fileName.c_str());
fout_clust.precision(15);
fout_clust << "#T \t clustSize \t num_generated \t num_accepted \t num_rejected" << std::endl;
for( uint i=0; i<N_; i++ )
{
fout_clust << T_ << '\t' << (i+1) << '\t' << clustSizes_[i] << '\t' << clustSizes_accepted_[i]
<< '\t' << clustSizes_rejected_[i] << std::endl;
}
fout_clust.close();
}
}
/******************************** uintPower(int base, int exp) *******************************/
uint ON_Model::uintPower(uint base, uint exp)
{
uint result = 1;
for(uint i=1; i<=exp; i++)
{ result *= base; }
return result;
} //uintPower method
/******************** writeBin(int binNum, int numMeas, int sweepsPerMeas) *******************/
void ON_Model::writeBin(int binNum, int numMeas, int sweepsPerMeas)
{
//Note: the following two measurements will be divided by numMeas in the call to
//writeAverages() such that acceptance rates are written to file
measures.accumulate( "AccRate_local", numAccept_local_/(1.0*N_*sweepsPerMeas) );
measures.accumulate( "AccRate_clust", numAccept_clust_/(1.0*sweepsPerMeas) );
//if this is the first bin being written to file, then also write a line of text explaining
//each column:
if( binNum == 1)
{
fout << "# L \t T \t binNum";
measures.writeMeasNames(&fout);
fout << std::endl;
}
fout << L_[0] << '\t' << T_ << '\t' << binNum;
measures.writeAverages(&fout, numMeas);
fout << std::endl;
} //writeBin method
/************************************* zeroMeasurements() ************************************/
void ON_Model::zeroMeasurements()
{
measures.zero();
numAccept_local_ = 0;
numAccept_clust_ = 0;
}