Giter Club home page Giter Club logo

Comments (4)

adtzlr avatar adtzlr commented on August 15, 2024 1

Nice, thanks for your fast response! I thought a pull request would be a little overkill for that 🙂 so I just opened an Issue. Thanks again!

from pacopy.

adtzlr avatar adtzlr commented on August 15, 2024

Or another 2D-Example:

Just a side-node: I had to use the ugly numpy.array([out0, out1]) code because autograd is not able to do automatic differentiation with item-assignments of arrays out[0] = .... Has nothing to do with pacopy. I did use the older autograd instead of its successor jax as jax won't install on Windows.

from autograd import numpy
import autograd
import scipy
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

import pacopy

class SimpleProblem2D:
    def __init__(self):
        self.n = 2

    def inner(self, a, b):
        """The inner product of the problem. Can be numpy.dot(a, b), but factoring in
        the mesh width stays true to the PDE.
        """
        return numpy.dot(a, b)

    def norm2_r(self, a):
        """The norm in the range space; used to determine if a solution has been found."""
        return numpy.dot(a, a)

    def f(self, u, lmbda):
        """The evaluation of the function to be solved"""
        out0 = numpy.sin(u[0])      - lmbda
        out1 = numpy.cos(u[1])*u[1] - lmbda
        return numpy.array([out0, out1])

    def df_dlmbda(self, u, lmbda):
        """The function's derivative with respect to the parameter. Used in Euler-Newton
        continuation.
        """
        out = -numpy.ones_like(u)
        return out

    def jacobian_solver(self, u, lmbda, rhs):
        """A solver for the Jacobian problem."""
        df_du = autograd.jacobian(self.f)
        return numpy.linalg.solve(df_du(u, lmbda), rhs)
    
problem = SimpleProblem2D()
# Initial guess
u0 = numpy.zeros(problem.n)
# Initial parameter value
lmbda0 = 0.0

# Init lists
lmbda_list = []
values_list = []

def callback(k, lmbda, sol):
    # Use the callback for plotting, writing data to files etc.
    lmbda_list.append(lmbda)
    values_list.append(sol)

pacopy.euler_newton(problem, u0, lmbda0, callback, max_steps=50, newton_tol=1.0e-10)

# plot solution
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
data = numpy.hstack((numpy.array(values_list),
                     numpy.array(lmbda_list).reshape(-1,1))).T
ax.plot(*data,'.-')
ax.set_xlabel('$u_1$')
ax.set_ylabel('$u_2$')
ax.set_zlabel('$\lambda$')

from pacopy.

nschloe avatar nschloe commented on August 15, 2024

Thanks for the suggestions! I've added some simple ODE examples to the main readme.

from pacopy.

nschloe avatar nschloe commented on August 15, 2024

For sure, I fiddled around with it too and created added some plots to the readme.

from pacopy.

Related Issues (12)

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.