Skip to main content

Library for setting up finite difference problems

Project description

Description

This is a library for setting up basic time-dependent finite difference problems. It handles the array manipulation and gives strict errors when a model isn't being set up correctly. However, it does not check for instability, nor does it check whether the model is actually physically accurate.

I see this mainly as an educational tool, although if more features are added it might be helpful for some research.

Installation

pip install finite_difference

Contributing

Any contribution or feedback is very welcome -- feel free to create an issue on github. In particular, if you are getting unexpected behavior or unclear errors, or if there's something you would like to see added, I would want to hear.

Features

The library can support:

  • Regular grid domains with arbitrary number of spatial dimensions
  • Periodic or fixed boundary conditions
  • Derivative approximations of arbitrary order and arbitrary sampled coordinates
  • Time-dependent scalar fields with values at cells or edges between cells

Example Use

Shown is all the code required for setting up a thermal diffusion model. For more examples, see the Google Colab.

  1. Create a single Model object for the domain, with a time dimension and space dimension(s).
import finite_difference as fd
m = fd.Model({"x": range(1,100,4), "t": range(1,10)}, time_axis = "t")
  1. Create fields representing a property that changes over time as a scalar field.
T = fd.Field(m, "Temperature", n_time_ders = 1)
  1. Create stencils for numerical approximations of spatial derivatives.
diff_2 = fd.Stencil([-1,0,1], der_order = 2)
  1. Apply boundary conditions and initial conditions.
T.set_IC("1")
T.set_BC("0","x","start")
T.set_BC("0","x","end")
  1. Run the simulation in a loop, updating the fields each iteration.
k = 2 # thermal conductivity

m.check_IC() # not required, but recommended: checks if all necessary initial conditions have been set up

while not m.finished: # checks if it has reached the final timestep

    # implements the equations: dT/dt = k * d^2T/dx^2
    Tp = k*diff_2.der(T.prev)
    T.dot.assign_update(Tp)
    T.time_integrate_update()

    m.increment_time() # increment the time step
  1. Once the run is complete, an interactive visual can be created showing the fields over time. Alternatively, you can get the values of the fields across all time as numpy arrays with the field's data property.
m.interact() # creates an interactive visual in a jupyter notebook

# get numpy array of the temperature:
T.data      

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

finite_difference-0.0.3.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

finite_difference-0.0.3-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file finite_difference-0.0.3.tar.gz.

File metadata

  • Download URL: finite_difference-0.0.3.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for finite_difference-0.0.3.tar.gz
Algorithm Hash digest
SHA256 5d841582e6e73876779427e2e243d46e8b843e241972aca729c475276a5ae459
MD5 fea56433953ffb2db079aef11efcd8d1
BLAKE2b-256 060c3d9aad9c5f46633745d8e4229b560f9a4ebb3f56fd376fb8cd84942c69d9

See more details on using hashes here.

File details

Details for the file finite_difference-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for finite_difference-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 307e17dc04e606cadd7edce4adcbe1f4867bce3ef29c68839bd684cdf8099bdc
MD5 4642309d9536720058246cdfdde71de1
BLAKE2b-256 8e688cd5587d75b1038869f807417539c0514b877453d6694a0ffc849eb7b2fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page