Skip to content

Commit e82420d

Browse files
committed
compiler: Utilise petsctools and start extending solver parameters functionality
examples: Add example using argparse in conjunction with PetscInitialize
1 parent 91c91c3 commit e82420d

3 files changed

Lines changed: 56 additions & 25 deletions

File tree

devito/petsc/utils.py

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from pathlib import Path
55
from devito.tools import memoized_func
66

7+
from petsctools import get_petscvariables
8+
79

810
class PetscOSError(OSError):
911
pass
@@ -51,30 +53,7 @@ def core_metadata():
5153
}
5254

5355

54-
@memoized_func
55-
def get_petsc_variables():
56-
"""
57-
Taken from https://www.firedrakeproject.org/_modules/firedrake/petsc.html
58-
Get a dict of PETSc environment variables from the file:
59-
$PETSC_DIR/$PETSC_ARCH/lib/petsc/conf/petscvariables
60-
"""
61-
try:
62-
petsc_dir = get_petsc_dir()
63-
except PetscOSError:
64-
petsc_variables = {}
65-
else:
66-
path = [petsc_dir[-1], 'lib', 'petsc', 'conf', 'petscvariables']
67-
variables_path = Path(*path)
68-
69-
with open(variables_path) as fh:
70-
# Split lines on first '=' (assignment)
71-
splitlines = (line.split("=", maxsplit=1) for line in fh.readlines())
72-
petsc_variables = {k.strip(): v.strip() for k, v in splitlines}
73-
74-
return petsc_variables
75-
76-
77-
petsc_variables = get_petsc_variables()
56+
petsc_variables = get_petscvariables()
7857

7958

8059
def get_petsc_type_mappings():

examples/petsc/solver_options.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import os
2+
import numpy as np
3+
4+
from devito import (Grid, Function, Eq, Operator, configuration,
5+
switchconfig)
6+
from devito.petsc import PETScSolve
7+
from devito.petsc.initialize import PetscInitialize
8+
import petsctools
9+
from petsctools import get_commandline_options
10+
configuration['compiler'] = 'custom'
11+
os.environ['CC'] = 'mpicc'
12+
13+
14+
# Initialize petsctools
15+
# petsctools.init()
16+
17+
from argparse import ArgumentParser
18+
19+
parser = ArgumentParser()
20+
21+
parser.add_argument('--nx', type=int, default=11)
22+
parser.add_argument('--ny', type=int, default=11)
23+
24+
args, unknown = parser.parse_known_args()
25+
26+
PetscInitialize(unknown)
27+
28+
grid = Grid(shape=(args.nx, args.ny), extent=(2., 2.), dtype=np.float64)
29+
30+
u = Function(name='u', grid=grid, dtype=np.float64, space_order=2)
31+
v = Function(name='v', grid=grid, dtype=np.float64, space_order=2)
32+
33+
v.data[:] = 5.0
34+
35+
eq = Eq(v, u.laplace, subdomain=grid.interior)
36+
37+
petsc = PETScSolve([eq], u)
38+
39+
with switchconfig(language='petsc'):
40+
op = Operator(petsc)
41+
op.apply()
42+
43+
# print(op.ccode)
44+
45+
# print(grid.shape)
46+
47+
48+
import sys
49+
petsctools.options._commandline_options = sys.argv[1:]
50+
tmp = get_commandline_options()
51+
print("Command line options:", tmp)

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ codepy>=2019.1,<2025
88
multidict<6.3
99
anytree>=2.4.3,<=2.13.0
1010
cloudpickle<3.1.2
11-
packaging<25.1
11+
packaging<25.1
12+
petsctools

0 commit comments

Comments
 (0)