Commit a39736e8 authored by Lukas Weber's avatar Lukas Weber

add simple test for merger based on algebraic series

parent 32805092
......@@ -6,4 +6,5 @@ t1 = executable('tests',
)
test('tests', t1)
subdir('silly_mc')
#include "silly_mc.h"
#include "loadleveller/loadleveller.h"
int main(int argc, char *argv[]) {
loadl::run<silly_mc>(argc, argv);
}
silly_mc = executable('silly_mc_test',
['main.cpp', 'silly_mc.cpp'],
dependencies : loadleveller_dep,
)
testscript = find_program('silly_test.py')
test('algebraic series mc', testscript, args : [silly_mc, files('test_parameters.json')])
#include "silly_mc.h"
silly_mc::silly_mc(const loadl::parser &p) : loadl::mc(p) {
}
void silly_mc::do_update() {
idx_++;
}
void silly_mc::do_measurement() {
std::vector<double> silly = {1.*idx_, 1.*(3-idx_%5)*idx_};
measure.add("MagicNumber", silly);
measure.add("MagicNumber2", idx_*idx_);
}
void silly_mc::init() {
idx_=0;
int binsize = param.get("binsize", 1);
measure.register_observable("MagicNumber", binsize,2);
measure.register_observable("MagicNumber2", binsize);
}
void silly_mc::checkpoint_write(const loadl::iodump::group &d) {
d.write("idx", idx_);
}
void silly_mc::checkpoint_read(const loadl::iodump::group &d) {
d.read("idx", idx_);
}
void silly_mc::register_evalables(std::vector<loadl::evalable> &evalables) {
evalables.push_back(
loadl::evalable{"AntimagicNumber",
{"MagicNumber", "MagicNumber2"},
[](const std::vector<std::vector<double>> &obs) -> std::vector<double> {
double mag = obs[0][0];
double mag2 = obs[1][0];
return {mag * mag / mag2};
}});
}
#pragma once
#include <loadleveller/loadleveller.h>
#include <string>
#include <vector>
class silly_mc : public loadl::mc {
private:
uint64_t idx_;
public:
void init();
void do_update();
void do_measurement();
void checkpoint_write(const loadl::iodump::group &out);
void checkpoint_read(const loadl::iodump::group &in);
void register_evalables(std::vector<loadl::evalable> &evalables);
silly_mc(const loadl::parser &p);
};
#!/usr/bin/env python3
import os
import json
import subprocess
import argparse
import sys
import shutil
import h5py
import numpy as np
parser = argparse.ArgumentParser()
parser.add_argument('silly_mc')
parser.add_argument('test_param_file')
args = parser.parse_args()
shutil.rmtree('silly_job.data', ignore_errors=True)
proc = subprocess.run(['valgrind', args.silly_mc, 'single', args.test_param_file])
if proc.returncode != 0:
sys.exit(1)
with open('silly_job.results.json', 'r') as f:
results = json.load(f)
with open(args.test_param_file, 'r') as f:
rebin_size = json.load(f)['jobconfig']['merge_rebin_length']
sweeps = results[0]['parameters']['sweeps']
therm = results[0]['parameters']['thermalization']
binsize = results[0]['parameters']['binsize']*rebin_size
nbin = sweeps//binsize
# based on analytical expressions for Σi, Σi²
mean = (binsize*nbin)*(binsize*nbin+2*therm-1)/2/(nbin*binsize)
binmean2 = 1/12 * (3-12*therm+12*therm**2-binsize**2-6*binsize*nbin+12*therm*binsize*nbin+4*binsize**2*nbin**2)
error = (1/(nbin-1)*(binmean2 - mean**2))**0.5
reference = {
'MagicNumber': {
'mean' : mean,
'error' : error
}
}
# TODO autocorrelation time and evalables
failcount = 0
for obsname, obs in reference.items():
for entry, val in obs.items():
mcval = results[0]['results'][obsname][entry][0]
if not np.isclose(val, mcval):
print('{}/{}: {} != {}'.format(obsname, entry, val, mcval))
failcount += 1
sys.exit(failcount)
{
"jobname": "silly_job",
"jobconfig": {
"mc_runtime": "24:00:00",
"mc_checkpoint_time": "15:00",
"merge_rebin_length": 7
},
"tasks": {
"task0001": {
"sweeps": 1000,
"thermalization": 20,
"binsize": 3
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment