HILA
Loading...
Searching...
No Matches
gaugefield.h
Go to the documentation of this file.
1/**
2 * @file gaugefield.h
3 * @brief Definition of Gauge Field
4 * @details This file contains the definition for the GaugeField class
5 */
6#ifndef GAUGEFIELD_H_
7#define GAUGEFIELD_H_
8
9#include "hila.h"
10
11/**
12 * @brief Gauge field class
13 * @details Stores and defines links between Lattice Field elements. Number of links is
14 * `lattice.size()*NDIM`, since for each point there is a link in all directions.
15 *
16 * @param fdir std::array<Field<T>,NDIM> type element which stores GaugeField links in back to back
17 * direction wise ordering.
18 *
19 * @tparam T Group that GaugeField consists of
20 */
21template <typename T>
23 private:
24 std::array<Field<T>, NDIM> fdir;
25
26 // somewhat arbitrary fingerprint flag for configuration files
27 static constexpr int64_t config_flag = 394824242;
28
29 public:
30 // Default constructor
31 GaugeField() = default;
32
33 // Straightforward copy constructor seems to be necessary
34 GaugeField(const GaugeField &other) = default;
35
36 // copy constructor - from fields which can be assigned
37 template <typename A, std::enable_if_t<std::is_convertible<A, T>::value, int> = 0>
38 GaugeField(const GaugeField<A> &other) {
39 foralldir(d) fdir[d] = other[d];
40 }
41
42 // constructor with compatible scalar
43 template <typename A, std::enable_if_t<hila::is_assignable<T &, A>::value, int> = 0>
44 GaugeField(const A &val) {
45 foralldir(d) fdir[d] = val;
46 }
47
48 // constructor from 0 - nullptr trick in use
49 GaugeField(const std::nullptr_t z) {
50 foralldir(d) fdir[d] = 0;
51 }
52
53
54 /////////////////////////////////////////////////
55 /// Destructor
56
57 ~GaugeField() = default;
58
59 /////////////////////////////////////////////////
60 /// Access components with []
61
63 return fdir[d];
64 }
65
66 inline const Field<T> &operator[](Direction d) const {
67 return fdir[d];
68 }
69
70 //////////////////////////////////////////////////
71 /// Assign from anything the field allows
72 template <typename A>
73 GaugeField &operator=(const A &val) {
74 foralldir(d) fdir[d] = val;
75 return *this;
76 }
77
78 /// Separate 0 assignment
79 GaugeField &operator=(std::nullptr_t np) {
80 foralldir(d) fdir[d] = 0;
81 return *this;
82 }
83
84 template <typename A>
86 foralldir(d) fdir[d] = rhs[d];
87 return *this;
88 }
89
90 /**
91 * @brief Reunitarize Gauge Field consisting of \f$ SU(N)\f$ matrices
92 * @details Only defined for \f$ SU(N) \f$ matrices and Fields
93 */
95 foralldir(d) {
96 onsites(ALL)(*this)[d][X].reunitarize();
97 }
98 }
99
100 /**
101 * @brief Computes Wilson action
102 * @details \f{align}{ S &= \beta\sum_{\textbf{dir}_1 < \textbf{dir}_2}\sum_{X} \frac{1}{N}
103 * \Re\mathrm{Tr}\left[ 1- U_{\textbf{dir}_1 \textbf{dir}_2}(X) \right] \f} Where \f$\beta =
104 * 2N/g^2\f$
105 *
106 * @return double
107 */
108 double measure_plaq() const {
110 plaq.allreduce(false);
111
112 foralldir(dir1) foralldir(dir2) if (dir1 < dir2) {
113
114 onsites(ALL) {
115 plaq += 1.0 -
116 real(trace((*this)[dir1][X] * (*this)[dir2][X + dir1] *
117 (*this)[dir1][X + dir2].dagger() * (*this)[dir2][X].dagger())) /
118 T::size();
119 }
120 }
121
122 return plaq.value();
123 }
124 ////////////////////////////////////////////////////////
125 // I/O operations for gauge fields (here only binary)
126
127 void write(std::ofstream &outputfile) const {
128 foralldir(d) {
129 fdir[d].write(outputfile);
130 }
131 }
132
133 void write(const std::string &filename) const {
134 std::ofstream outputfile;
135 hila::open_output_file(filename, outputfile);
136 write(outputfile);
137 hila::close_file(filename, outputfile);
138 }
139
140 void read(std::ifstream &inputfile) {
141 foralldir(d) {
142 fdir[d].read(inputfile);
143 }
144 }
145
146 void read(const std::string &filename) {
147 std::ifstream inputfile;
148 hila::open_input_file(filename, inputfile);
149 read(inputfile);
150 hila::close_file(filename, inputfile);
151 }
152
153 /// config_write writes the gauge field to file, with additional "verifying" header
154
155 void config_write(const std::string &filename) const {
156 std::ofstream outputfile;
157 hila::open_output_file(filename, outputfile);
158
159 // write header
160 if (hila::myrank() == 0) {
161 int64_t f = config_flag;
162 outputfile.write(reinterpret_cast<char *>(&f), sizeof(int64_t));
163 f = NDIM;
164 outputfile.write(reinterpret_cast<char *>(&f), sizeof(int64_t));
165 f = sizeof(T);
166 outputfile.write(reinterpret_cast<char *>(&f), sizeof(int64_t));
167
168 foralldir(d) {
169 f = lattice.size(d);
170 outputfile.write(reinterpret_cast<char *>(&f), sizeof(int64_t));
171 }
172 }
173
174 write(outputfile);
175 hila::close_file(filename, outputfile);
176 }
177
178 void config_read(const std::string &filename) {
179 std::ifstream inputfile;
180 hila::open_input_file(filename, inputfile);
181 std::string conferr("CONFIG ERROR in file " + filename + ": ");
182
183 // read header
184 bool ok = true;
185 int64_t f;
186 if (hila::myrank() == 0) {
187 inputfile.read(reinterpret_cast<char *>(&f), sizeof(int64_t));
188 ok = (f == config_flag);
189 if (!ok)
190 hila::out0 << conferr << "wrong id, should be " << config_flag << " is " << f
191 << '\n';
192 }
193
194 if (ok && hila::myrank() == 0) {
195 inputfile.read(reinterpret_cast<char *>(&f), sizeof(int64_t));
196 ok = (f == NDIM);
197 if (!ok)
198 hila::out0 << conferr << "wrong dimensionality, should be " << NDIM << " is " << f
199 << '\n';
200 }
201
202 if (ok && hila::myrank() == 0) {
203 inputfile.read(reinterpret_cast<char *>(&f), sizeof(int64_t));
204 ok = (f == sizeof(T));
205 if (!ok)
206 hila::out0 << conferr << "wrong size of field element, should be " << sizeof(T)
207 << " is " << f << '\n';
208 }
209
210 if (ok && hila::myrank() == 0) {
211
212 foralldir(d) {
213 inputfile.read(reinterpret_cast<char *>(&f), sizeof(int64_t));
214 ok = ok && (f == lattice.size(d));
215 if (!ok)
216 hila::out0 << conferr << "incorrect lattice dimension " << hila::prettyprint(d)
217 << " is " << f << " should be " << lattice.size(d) << '\n';
218 }
219 }
220
221 if (!hila::broadcast(ok)) {
223 }
224
225 read(inputfile);
226 hila::close_file(filename, inputfile);
227 }
228};
229
230
231/// Implement hila::swap for gauge fields
232namespace hila {
233template <typename T>
234void swap(GaugeField<T> &A, GaugeField<T> &B) {
235 foralldir(d) hila::swap(A[d], B[d]);
236}
237} // namespace std
238
239
240///////////////////////////////////////////////////////
241/// Alias VectorField to GaugeField
242template <typename T>
244
245
246#endif
Array< n, m, hila::arithmetic_type< T > > real(const Array< n, m, T > &arg)
Return real part of Array.
Definition array.h:688
The field class implements the standard methods for accessing Fields. Hilapp replaces the parity acce...
Definition field.h:61
Gauge field class.
Definition gaugefield.h:22
~GaugeField()=default
Destructor.
GaugeField & operator=(std::nullptr_t np)
Separate 0 assignment.
Definition gaugefield.h:79
Field< T > & operator[](Direction d)
Access components with [].
Definition gaugefield.h:62
GaugeField & operator=(const A &val)
Assign from anything the field allows.
Definition gaugefield.h:73
void reunitarize_gauge()
Reunitarize Gauge Field consisting of matrices.
Definition gaugefield.h:94
double measure_plaq() const
Computes Wilson action.
Definition gaugefield.h:108
void config_write(const std::string &filename) const
config_write writes the gauge field to file, with additional "verifying" header
Definition gaugefield.h:155
Special reduction class: enables delayed and non-blocking reductions, which are not possible with the...
Definition reduction.h:14
const T value()
Return value of the reduction variable. Wait for the comms if needed.
Definition reduction.h:157
Reduction & allreduce(bool b=true)
allreduce(bool) turns allreduce on or off. By default on.
Definition reduction.h:130
Complex< T > dagger(const Complex< T > &val)
Return dagger of Complex number.
Definition cmplx.h:1223
#define foralldir(d)
Macro to loop over (all) Direction(s)
Definition coordinates.h:78
Direction
Enumerator for direction that assigns integer to direction to be interpreted as unit vector.
Definition coordinates.h:34
constexpr Parity ALL
bit pattern: 011
Implement hila::swap for gauge fields.
Definition array.h:981
int myrank()
rank of this node
Definition com_mpi.cpp:235
std::ostream out0
This writes output only from main process (node 0)
T broadcast(T &var, int rank=0)
Broadcast the value of var to all MPI ranks from rank (default=0).
Definition com_mpi.h:153
void terminate(int status)