Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8992729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:01:33+00:00 2026-06-15T23:01:33+00:00

I’m currently working on a project that involves calculating electric fields and there gradients

  • 0

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;

################################################################################
#  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 > 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 > 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 < 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 < 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]

So when I run a the following

    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)

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.

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).

Might be a long shot that someone has tried to do this before but any help would be really appreciated,

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:01:34+00:00Added an answer on June 15, 2026 at 11:01 pm

    http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.interpolation.map_coordinates.html uses splines to interpolate the data and you choose the degree of the spline with the order paramter, default 3.

    If you are familiar with spline interpolation it “patches” datapoints using low degree polynomials. A quick read on wikipedia http://en.wikipedia.org/wiki/Spline_interpolation or http://math.tut.fi/~piche/numa2/lecture16.pdf

    The order of the polynomial reduces error at the cost of computation time, if your data is not so irregular you can go ahead and try with a lower order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I am doing a simple coin flipping experiment for class that involves flipping a
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.