Coverage for pySDC / projects / parallelSDC_reloaded / scripts / fig06_allenCahnMPI.py: 95%
64 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-23 17:16 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-23 17:16 +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"""
9import os
10import sys
11import json
12import numpy as np
13from mpi4py import MPI
15from pySDC.projects.parallelSDC_reloaded.utils import solutionExact, getParamsSDC, solutionSDC, getParamsRK
16from pySDC.implementations.sweeper_classes.generic_implicit_MPI import generic_implicit_MPI
18PATH = '/' + os.path.join(*__file__.split('/')[:-1])
19SCRIPT = __file__.split('/')[-1].split('.')[0]
21COMM_WORLD = MPI.COMM_WORLD
23# SDC parameters
24nNodes = 4
25quadType = 'RADAU-RIGHT'
26nodeType = 'LEGENDRE'
27parEfficiency = 0.8 # 1/nNodes
28nSweeps = 4
30# Problem parameters
31pName = "ALLEN-CAHN"
32tEnd = 50
33pParams = {
34 "periodic": False,
35 "nvars": 2**11 - 1,
36 "epsilon": 0.04,
37}
39# -----------------------------------------------------------------------------
40# %% Convergence and error VS cost plots
41# -----------------------------------------------------------------------------
42nStepsList = np.array([5, 10, 20, 50, 100, 200, 500])
43dtVals = tEnd / nStepsList
46def getError(uNum, uRef):
47 if uNum is None:
48 return np.inf
49 return np.linalg.norm(uRef[-1, :] - uNum[-1, :], ord=2)
52def getCost(counters):
53 _, _, tComp = counters
54 return tComp
57try:
58 qDelta = sys.argv[1]
59 if qDelta.startswith("--"):
60 qDelta = "MIN-SR-FLEX"
61except IndexError:
62 qDelta = "MIN-SR-FLEX"
64try:
65 params = getParamsRK(qDelta)
66except KeyError:
67 params = getParamsSDC(quadType=quadType, numNodes=nNodes, nodeType=nodeType, qDeltaI=qDelta, nSweeps=nSweeps)
69useMPI = False
70if COMM_WORLD.Get_size() == 4 and qDelta in ["MIN-SR-NS", "MIN-SR-S", "MIN-SR-FLEX", "VDHS"]: # pragma: no cover
71 params['sweeper_class'] = generic_implicit_MPI
72 useMPI = True
74errors = []
75costs = []
77root = COMM_WORLD.Get_rank() == 0
78if root:
79 print(f"Running simulation with {qDelta}")
81for nSteps in nStepsList:
82 if root:
83 uRef = solutionExact(tEnd, nSteps, pName, **pParams)
85 uSDC, counters, parallel = solutionSDC(tEnd, nSteps, params, pName, verbose=root, noExcept=True, **pParams)
87 if root:
88 err = getError(uSDC, uRef)
89 errors.append(err)
91 cost = getCost(counters)
92 costs.append(cost)
94if COMM_WORLD.Get_rank() == 0:
95 errors = [float(e) for e in errors]
97 print("errors : ", errors)
98 print("tComps : ", costs)
99 fileName = f"{PATH}/fig06_compTime.json"
100 timings = {}
101 if os.path.isfile(fileName):
102 with open(fileName, "r") as f:
103 timings = json.load(f)
105 timings[qDelta + "_MPI" * useMPI] = {"errors": errors, "costs": costs}
107 with open(fileName, 'w') as f:
108 json.dump(timings, f, indent=4)