Skip to content

Commit 17f0dc5

Browse files
committed
compiler: Edit argument to PetscInitialize
1 parent e82420d commit 17f0dc5

4 files changed

Lines changed: 114 additions & 6 deletions

File tree

devito/finite_differences/finite_difference.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# Number of digits for FD coefficients to avoid roundup errors and non-deterministic
1111
# code generation
12-
_PRECISION = 9
12+
_PRECISION = 18
1313

1414

1515
@check_input

devito/petsc/initialize.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
_petsc_initialized = False
1313

1414

15-
def PetscInitialize():
15+
def PetscInitialize(clargs=sys.argv):
1616
global _petsc_initialized
1717
if not _petsc_initialized:
1818
dummy = Symbol(name='d')
@@ -33,11 +33,12 @@ def PetscInitialize():
3333
# `argv_bytes` must be a list so the memory address persists
3434
# `os.fsencode` should be preferred over `string().encode('utf-8')`
3535
# in case there is some system specific encoding in use
36-
argv_bytes = list(map(os.fsencode, sys.argv))
37-
argv_pointer = (POINTER(c_char)*len(sys.argv))(
36+
argv_bytes = list(map(os.fsencode, clargs))
37+
argv_pointer = (POINTER(c_char)*len(clargs))(
3838
*map(lambda s: cast(s, POINTER(c_char)), argv_bytes)
3939
)
40-
op_init.apply(argc=len(sys.argv), argv=argv_pointer)
40+
# from IPython import embed; embed()
41+
op_init.apply(argc=len(clargs), argv=argv_pointer)
4142

4243
atexit.register(op_finalize.apply)
4344
_petsc_initialized = True

examples/petsc/solver_options.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
2+
static char help[] = "Modified snes tutorial 1\n\n";
3+
4+
#include <petscsnes.h>
5+
6+
extern PetscErrorCode FormJacobian1(SNES, Vec, Mat, Mat, void *);
7+
extern PetscErrorCode FormFunction1(SNES, Vec, Vec, void *);
8+
9+
int main(int argc, char **argv)
10+
{
11+
SNES snes;
12+
KSP ksp;
13+
PC pc;
14+
Vec x, r;
15+
Mat J;
16+
PetscMPIInt size;
17+
PetscBool flg;
18+
PetscFunctionBeginUser;
19+
20+
PetscCall(PetscInitialize(&argc, &argv, NULL, help));
21+
PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
22+
23+
PetscCall(PetscOptionsSetValue(NULL, "-poisson_ksp_type", "cg"));
24+
PetscCall(PetscOptionsInsert(NULL, &argc, &argv, NULL));
25+
26+
PetscCall(SNESCreate(PETSC_COMM_WORLD, &snes));
27+
PetscCall(SNESSetOptionsPrefix(snes, "poisson_"));
28+
PetscCall(SNESSetFromOptions(snes));
29+
30+
PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
31+
PetscCall(VecSetSizes(x, PETSC_DECIDE, 2));
32+
PetscCall(VecSetFromOptions(x));
33+
PetscCall(VecDuplicate(x, &r));
34+
35+
PetscCall(MatCreate(PETSC_COMM_WORLD, &J));
36+
PetscCall(MatSetSizes(J, PETSC_DECIDE, PETSC_DECIDE, 2, 2));
37+
PetscCall(MatSetFromOptions(J));
38+
PetscCall(MatSetUp(J));
39+
40+
PetscCall(SNESSetFunction(snes, r, FormFunction1, NULL));
41+
PetscCall(SNESSetJacobian(snes, J, J, FormJacobian1, NULL));
42+
43+
PetscCall(SNESGetKSP(snes, &ksp));
44+
PetscCall(KSPGetPC(ksp, &pc));
45+
PetscCall(PCSetType(pc, PCNONE));
46+
PetscCall(KSPSetTolerances(ksp, 1.e-4, PETSC_CURRENT, PETSC_CURRENT, 20));
47+
48+
49+
// PetscCall(VecSet(x, pfive));
50+
PetscCall(SNESSolve(snes, NULL, x));
51+
52+
PetscCall(VecDestroy(&x));
53+
PetscCall(VecDestroy(&r));
54+
PetscCall(MatDestroy(&J));
55+
PetscCall(SNESDestroy(&snes));
56+
PetscCall(PetscFinalize());
57+
return 0;
58+
}
59+
60+
PetscErrorCode FormFunction1(SNES snes, Vec x, Vec f, void *ctx)
61+
{
62+
const PetscScalar *xx;
63+
PetscScalar *ff;
64+
65+
PetscFunctionBeginUser;
66+
67+
PetscCall(VecGetArrayRead(x, &xx));
68+
PetscCall(VecGetArray(f, &ff));
69+
70+
/* Compute function */
71+
ff[0] = xx[0] * xx[0] + xx[0] * xx[1] - 3.0;
72+
ff[1] = xx[0] * xx[1] + xx[1] * xx[1] - 6.0;
73+
74+
/* Restore vectors */
75+
PetscCall(VecRestoreArrayRead(x, &xx));
76+
PetscCall(VecRestoreArray(f, &ff));
77+
PetscFunctionReturn(PETSC_SUCCESS);
78+
}
79+
80+
PetscErrorCode FormJacobian1(SNES snes, Vec x, Mat jac, Mat B, void *dummy)
81+
{
82+
const PetscScalar *xx;
83+
PetscScalar A[4];
84+
PetscInt idx[2] = {0, 1};
85+
86+
PetscFunctionBeginUser;
87+
88+
PetscCall(VecGetArrayRead(x, &xx));
89+
90+
A[0] = 2.0 * xx[0] + xx[1];
91+
A[1] = xx[0];
92+
A[2] = xx[1];
93+
A[3] = xx[0] + 2.0 * xx[1];
94+
PetscCall(MatSetValues(B, 2, idx, 2, idx, A, INSERT_VALUES));
95+
96+
PetscCall(VecRestoreArrayRead(x, &xx));
97+
98+
PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
99+
PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
100+
if (jac != B) {
101+
PetscCall(MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY));
102+
PetscCall(MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY));
103+
}
104+
PetscFunctionReturn(PETSC_SUCCESS);
105+
}

examples/petsc/solver_options.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@
4848
import sys
4949
petsctools.options._commandline_options = sys.argv[1:]
5050
tmp = get_commandline_options()
51-
print("Command line options:", tmp)
51+
print("Command line options:", tmp)
52+
53+

0 commit comments

Comments
 (0)