HILA
Loading...
Searching...
No Matches
hmc.h
1#ifndef HMC_H
2#define HMC_H
3
4#include <sys/time.h>
5#include <ctime>
6#include "integrator.h"
7
8/// The Hybrid Montecarlo algorithm.
9// Consists of an integration step following equations of
10// motion implemented in the integrator class gt
11// and an accept-reject step using the action
12//
13// The integrator class must implement at least two functions,
14// action() an integrator_step(double eps)
15template <class integrator_type>
16void update_hmc(integrator_type &integrator, int steps, double traj_length) {
17
18 static int accepted = 0, trajectory = 1;
19 struct timeval start, end;
20 double timing;
21
22 // Draw the momentum
23 integrator.draw_gaussian_fields();
24
25 // Make a copy of the gauge field in case the update is rejected
26 integrator.backup_fields();
27
28 gettimeofday(&start, NULL);
29
30 // Calculate the starting action and print
31 double start_action = integrator.action();
32 hila::out0 << "Begin HMC Trajectory " << trajectory << ": Action " << start_action
33 << "\n";
34
35 // Run the integrator
36 for (int step = 0; step < steps; step++) {
37 integrator.step(traj_length / steps);
38 }
39
40 // Recalculate the action
41 double end_action = integrator.action();
42 double edS = exp(-(end_action - start_action));
43
44 // Accept or reject
45 bool accept = hila::random() < edS;
46 hila::broadcast(accept);
47 if (accept) {
48 hila::out0 << "Accepted!\n";
49 accepted++;
50 } else {
51 hila::out0 << "Rejected!\n";
52 integrator.restore_backup();
53 }
54
55 hila::out0 << "End HMC: Action " << end_action << " " << end_action - start_action
56 << " exp(-dS) " << edS << ". Acceptance " << accepted << "/" << trajectory
57 << " " << (double)accepted / (double)trajectory << "\n";
58
59 gettimeofday(&end, NULL);
60 timing = (double)(end.tv_sec - start.tv_sec) + 1e-6 * (end.tv_usec - start.tv_usec);
61
62 hila::out0 << "HMC done in " << timing << " seconds \n";
63 trajectory++;
64}
65
66#endif
Array< n, m, T > exp(Array< n, m, T > a)
Exponential.
Definition array.h:1059
double random()
Real valued uniform random number generator.
Definition hila_gpu.cpp:118
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