Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to fit a function to a 3D numpy array?
    primarykey
    data
    text
    <p>I'm currently working on a project that involves calculating electric fields and there gradients in 3D space. This requires numerically solving Laplace's equation and I have written a class to do this (below) which works but here it is just for background;</p> <pre><code>################################################################################ # class: ThreeDRectLaplaceSolver # # # # A class to solve the Laplace equation given the potential on the boundaries.# ################################################################################ class ThreeDCuboidLaplaceSolver: ############################################################################# # Store the init variables as class varibales, calculate the grid spacing to# # solve Laplace's equation on and create arrays to store the iterations and # # results in. # ############################################################################ def __init__(self, xmin, xmax, ymin, ymax, zmin, zmax, gridstep): self.xmin, self.xmax = xmin, xmax self.ymin, self.ymax = ymin, ymax self.zmin, self.zmax = zmin, zmax self.xpoints = int((xmax-xmin)/gridstep) + 1 self.ypoints = int((ymax-ymin)/gridstep) + 1 self.zpoints = int((zmax-zmin)/gridstep) + 1 self.dx = (xmax-xmin)/self.xpoints self.dy = (ymax-ymin)/self.ypoints self.dz = (zmax-zmin)/self.zpoints self.v = np.zeros((self.xpoints, self.ypoints, self.zpoints)) self.old_v = self.v.copy() self.timeStep = 0 ############################################################################ # Set constant values along the boundaries # # # # Top(bottom) is +ive(-ive) end of z-axis # # Right(left) is +ive(-ive) end of y-axis # # Front(back) is +ive(-ive) end of x-axis # ############################################################################ def setBC(self, frontBC, backBC, rightBC, leftBC, topBC, bottomBC): self.v[-1, :, :] = frontBC self.v[0 , :, :] = backBC self.v[: ,-1, :] = rightBC self.v[: , 0, :] = leftBC self.v[: , :,-1] = topBC self.v[: , :, 0] = bottomBC self.old_v = self.v.copy() def solve_slow(self, PercentageTolerance = 5): PercentageError = PercentageTolerance + 1 while PercentageError &gt; PercentageTolerance: self.Iterate() PercentageError = self.Get_LargestPercentageError() print "Completed iteration number %s \n Percentage Error is %s\n" % (str(self.timeStep), str(PercentageError)) return self.v def solve_quick(self, Tolerance = 2): AbsError = Tolerance + 1 while AbsError &gt; Tolerance: self.Iterate() AbsError = self.Get_LargestAbsError() print "Completed iteration number %s \nAbsolute Error is %s\n" % (str(self.timeStep), str(AbsError)) return self.v def Get_LargestAbsError(self): return np.sqrt((self.v - self.old_v)**2).max() def Get_LargestPercentageError(self): AbsDiff = (np.sqrt((self.v - self.old_v)**2)).flatten() v = self.v.flatten() vLength = len(v) Errors = [] i=0 while i &lt; vLength: if v[i]==0 and AbsDiff[i]==0: Errors.append(0) elif v[i]==0 and AbsDiff[i]!=0: Errors.append(np.infty) else: Errors.append(AbsDiff[i]/v[i]) i+=1 return max(Errors)*100 # Perform one round of iteration (ie the value at each point is iterated by one timestep) def Iterate(self): self.old_v = self.v.copy() print self.Get_vAt(0,5,0) self.v[1:-1,1:-1,1:-1] = (1/26)*(self.v[0:-2, 2:, 2: ] + self.v[0:-2, 1:-1, 2: ] + self.v[0:-2, 0:-2, 2: ] +\ self.v[1:-1, 2:, 2: ] + self.v[1:-1, 1:-1, 2: ] + self.v[1:-1, 0:-2, 2: ] +\ self.v[2: , 2:, 2: ] + self.v[2: , 1:-1, 2: ] + self.v[2: , 0:-2, 2: ] +\ self.v[0:-2, 2:, 1:-1] + self.v[0:-2, 1:-1, 1:-1] + self.v[0:-2, 0:-2, 1:-1] +\ self.v[1:-1, 2:, 1:-1] + self.v[1:-1, 0:-2, 1:-1] +\ self.v[2: , 2:, 1:-1] + self.v[2: , 1:-1, 1:-1] + self.v[2: , 0:-2, 1:-1] +\ self.v[0:-2, 2:, 0:-2] + self.v[0:-2, 1:-1, 0:-2] + self.v[0:-2, 0:-2, 0:-2] +\ self.v[1:-1, 2:, 0:-2] + self.v[1:-1, 1:-1, 0:-2] + self.v[1:-1, 0:-2, 0:-2] +\ self.v[2: , 2:, 0:-2] + self.v[2: , 1:-1, 0:-2] + self.v[2: , 0:-2, 0:-2]) self.timeStep += 1 # Iterate through a certain number of time steps def IterateSteps(self, timeSteps): i = 0 while i &lt; timeSteps: self.Iterate() i+=1 # Get the value of v at a point (entered as coordinates, NOT indices) def Get_vAt(self, xPoint, yPoint, zPoint): # Find the indices nearest to the coordinates entered diff = [np.sqrt((x-xPoint)**2) for x in np.linspace(self.xmin,self.xmax,self.xpoints)] xIndex = diff.index(min(diff)) diff = [np.sqrt((y-yPoint)**2) for y in np.linspace(self.ymin,self.ymax,self.ypoints)] yIndex = diff.index(min(diff)) diff = [np.sqrt((z-zPoint)**2) for z in np.linspace(self.zmin,self.zmax,self.zpoints)] zIndex = diff.index(min(diff)) # retun the value from of v at this point return self.v[xIndex, yIndex, zIndex] </code></pre> <p>So when I run a the following</p> <pre><code> solver = ThreeDCuboidLaplaceSolver(0, 20, 0, 10, 0, 20, 1) TwoDsolver = TwoDRectLaplaceSolver(0,20,0,10,1) TwoDsolver.setBC(1000,0,0,0) v_0 = np.zeros((21,11)) v_0[4:6,4:6] = 1000 v_0[14:15, 9:10] = 2000 solver.setBC(0,0,0,0,0,v_0) v = solver.solve_quick(0.00001) </code></pre> <p>I get a 3D numpy array with the value of the potential (V) at each point on my grid. However in order to do more useful things with this I would like to be able to approximate this array of values with a continuous function so I can calculate the field and its gradient at points not on my grid.</p> <p>A) Is this possibe? B) How would you go about doing this? I have seen basic scipy fitting functions but nothing that would handle 3D data in this way (or indeed fit an analogous function for a 2D array).</p> <p>Might be a long shot that someone has tried to do this before but any help would be really appreciated,</p> <p>Thanks </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload