After extracting nodal values from the solution array, I want to reorder these values so that they correspond to new_coordinates. If I use the numpy array instead of dof_coor
it is working well, I am not sure what is going wrong if I use dof_coor array to search for coordinates. It is giving key missing error. I would appreciate any help.
import numpy as np
import matplotlib.pyplot as plt
from dolfinx.fem import functionspace
from dolfinx.mesh import create_unit_square
from mpi4py import MPI
# define mesh and function space
N = 3
degree = 1
msh = create_unit_square(MPI.COMM_WORLD,N,N)
V = functionspace(msh, ("Lagrange", degree))
x = SpatialCoordinate(msh)
uf = fem.Function(V)
uf.interpolate(lambda x : x[0] + x[1])
# points = msh.geometry.x # I can use either this or tabulate_dof_coordinates() to get coordinates of corresponding nodal values
# dof_coor = np.delete(points,2,1)
array = uf.x.array
print(array)
# print(dof_coor.dtype)
dof_coor = V.tabulate_dof_coordinates()
dof_coor = np.delete(dof_coor,2,1)
print(dof_coor)
import numpy as np
import matplotlib.pyplot as plt
# Original coordinates and values if I use this with new_coords then it works well
# old_coords = np.array([[0.66666667, 0. ],
# [1. , 0. ],
# [1. , 0.33333333],
# [0.66666667, 0.33333333],
# [0.33333333, 0. ],
# [1. , 0.66666667],
# [0.33333333, 0.33333333],
# [0.66666667, 0.66666667],
# [0. , 0. ],
# [1. , 1. ],
# [0. , 0.33333333],
# [0.33333333, 0.66666667],
# [0.66666667, 1. ],
# [0. , 0.66666667],
# [0.33333333, 1. ],
# [0. , 1. ]])
old_coords = dof_coor # but if I use this and meshgrid coordinates it doesn't work well
values = array
# Given new coordinates
# new_coords = np.array([[0. , 0. ],
# [0.33333333, 0. ],
# [0.66666667, 0. ],
# [1. , 0. ],
# [0. , 0.33333333],
# [0.33333333, 0.33333333],
# [0.66666667, 0.33333333],
# [1. , 0.33333333],
# [0. , 0.66666667],
# [0.33333333, 0.66666667],
# [0.66666667, 0.66666667],
# [1. , 0.66666667],
# [0. , 1. ],
# [0.33333333, 1. ],
# [0.66666667, 1. ],
# [1. , 1. ]])
# mesh
a = 0
b = 1
x, y = np.linspace(a,b,4,dtype = np.float64), np.linspace(a,b,4,dtype = np.float64)
X,Y = np.meshgrid(x,y)
X_flat, Y_flat = X.flatten(), Y.flatten()
new_coords = np.vstack((X_flat, Y_flat)).T
print(new_coords)
# Create a mapping from old coordinates to values
mapping = {tuple(coord): value for coord, value in zip(old_coords, values)}
print(mapping)
# Use the mapping to assign values to new coordinates
new_values = np.array([mapping[tuple(coord)] for coord in new_coords])
new_values = new_values.reshape((4,4))
X,Y = np.meshgrid(new_values[:,0],new_values[:,1])
plt.scatter(X,Y)
error
KeyError Traceback (most recent call last)
<ipython-input-92-ef8cf7fe0e24> in <cell line: 56>()
54 print(mapping)
55 # Use the mapping to assign values to new coordinates
---> 56 new_values = np.array([mapping[tuple(coord)] for coord in new_coords])
57 new_values = new_values.reshape((4,4))
58 X,Y = np.meshgrid(new_values[:,0],new_values[:,1])
<ipython-input-92-ef8cf7fe0e24> in <listcomp>(.0)
54 print(mapping)
55 # Use the mapping to assign values to new coordinates
---> 56 new_values = np.array([mapping[tuple(coord)] for coord in new_coords])
57 new_values = new_values.reshape((4,4))
58 X,Y = np.meshgrid(new_values[:,0],new_values[:,1])
KeyError: (0.0, 0.0)