|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Created on Thu Jan 11 11:14:01 2024 |
| 5 | +
|
| 6 | +Figures with experiments on the Allen-Cahn problem (MPI runs) |
| 7 | +""" |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import json |
| 11 | +import numpy as np |
| 12 | +from mpi4py import MPI |
| 13 | + |
| 14 | +from pySDC.projects.parallelSDC_reloaded.utils import solutionExact, getParamsSDC, solutionSDC, getParamsRK |
| 15 | +from pySDC.implementations.sweeper_classes.generic_implicit_MPI import generic_implicit_MPI |
| 16 | + |
| 17 | +PATH = '/' + os.path.join(*__file__.split('/')[:-1]) |
| 18 | +SCRIPT = __file__.split('/')[-1].split('.')[0] |
| 19 | + |
| 20 | +COMM_WORLD = MPI.COMM_WORLD |
| 21 | + |
| 22 | +# SDC parameters |
| 23 | +nNodes = 4 |
| 24 | +quadType = 'RADAU-RIGHT' |
| 25 | +nodeType = 'LEGENDRE' |
| 26 | +parEfficiency = 0.8 # 1/nNodes |
| 27 | +nSweeps = 4 |
| 28 | + |
| 29 | +# Problem parameters |
| 30 | +pName = "ALLEN-CAHN" |
| 31 | +tEnd = 50 |
| 32 | +pParams = { |
| 33 | + "periodic": False, |
| 34 | + "nvars": 2**11 - 1, |
| 35 | + "epsilon": 0.04, |
| 36 | +} |
| 37 | + |
| 38 | +# ----------------------------------------------------------------------------- |
| 39 | +# %% Convergence and error VS cost plots |
| 40 | +# ----------------------------------------------------------------------------- |
| 41 | +nStepsList = np.array([5, 10, 20, 50, 100, 200, 500]) |
| 42 | +dtVals = tEnd / nStepsList |
| 43 | + |
| 44 | + |
| 45 | +def getError(uNum, uRef): |
| 46 | + if uNum is None: |
| 47 | + return np.inf |
| 48 | + return np.linalg.norm(uRef[-1, :] - uNum[-1, :], ord=2) |
| 49 | + |
| 50 | + |
| 51 | +def getCost(counters): |
| 52 | + _, _, tComp = counters |
| 53 | + return tComp |
| 54 | + |
| 55 | + |
| 56 | +try: |
| 57 | + qDelta = sys.argv[1] |
| 58 | + if qDelta.startswith("--"): |
| 59 | + qDelta = "MIN-SR-FLEX" |
| 60 | +except IndexError: |
| 61 | + qDelta = "MIN-SR-FLEX" |
| 62 | + |
| 63 | +try: |
| 64 | + params = getParamsRK(qDelta) |
| 65 | +except KeyError: |
| 66 | + params = getParamsSDC(quadType=quadType, numNodes=nNodes, nodeType=nodeType, qDeltaI=qDelta, nSweeps=nSweeps) |
| 67 | + |
| 68 | +useMPI = False |
| 69 | +if 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 |
| 72 | + |
| 73 | +errors = [] |
| 74 | +costs = [] |
| 75 | + |
| 76 | +root = COMM_WORLD.Get_rank() == 0 |
| 77 | +if root: |
| 78 | + print(f"Running simulation with {qDelta}") |
| 79 | + |
| 80 | +for nSteps in nStepsList: |
| 81 | + if root: |
| 82 | + uRef = solutionExact(tEnd, nSteps, pName, **pParams) |
| 83 | + |
| 84 | + uSDC, counters, parallel = solutionSDC(tEnd, nSteps, params, pName, verbose=root, noExcept=True, **pParams) |
| 85 | + |
| 86 | + if root: |
| 87 | + err = getError(uSDC, uRef) |
| 88 | + errors.append(err) |
| 89 | + |
| 90 | + cost = getCost(counters) |
| 91 | + costs.append(cost) |
| 92 | + |
| 93 | +if COMM_WORLD.Get_rank() == 0: |
| 94 | + errors = [float(e) for e in errors] |
| 95 | + |
| 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) |
| 103 | + |
| 104 | + timings[qDelta + "_MPI" * useMPI] = {"errors": errors, "costs": costs} |
| 105 | + |
| 106 | + with open(fileName, 'w') as f: |
| 107 | + json.dump(timings, f, indent=4) |
0 commit comments