Coverage for pySDC/projects/parallelSDC_reloaded/scripts/fig06_allenCahnMPI.py: 95%
64 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-16 14:51 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-16 14:51 +0000
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""
4Created on Thu Jan 11 11:14:01 2024
6Figures with experiments on the Allen-Cahn problem (MPI runs)
7"""
8import os
9import sys
10import json
11import numpy as np
12from mpi4py import MPI
14from pySDC.projects.parallelSDC_reloaded.utils import solutionExact, getParamsSDC, solutionSDC, getParamsRK
15from pySDC.implementations.sweeper_classes.generic_implicit_MPI import generic_implicit_MPI
17PATH = '/' + os.path.join(*__file__.split('/')[:-1])
18SCRIPT = __file__.split('/')[-1].split('.')[0]
20COMM_WORLD = MPI.COMM_WORLD
22# SDC parameters
23nNodes = 4
24quadType = 'RADAU-RIGHT'
25nodeType = 'LEGENDRE'
26parEfficiency = 0.8 # 1/nNodes
27nSweeps = 4
29# Problem parameters
30pName = "ALLEN-CAHN"
31tEnd = 50
32pParams = {
33 "periodic": False,
34 "nvars": 2**11 - 1,
35 "epsilon": 0.04,
36}
38# -----------------------------------------------------------------------------
39# %% Convergence and error VS cost plots
40# -----------------------------------------------------------------------------
41nStepsList = np.array([5, 10, 20, 50, 100, 200, 500])
42dtVals = tEnd / nStepsList
45def getError(uNum, uRef):
46 if uNum is None:
47 return np.inf
48 return np.linalg.norm(uRef[-1, :] - uNum[-1, :], ord=2)
51def getCost(counters):
52 _, _, tComp = counters
53 return tComp
56try:
57 qDelta = sys.argv[1]
58 if qDelta.startswith("--"):
59 qDelta = "MIN-SR-FLEX"
60except IndexError:
61 qDelta = "MIN-SR-FLEX"
63try:
64 params = getParamsRK(qDelta)
65except KeyError:
66 params = getParamsSDC(quadType=quadType, numNodes=nNodes, nodeType=nodeType, qDeltaI=qDelta, nSweeps=nSweeps)
68useMPI = False
69if COMM_WORLD.Get_size() == 4 and qDelta in ["MIN-SR-NS", "MIN-SR-S", "MIN-SR-FLEX", "VDHS"]: # pragma: no cover
70 params['sweeper_class'] = generic_implicit_MPI
71 useMPI = True
73errors = []
74costs = []
76root = COMM_WORLD.Get_rank() == 0
77if root:
78 print(f"Running simulation with {qDelta}")
80for nSteps in nStepsList:
81 if root:
82 uRef = solutionExact(tEnd, nSteps, pName, **pParams)
84 uSDC, counters, parallel = solutionSDC(tEnd, nSteps, params, pName, verbose=root, noExcept=True, **pParams)
86 if root:
87 err = getError(uSDC, uRef)
88 errors.append(err)
90 cost = getCost(counters)
91 costs.append(cost)
93if COMM_WORLD.Get_rank() == 0:
94 errors = [float(e) for e in errors]
96 print("errors : ", errors)
97 print("tComps : ", costs)
98 fileName = f"{PATH}/fig06_compTime.json"
99 timings = {}
100 if os.path.isfile(fileName):
101 with open(fileName, "r") as f:
102 timings = json.load(f)
104 timings[qDelta + "_MPI" * useMPI] = {"errors": errors, "costs": costs}
106 with open(fileName, 'w') as f:
107 json.dump(timings, f, indent=4)