Considering something along the lines of
from fenics import *
import numpy as np
# Create mesh and function space
mesh = UnitSquareMesh(2, 2)
V = FunctionSpace(mesh, 'P', 1)
# Define trial and test functions
u = TrialFunction(V)
v = TestFunction(V)
a = inner(grad(u), grad(v)) * dx
A = PETScMatrix()
assemble(a, tensor=A)
def print_entries(entries):
# DO NOT DO THIS IN AN ACTUAL CODE. You can use A.get instead
AM = np.array(A.array())
for entry in entries:
print(f"Before: A[{entry[0], entry[1]}] = {AM[entry[0], entry[1]]}")
print("Before")
print_entries([(0, 0), (0, 1), (1, 0), (1, 1)])
A.set([[2.0, 3.0], [4.0, 5.0]], [0, 1], [0, 1])
A.apply("insert")
print("After")
print_entries([(0, 0), (0, 1), (1, 0), (1, 1)])
Furthermore read Read before posting: How do I get my question answered? (next time, please format the code properly) and The new DOLFINx solver is now recommended over DOLFIN