Skip to main content

An automized PDE solver for accelerators based on JAX

Project description

AutoPDEx is a free open source partial differential equation (PDE) solver based on the automatic code transformation capabilities of JAX.

The idea of the project is to develop a modular and easily extendable environment for the solution of boundary and initial boundary value problems, which provides automatic sensitivity analysis, allows for good integration with machine learning algorithms and can be executed on accelerators such as GPUs.

The documentation with more examples is available here.

Installation

To install AutoPDEx, you can use the following command. Note, that it requires python>=3.10.

pip install --upgrade pip
pip install autopdex

Example

This is a short example for solving Poisson's problem with homogeneous Dirichlet conditions on the domain $[0,1]\times[0,1]$ with the source term

$$b = 20 \left(\sin{\left(10\ \boldsymbol{x}\cdot\boldsymbol{x}\right)} - \cos{\left(10\ \left(\boldsymbol{x} - \boldsymbol{x}_2\right) \cdot \left(\boldsymbol{x} - \boldsymbol{x}_2\right)\right)}\right)$$

where $\boldsymbol{x}_2 = (1, 0.5)^T$.

Download full example

First, we import the necessary packages and enable double precision.

import jax
import jax.numpy as jnp
import flax
import meshio

from autopdex import seeder, geometry, solver, utility, models, spaces, mesher
jax.config.update("jax_enable_x64", True)

Generate the mesh (or import the node coordinates and connectivity)

pts = [[0., 0.], [1., 0.], [1., 1.], [0., 1.]]
coords, elems = mesher.structured_mesh((200, 200), pts, 'quad')
node_coordinates = {'phi': coords,}
connectivity = {'phi': elems,}

Selection of nodes and degrees of freedom for Dirichlet conditions

sdf = lambda x: geometry.psdf_polygon(x, pts)
dirichlet_nodes = geometry.in_sdfs(node_coordinates['phi'], sdf)
dirichlet_dofs = {'phi': dirichlet_nodes,}
dirichlet_conditions = utility.dict_zeros_like(dirichlet_dofs, dtype=jnp.float64)

The variational problem is defined in terms of the potential $\Pi$. The stationarity condition of this potential gives the Poisson equation. The potential is represented by the integral

$$\Pi(\phi) = \int_\Omega \left( \frac{1}{2} \nabla \phi \cdot \nabla \phi - b \phi \right)\ \mathrm{d}\Omega$$

This leads to the following Euler-Lagrange equation (Poisson's equation):

$$- \Delta \phi = b$$

def integrand_fun(x_int, ansatz_fun, settings, static_settings, elem_number, set):
    # Definition of custom functional    
    x = ansatz_fun['physical coor'](x_int)
    phi_fun = ansatz_fun['phi']
    phi = phi_fun(x_int)
    dphi_dx = jax.jacrev(phi_fun)(x_int)
    x_2 = x - jnp.array([1., 0.5])
    b = 20 * (jnp.sin(10 * x @ x) - jnp.cos(10 * x_2 @ x_2))
    return (1/2) * dphi_dx @ dphi_dx - b * phi

Set up the finite element, here Q1 elements for the field 'phi'

user_potential = models.mixed_reference_domain_potential(
    integrand_fun,
    {'phi': spaces.fem_iso_line_quad_brick,},
    *seeder.gauss_legendre_nd(dimension = 2, order = 2),
    'phi')

Prepare the settings for autopdex

static_settings = flax.core.FrozenDict({
  'assembling mode': ('user potential',),
  'solution structure': ('nodal imposition',),
  'model': (user_potential, ),
  'solver type': 'newton',
  'solver backend': 'scipy',
  'solver': 'lapack',
  'verbose': 1,
})
settings = {
  'connectivity': (connectivity,),
  'dirichlet dofs': dirichlet_dofs,
  'node coordinates': node_coordinates,
  'dirichlet conditions': dirichlet_conditions,
}

Compile, assemble and solve linear system

initial_guess = utility.dict_zeros_like(dirichlet_dofs, dtype=jnp.float64)
dofs = solver.solver(initial_guess, settings, static_settings)[0]

Write vtk file for visualization with Paraview

meshio.Mesh(
    coords,
    {'quad': elems},
    point_data={
        "phi": dofs['phi'],
    },
).write("./short_example.vtk")

Contributions

You are warmly invited to contribute to the project. For larger developments, please get in touch beforehand in order to circumvent double work.

For detailed information on how to contribute, please see our Contribution Guidelines

License

AutoPDEx is licensed under the GNU Affero General Public License, Version 3.

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

autopdex-1.1.4.tar.gz (259.6 kB view details)

Uploaded Source

Built Distribution

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

autopdex-1.1.4-py3-none-any.whl (276.4 kB view details)

Uploaded Python 3

File details

Details for the file autopdex-1.1.4.tar.gz.

File metadata

  • Download URL: autopdex-1.1.4.tar.gz
  • Upload date:
  • Size: 259.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for autopdex-1.1.4.tar.gz
Algorithm Hash digest
SHA256 a0a1bf42fe44610881b5a0f94d1e4ce44c45e0baf2d6dae1401188d9359d47c3
MD5 4b1221c97b7167aed56f7cf70ee13b54
BLAKE2b-256 be05a7251b2bf3ef0296829dd0a7efb460bec9cb520c6fa80ee606645713e326

See more details on using hashes here.

File details

Details for the file autopdex-1.1.4-py3-none-any.whl.

File metadata

  • Download URL: autopdex-1.1.4-py3-none-any.whl
  • Upload date:
  • Size: 276.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for autopdex-1.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 092f469d028f9434f3729f286b9c740579d5fcf2e6467db483834b4d71632763
MD5 f93f69e83a3ae8657638009e79b28a8a
BLAKE2b-256 49d2b4d0c11608acce15f68b7e6fefa880c29342ccb9ba761bcb0d8eb8cacb16

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