Skip to content

Commit f5decfc

Browse files
committed
PETSc datatype WIP
1 parent c6910b7 commit f5decfc

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from petsc4py import PETSc
2+
3+
4+
class mymesh(PETSc.Vec):
5+
6+
def __new__(cls, init=None, val=0.0):
7+
if isinstance(init, mymesh):
8+
obj = PETSc.Vec().__new__(cls)
9+
init.copy(obj)
10+
# obj.
11+
# obj[:] = init[:]
12+
# obj.part1 = obj[0, :]
13+
# obj.part2 = obj[1, :]
14+
elif isinstance(init, tuple):
15+
obj = PETSc.Vec().__new__(cls)
16+
obj.createMPI(size=init[0], comm=init[1])
17+
objarr = obj.getArray()
18+
objarr[:] = val
19+
else:
20+
obj = PETSc.Vec().__new__(cls)
21+
return obj
22+
23+
# def __array_finalize__(self, obj):
24+
# """
25+
# Finalizing the datatype. Without this, new datatypes do not 'inherit' the communicator.
26+
# """
27+
# if obj is None:
28+
# return
29+
# self.part1 = getattr(obj, 'part1', None)
30+
# self.part2 = getattr(obj, 'part2', None)
31+
32+
u = mymesh((10, PETSc.COMM_WORLD), val=9)
33+
uarr = u.getArray()
34+
# uarr[:] = 1
35+
print(uarr, u.getOwnershipRange())
36+
v = mymesh(u)
37+
varr = v.getArray()
38+
print(varr, v.getOwnershipRange() )
39+
if v.comm.getRank() == 0:
40+
uarr[0] = 7
41+
42+
print(uarr)
43+
print(varr)
44+
45+
w = u + v
46+
warr = w.getArray()
47+
print(warr, w.getOwnershipRange())
48+
exit()
49+
50+
51+
m = mymesh((10, np.dtype('float64')))
52+
m.part1[:] = 1
53+
m.part2[:] = 2
54+
print(m.part1, m.part2)
55+
print(m)
56+
print()
57+
58+
n = mymesh(m)
59+
n.part1[0] = 10
60+
print(n.part1, n.part2)
61+
print(n)
62+
print()
63+
64+
print(o.part1, o.part2)
65+
print(o)
66+
print()
67+
# print(n + m)

0 commit comments

Comments
 (0)