Skip to content

Commit

Permalink
Allow to provide experiment parameters via CLI.
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminRodenberg committed Oct 26, 2023
1 parent f682833 commit 653678e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 13 deletions.
79 changes: 67 additions & 12 deletions partitioned-heat-conduction/doConvergenceStudy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import uuid
import argparse
from enum import Enum


default_precice_config_params = {
Expand All @@ -14,6 +15,13 @@
}


class Experiments(Enum):
POLYNOMIAL0 = 'p0'
POLYNOMIAL1 = 'p1'
POLYNOMIAL2 = 'p2'
TRIGONOMETRIC = 't'


def render(precice_config_params):
base_path = Path(__file__).parent.absolute()

Expand All @@ -30,7 +38,7 @@ def render(precice_config_params):
file.write(precice_config_template.render(precice_config_params))


def do_run(dt, n_substeps=1, polynomial_degree=1, error_tol=10e-3, precice_config_params=default_precice_config_params):
def do_run(dt, n_substeps=1, precice_config_params=default_precice_config_params, experiment_params={}):
time_window_size = dt
time_step_size = time_window_size / n_substeps

Expand All @@ -56,12 +64,11 @@ def do_run(dt, n_substeps=1, polynomial_degree=1, error_tol=10e-3, precice_confi

for participant in participants:
with open(fenics / participant['logfile'], "w") as outfile:
p = subprocess.Popen(["python3",
fenics / "heat.py",
participant["cmd"],
f"-e {error_tol}",
f"-s {n_substeps}",
f"-p {polynomial_degree}"],
cmd = ["python3",
fenics / "heat.py",
participant["cmd"],
f"--n-substeps={n_substeps}"] + [f"{opt}={value}" for opt, value in experiment_params.items()]
p = subprocess.Popen(cmd,
cwd=fenics,
stdout=outfile)
participant["proc"] = p
Expand All @@ -88,9 +95,26 @@ def do_run(dt, n_substeps=1, polynomial_degree=1, error_tol=10e-3, precice_confi
if __name__ == "__main__":

parser = argparse.ArgumentParser(description="Solving heat equation for simple or complex interface case")
parser.add_argument("-dt", "--base-time-window-size", help="Base time window / time step size", type=float, default=0.1)
parser.add_argument("-w", "--time-window-refinements", help="Number of refinements by factor 2 for the time window size", type=int, default=5)
parser.add_argument("-s", "--time-step-refinements", help="Number of refinements by factor 2 for the time step size ( >1 will result in subcycling)", type=int, default=1)
parser.add_argument(
"-dt",
"--base-time-window-size",
help="Base time window / time step size",
type=float,
default=0.1)
parser.add_argument(
"-w",
"--time-window-refinements",
help="Number of refinements by factor 2 for the time window size",
type=int,
default=5)
parser.add_argument(
"-s",
"--time-step-refinements",
help="Number of refinements by factor 2 for the time step size ( >1 will result in subcycling)",
type=int,
default=1)
parser.add_argument("-e", "--experiment", help="Provide identifier for a specific experiment",
choices=[e.value for e in Experiments], default=Experiments.POLYNOMIAL0.value)
args = parser.parse_args()

base_dt = args.base_time_window_size
Expand All @@ -108,12 +132,39 @@ def do_run(dt, n_substeps=1, polynomial_degree=1, error_tol=10e-3, precice_confi
'time_windows_reused': 5,
}

if args.experiment == 'p0':
experiment_params = {
'--polynomial-order': 0,
'--time-dependence': 'polynomial',
}
elif args.experiment == 'p1':
experiment_params = {
'--polynomial-order': 1,
'--time-dependence': 'polynomial',
}
elif args.experiment == 'p2':
experiment_params = {
'--polynomial-order': 2,
'--time-dependence': 'polynomial',
}
elif args.experiment == 't':
experiment_params = {
'--time-dependence': 'trigonometric',
}
else:
raise Exception("Unknown experiment identifier")

experiment_params['--error-tol'] = 10e10

summary_file = Path("convergence-studies") / f"{uuid.uuid4()}.csv"

for dt in dts:
for n in substeps:
summary = do_run(dt, n_substeps=n, polynomial_degree=polynomial_degree,
error_tol=10e10, precice_config_params=precice_config_params)
summary = do_run(
dt,
n_substeps=n,
precice_config_params=precice_config_params,
experiment_params=experiment_params)
df = pd.concat([df, pd.DataFrame(summary, index=[0])], ignore_index=True)

print(f"Write preliminary output to {summary_file}")
Expand All @@ -140,6 +191,7 @@ def do_run(dt, n_substeps=1, polynomial_degree=1, error_tol=10e-3, precice_confi
"git commit": chash,
"args": args,
"precice_config_params": precice_config_params,
"experiment_params": experiment_params,
}

summary_file.unlink()
Expand All @@ -150,5 +202,8 @@ def do_run(dt, n_substeps=1, polynomial_degree=1, error_tol=10e-3, precice_confi
df.to_csv(f)

print('-' * term_size.columns)
for key, value in metadata.items():
print(f"{key}:{value}")
print()
print(df)
print('-' * term_size.columns)
5 changes: 4 additions & 1 deletion partitioned-heat-conduction/fenics/heat.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def determine_gradient(V_g, u, flux):
type=int,
default=1)
parser.add_argument("-p", "--polynomial-order", help="Polynomial order of manufactured solution", type=int, default=1)
parser.add_argument("-t", "--time-dependence", help="Time dependence of manufactured solution",
choices=[e.value for e in TimeDependence], default=TimeDependence.POLYNOMIAL.value)
parser.add_argument("-e", "--error-tol", help="set error tolerance", type=float, default=10**-6,)

args = parser.parse_args()
Expand All @@ -93,7 +95,8 @@ def determine_gradient(V_g, u, flux):
W = V_g.sub(0).collapse()

# Define boundary conditions
u_manufactured, symbols = get_manufactured_solution(TimeDependence.POLYNOMIAL, alpha, beta, p=args.polynomial_order)
u_manufactured, symbols = get_manufactured_solution(TimeDependence(
args.time_dependence), alpha, beta, p=args.polynomial_order)
u_D = Expression(sp.printing.ccode(u_manufactured), degree=2, t=0)
u_D_function = interpolate(u_D, V)

Expand Down

0 comments on commit 653678e

Please sign in to comment.