HILA
Loading...
Searching...
No Matches
utility.h
1#ifndef UTILITY_H_
2#define UTILITY_H_
3
4#include "hila.h"
5#include <algorithm>
6
7enum class APPEND_FILE { TRUE, FALSE };
8enum class CLOSE_FILE { TRUE, FALSE };
9
10int z_ind(int z) {
11 return (z + lattice.size(e_z)) % lattice.size(e_z);
12}
13
14template <typename T>
15void print_formatted_numbers(const std::vector<Complex<T>> &numbers, std::string label,
16 bool indices, bool sum = false) {
17
18 if (indices) {
19 hila::out0 << label << ": ";
20 for (int i = 0; i < numbers.size() - 1; i++) {
21 hila::out0 << i << ", ";
22 }
23 if (sum == true) {
24 hila::out0 << "sum\n";
25 } else {
26 hila::out0 << numbers.size() - 1 << "\n";
27 }
28 } else {
29 hila::out0 << label << ": ";
30 for (int i = 0; i < numbers.size() - 1; i++) {
31 hila::out0 << std::setprecision(12) << numbers[i].re << " " << numbers[i].im << ", ";
32 }
33 hila::out0 << std::setprecision(12) << numbers[numbers.size() - 1].re << " "
34 << numbers[numbers.size() - 1].im << "\n";
35 }
36}
37
38template <typename T>
39void print_formatted_numbers(const std::vector<T> &numbers, std::string label, bool indices,
40 bool sum = false) {
41
42 if (indices) {
43 hila::out0 << label << ": ";
44 for (int i = 0; i < numbers.size() - 1; i++) {
45 hila::out0 << i << ", ";
46 }
47 if (sum == true) {
48 hila::out0 << "sum\n";
49 } else {
50 hila::out0 << numbers.size() - 1 << "\n";
51 }
52 } else {
53 hila::out0 << label << ": ";
54 for (int i = 0; i < numbers.size() - 1; i++) {
55 hila::out0 << std::setprecision(12) << numbers[i] << ", ";
56 }
57 hila::out0 << std::setprecision(12) << numbers[numbers.size() - 1] << "\n";
58 }
59}
60
61// template <typename T>
62// void write_surface(std::vector<T> surf, std::string file_name, APPEND_FILE
63// append_true,
64// CLOSE_FILE close_file) {
65
66// static std::ofstream MFile; // Declare static ofstream object
67
68// // Open file if not already open, based on append_true parameter
69// if (!MFile.is_open()) {
70// if (append_true == APPEND_FILE::TRUE)
71// MFile.open(file_name, std::ios::app); // Append mode
72// else
73// MFile.open(file_name); // Default (overwrite) mode
74
75// if (!MFile.is_open()) {
76// std::cerr << "Error: Failed to open file " << file_name <<
77// std::endl; return;
78// }
79
80// std::cout << "Opened file " << file_name << std::endl;
81// }
82// // MFile.open("surface", std::ios_base::app);
83// MFile << "volume: " << lattice.size(e_x) << " " << lattice.size(e_y) << "
84// " << lattice.size(e_z)
85// << " " << lattice.size(e_t) << std::endl;
86// MFile << "x y z" << std::endl;
87// for (int y = 0; y < lattice.size(e_y); y++) {
88// for (int x = 0; x < lattice.size(e_x); x++) {
89// if (hila::myrank() == 0)
90// MFile << x << ' ' << y << ' ' << surf[x + y *
91// lattice.size(e_x)] << std::endl;
92// }
93// }
94// // Close file if specified by close_file parameter
95// if (close_file == CLOSE_FILE::TRUE) {
96// MFile.close();
97// std::cout << "Closed file " << file_name << std::endl;
98// }
99// }
100template <typename T>
101void write_surface(std::vector<T> surf, std::string file_name, APPEND_FILE append_true,
102 CLOSE_FILE close_file) {
103
104 std::ofstream MFile; // Declare non-static ofstream object
105
106 // Open file based on append_true parameter
107 if (append_true == APPEND_FILE::TRUE)
108 MFile.open(file_name, std::ios::app); // Append mode
109 else
110 MFile.open(file_name); // Default (overwrite) mode
111
112 if (!MFile.is_open()) {
113 std::cerr << "Error: Failed to open file " << file_name << std::endl;
114 return;
115 }
116
117 // Write lattice size and surface data
118 MFile << "volume: " << lattice.size(e_x) << " " << lattice.size(e_y) << " " << lattice.size(e_z)
119 << " " << lattice.size(e_t) << std::endl;
120 MFile << "x y z" << std::endl;
121
122 for (int y = 0; y < lattice.size(e_y); y++) {
123 for (int x = 0; x < lattice.size(e_x); x++) {
124 if (hila::myrank() == 0) {
125 MFile << x << ' ' << y << ' ' << surf[x + y * lattice.size(e_x)] << std::endl;
126 }
127 }
128 }
129
130 // Close file if specified by close_file parameter
131 if (close_file == CLOSE_FILE::TRUE) {
132 MFile.close();
133 std::cout << "Closed file " << file_name << std::endl;
134 }
135}
136
137// template <typename T>
138// void write_fourier(std::vector<T> npow, std::vector<int> hits, int pow_size,
139// std::string file_name,
140// APPEND_FILE append_true, CLOSE_FILE close_file) {
141
142// static std::ofstream MFile; // Declare static ofstream object
143
144// // Open file if not already open, based on append_true parameter
145
146// if (!MFile.is_open()) {
147// if (append_true == APPEND_FILE::TRUE)
148// MFile.open(file_name, std::ios::app); // Append mode
149// else
150// MFile.open(file_name); // Default (overwrite) mode
151
152// if (!MFile.is_open()) {
153// std::cerr << "Error: Failed to open file " << file_name <<
154// std::endl; return;
155// }
156
157// std::cout << "Opened file " << file_name << std::endl;
158// }
159// // MFile.open("surface", std::ios_base::app);
160// MFile << "volume: " << lattice.size(e_x) << " " << lattice.size(e_y) << "
161// " << lattice.size(e_z)
162// << " " << lattice.size(e_t) << std::endl;
163// MFile << "i"
164// << " n/h"
165// << " h" << std::endl;
166// for (int i = 0; i < pow_size; i++) {
167// if (hits[i] > 0)
168// MFile << i << ' ' << npow[i] / hits[i] << ' ' << hits[i] << '\n';
169// }
170// // Close file if specified by close_file parameter
171// if (close_file == CLOSE_FILE::TRUE) {
172// MFile.close();
173// std::cout << "Closed file " << file_name << std::endl;
174// }
175// }
176
177template <typename T>
178void write_fourier(std::vector<T> npow, std::vector<int> hits, int pow_size, std::string file_name,
179 APPEND_FILE append_true, CLOSE_FILE close_file) {
180
181 std::ofstream MFile; // Declare ofstream object
182
183 // Open file based on append_true parameter
184 if (append_true == APPEND_FILE::TRUE)
185 MFile.open(file_name, std::ios::app); // Append mode
186 else
187 MFile.open(file_name); // Default (overwrite) mode
188
189 if (!MFile.is_open()) {
190 std::cerr << "Error: Failed to open file " << file_name << std::endl;
191 return;
192 }
193
194 // Write lattice size and data
195 MFile << "volume: " << lattice.size(e_x) << " " << lattice.size(e_y) << " " << lattice.size(e_z)
196 << " " << lattice.size(e_t) << std::endl;
197 MFile << "i"
198 << " n/h"
199 << " h" << std::endl;
200 for (int i = 0; i < pow_size; i++) {
201 if (hits[i] > 0)
202 MFile << i << ' ' << npow[i] / hits[i] << ' ' << hits[i] << '\n';
203 }
204
205 // Close file if specified by close_file parameter
206 if (close_file == CLOSE_FILE::TRUE) {
207 MFile.close();
208 std::cout << "Closed file " << file_name << std::endl;
209 }
210}
211
212#endif
Complex definition.
Definition cmplx.h:56
int myrank()
rank of this node
Definition com_mpi.cpp:234
std::ostream out0
This writes output only from main process (node 0)
int z_ind(int z)
Helper function to get valid z-coordinate index.
Definition suN_gauge.cpp:29