#!/usr/bin/env python
from math import *
import re
from string import Template
import sys
################################################################################
domain_size = 128.0
domain_spacing = 1.0
fine_box_size = 2.0
fine_box_spacing = 1/32.0
dtfac = 1/4.0
################################################################################
xmin = -domain_size
xmax = +domain_size
nlevels = round(log(1.0 * domain_spacing / fine_box_spacing, 2)) + 1
ncells = round(2.0 * domain_size / domain_spacing)
large_box_size = fine_box_size * 2 ** (nlevels - 2)
error_threshold = 1.0 / large_box_size
out_every = 1 #TODO round(1.0 / dtfac * 2 ** (nlevels - 1))
parfile = """
ActiveThorns = "
ADMBase
CarpetX
Coordinates
ErrorEstimator
Formaline
IOUtil
ODESolvers
TimerReport
TwoPunctures
Z4c
"
# domain_size = $domain_size
# domain_spacing = $domain_spacing
# fine_box_size = $fine_box_size
# fine_box_spacing = $fine_box_spacing
# dtfac = $dtfac
Cactus::cctk_show_schedule = yes
Cactus::terminate = "time"
Cactus::cctk_final_time = 100.0
CarpetX::verbose = no
CarpetX::poison_undefined_values = no
CarpetX::xmin = $xmin
CarpetX::ymin = $xmin
CarpetX::zmin = $xmin
CarpetX::xmax = $xmax
CarpetX::ymax = $xmax
CarpetX::zmax = $xmax
CarpetX::ncells_x = $ncells
CarpetX::ncells_y = $ncells
CarpetX::ncells_z = $ncells
CarpetX::periodic_x = no
CarpetX::periodic_y = no
CarpetX::periodic_z = no
CarpetX::periodic = no
# CarpetX::reflection_x = yes
# CarpetX::reflection_y = yes
# CarpetX::reflection_z = yes
# CarpetX::reflection_upper_x = yes
# CarpetX::reflection_upper_y = yes
# CarpetX::reflection_upper_z = yes
CarpetX::ghost_size = 3
CarpetX::max_num_levels = $nlevels
CarpetX::regrid_every = 0 #TODO 16
CarpetX::regrid_error_threshold = $error_threshold
ErrorEstimator::region_shape = "cube"
ErrorEstimator::scale_by_resolution = yes
CarpetX::prolongation_type = "ddf"
CarpetX::prolongation_order = 5
ODESolvers::method = "RK4"
CarpetX::dtfac = $dtfac
ADMBase::initial_data = "TwoPunctures"
ADMBase::initial_lapse = "TwoPunctures-averaged"
# QC-0 setup
TwoPunctures::par_b = 1.168642873
TwoPunctures::par_m_plus = 0.453
TwoPunctures::par_m_minus = 0.453
TwoPunctures::par_P_plus [1] = +0.3331917498
TwoPunctures::par_P_minus[1] = -0.3331917498
TwoPunctures::grid_setup_method = "evaluation"
TwoPunctures::TP_epsilon = 1.0e-2
TwoPunctures::TP_Tiny = 1.0e-2
TwoPunctures::verbose = yes
Z4c::chi_floor = 1.0e-6
Z4c::alphaG_floor = 1.0e-8
Z4c::epsdiss = 0.32
IO::out_dir = $$parfile
IO::out_every = $out_every
IO::out_mode = "np"
IO::out_proc_every = 1
CarpetX::out_plotfile_groups = ""
CarpetX::out_silo_vars = "
# ADMBase::metric
# ADMBase::curv
ADMBase::lapse
# ADMBase::shift
# Z4c::chi
# Z4c::gamma_tilde
# Z4c::K_hat
# Z4c::A_tilde
# Z4c::Gam_tilde
# Z4c::Theta
# Z4c::alphaG
# Z4c::betaG
# Z4c::ZtC
# Z4c::HC
# Z4c::MtC
Z4c::allC
"
CarpetX::out_tsv = no
TimerReport::out_every = 1 #TODO 128
TimerReport::out_filename = "TimerReport"
TimerReport::output_schedule_timers = no
TimerReport::n_top_timers = 100
"""
open(re.sub(r'(.*)\.rpar$', r'\1.par', sys.argv[0]), 'w').write(
re.sub(r'\n *',r'\n', Template(parfile).substitute(locals())))