Giter VIP home page Giter VIP logo

Comments (5)

david-zwicker avatar david-zwicker commented on August 25, 2024

This is certainly possible. The simplest way would be to use the PDE class,

from pde import PDE
eq = PDE({'P': '(P - P**2) * laplace(P)'})

where I used D(x) = x - x**2 as a concrete example. Alternatively, you may implement your own pde class.

See pde_custom_expression.py and pde_custom_class.py in the examples directory for respective implementations.

from py-pde.

ItayXD avatar ItayXD commented on August 25, 2024

I may be missing something, but it seems that you actually used D(P)=P - P**2 rather than D(x) = x - x**2, meaning the coefficient depends on the variable being solved for and not x (which P is being differentiated with respect to)

from py-pde.

david-zwicker avatar david-zwicker commented on August 25, 2024

Ohh, I wasn't reading your question carefully enough – sorry about this. The current implementation of the PDE class does not allow spatially varying coefficients. However, it is not too difficult to achieve this with a custom class:

import pde


class HeterogeneousDiffusionPDE(pde.PDEBase):

    def evolution_rate(self, state, t=0):
        """ implement the python version of the evolution equation """
        assert state.grid.dim == 1  # implementation only works for 1d problems
        cell_coords = state.grid.cell_coords[:, 0]  # this extracts the x-coordinates of all cells
        coeff = (cell_coords - cell_coords**2)  # this calculates the pre-factor
        return state.laplace('natural') * coeff


grid = pde.CartesianGrid([[0, 1]], 32)  # generate grid
state = pde.ScalarField.random_normal(grid, 0.1, 0.9)  # generate initial condition

eq = HeterogeneousDiffusionPDE()  # define the pde
result = eq.solve(state, t_range=1, dt=1e-4)
result.plot()

A numba-compiled implementation to accelerate the computation can be created similarly. I'll think about extending the PDE class, but it might be a bit challenging and thus not happen soon. However, pull requests in this direction are welcome!

from py-pde.

david-zwicker avatar david-zwicker commented on August 25, 2024

I took you suggestion as a challenge and implemented an extended version of the PDE class that can deal with spatially dependent coefficients. The implementation is not very tested, yet, but it seems to work. I added an example that displays how it works, but it really comes down to just using the spatial coordinates as variables in the expressions.

from py-pde.

ItayXD avatar ItayXD commented on August 25, 2024

Thanks! Looks good

from py-pde.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.