1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
# --------------------------------------------------------------------
from petsc4py import PETSc
# --------------------------------------------------------------------
OptDB = PETSc.Options()
INFO = OptDB.hasName('info')
def LOG(arg):
if INFO:
print(arg)
# --------------------------------------------------------------------
class Laplace1D(object):
def create(self, A):
LOG('Laplace1D.create()')
M, N = A.getSize()
assert M == N
def destroy(self, A):
LOG('Laplace1D.destroy()')
def view(self, A, vw):
LOG('Laplace1D.view()')
def setFromOptions(self, A):
LOG('Laplace1D.setFromOptions()')
def setUp(self, A):
LOG('Laplace1D.setUp()')
def assemblyBegin(self, A, flag):
LOG('Laplace1D.assemblyBegin()')
def assemblyEnd(self, A, flag):
LOG('Laplace1D.assemblyEnd()')
def getDiagonal(self, A, d):
LOG('Laplace1D.getDiagonal()')
M, N = A.getSize()
h = 1.0/(M-1)
d.set(2.0/h**2)
def mult(self, A, x, y):
LOG('Laplace1D.mult()')
M, N = A.getSize()
xx = x.getArray(readonly=1) # to numpy array
yy = y.getArray(readonly=0) # to numpy array
yy[0] = 2.0*xx[0] - xx[1]
yy[1:-1] = - xx[:-2] + 2.0*xx[1:-1] - xx[2:]
yy[-1] = - xx[-2] + 2.0*xx[-1]
h = 1.0/(M-1)
yy *= 1.0/h**2
def multTranspose(self, A, x, y):
LOG('Laplace1D.multTranspose()')
self.mult(A, x, y)
# --------------------------------------------------------------------
class Jacobi(object):
def create(self, pc):
LOG('Jacobi.create()')
self.diag = None
def destroy(self, pc):
LOG('Jacobi.destroy()')
if self.diag:
self.diag.destroy()
def view(self, pc, vw):
LOG('Jacobi.view()')
def setFromOptions(self, pc):
LOG('Jacobi.setFromOptions()')
def setUp(self, pc):
LOG('Jacobi.setUp()')
A, B = pc.getOperators()
self.diag = B.getDiagonal(self.diag)
def apply(self, pc, x, y):
LOG('Jacobi.apply()')
y.pointwiseDivide(x, self.diag)
def applyTranspose(self, pc, x, y):
LOG('Jacobi.applyTranspose()')
self.apply(pc, x, y)
# --------------------------------------------------------------------
class ConjGrad(object):
def create(self, ksp):
LOG('ConjGrad.create()')
self.work = []
def destroy(self, ksp):
LOG('ConjGrad.destroy()')
for vec in self.work:
if vec:
vec.destroy()
self.work = []
def view(self, ksp, viewer):
LOG('ConjGrad.view()')
def setUp(self, ksp):
LOG('ConjGrad.setUp()')
self.work[:] = ksp.getWorkVecs(right=3, left=None)
def solve(self, ksp, b, x):
LOG('ConjGrad.solve()')
A, P = get_op_pc(ksp, transpose=False)
pcg(ksp, A, P, b, x, *self.work)
def solveTranspose(self, ksp, b, x):
LOG('ConjGrad.solveTranspose()')
A, P = get_op_pc(ksp, transpose=True)
pcg(ksp, A, P, b, x, *self.work)
def get_op_pc(ksp, transpose=False):
op, _ = ksp.getOperators()
pc = ksp.getPC()
if not transpose:
A = op.mult
P = pc.apply
else:
A = op.multTranspose
P = pc.applyTranspose
return A, P
def do_loop(ksp, r):
its = ksp.getIterationNumber()
rnorm = r.norm()
ksp.setResidualNorm(rnorm)
ksp.logConvergenceHistory(rnorm)
ksp.monitor(its, rnorm)
reason = ksp.callConvergenceTest(its, rnorm)
if not reason:
ksp.setIterationNumber(its+1)
else:
ksp.setConvergedReason(reason)
return reason
def pcg(ksp, A, P, b, x, r, z, p):
A(x, r)
r.aypx(-1, b)
P(r, z)
delta = r.dot(z)
z.copy(p)
while not do_loop(ksp, z):
A(p, z)
alpha = delta / z.dot(p)
x.axpy(+alpha, p)
r.axpy(-alpha, z)
P(r, z)
delta_old = delta
delta = r.dot(z)
beta = delta / delta_old
p.aypx(beta, z)
def richardson(ksp, A, P, b, x, r, z):
A(x, r)
r.aypx(-1, b)
P(r, z)
x.axpy(1, z)
while not do_loop(ksp, z):
A(x, r)
r.aypx(-1, b)
P(r, z)
x.axpy(1, z)
# --------------------------------------------------------------------
|