using Distributed;
using DelimitedFiles;
@everywhere using Printf;

@show workdir = if length(ARGS) > 0
  ARGS[1];
else
  ".";
end

HOME = "/home/mgrasing"
dryrun = false;
fmt(x) = @sprintf("%06d", round(Int, 1e3*x));
nsteps = convert(Int, 1e6);
nruns = convert(Int, 1e2);
@show nsubprocs = if length(ARGS) > 1
  eval(Meta.parse(ARGS[2]));
else
  1;
end

mkpath(workdir);
#############################################################################
println("Example 2: translation symmetry");
cases = [
         Dict(
              :a => a,
              :f => f,
             )
         for a in 0.0:0.5:15.0,
             f in 0.0:0.5:15.0
        ];
cases = reshape(cases, length(cases));

println("total number of cases to run for ex2: $(length(cases))");
ks = ["std", "umb", "polya", "gu"];
open(joinpath(workdir, "trans-conv.csv"), "w") do w
  rows = pmap(case -> begin;
                command = `julia -O 3 -p $nsubprocs "ex2-trans.jl" -R $nruns -N $nsteps -a $(case[:a]) -n 4 --kT 1.0 -f $(case[:f]) --verbose 2 --do-conv-rates`
         output = read(command, String);
         max_αs = Dict();
         for line in split(output, '\n')
           if contains(line, '=')
             sides = map(strip, split(line, '='; limit=2));
             if (
                  startswith(sides[1], "αs") &&
                  maximum(map(k -> contains(sides[1], k), ks))
                )               
               αs = eval(Meta.parse(sides[2]));
               max_αs[split(sides[1], ' ')[end]] = maximum(values(αs));
             end
           end
         end       
         return hcat([case[:a] case[:f]], transpose(map(k -> max_αs[k], ks)));
       end, cases);
  writedlm(w, vcat(hcat(["a" "f"], permutedims(ks)), rows...), ',');
end
