from pytest_check import check
import numpy as np

import fenics
import fenics_adjoint as fa
import ufl

import fdm

from fenics_numpy import evaluate_primal, evaluate_vjp, evaluate_jvp
from fenics_numpy import fenics_to_numpy, numpy_to_fenics


mesh = fa.UnitSquareMesh(3, 2)
V = fenics.FunctionSpace(mesh, "P", 1)


def assemble_fenics(u, kappa0, kappa1):

    f = fa.Expression(
        "10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2
    )

    inner, grad, dx = ufl.inner, ufl.grad, ufl.dx
    J_form = 0.5 * inner(kappa0 * grad(u), grad(u)) * dx - kappa1 * f * u * dx
    J = fa.assemble(J_form)
    return J


templates = (fa.Function(V), fa.Constant(0.0), fa.Constant(0.0))
inputs = (np.ones(V.dim()), np.ones(1) * 0.5, np.ones(1) * 0.6)
Exemplo n.º 2
0
import pytest

import numpy as np

import fenics
import fenics_adjoint as fa
import ufl

import fdm

from fecr import evaluate_primal, evaluate_pullback, evaluate_pushforward
from fecr import to_numpy

mesh = fa.UnitSquareMesh(6, 5)
V = fenics.FunctionSpace(mesh, "P", 1)


def solve_fenics(kappa0, kappa1):

    f = fa.Expression(
        "10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2)

    u = fa.Function(V)
    bcs = [fa.DirichletBC(V, fa.Constant(0.0), "on_boundary")]

    inner, grad, dx = ufl.inner, ufl.grad, ufl.dx
    JJ = 0.5 * inner(kappa0 * grad(u), grad(u)) * dx - kappa1 * f * u * dx
    v = fenics.TestFunction(V)
    F = fenics.derivative(JJ, u, v)
    fa.solve(F == 0, u, bcs=bcs)
    return u
Exemplo n.º 3
0
import fenics
import fenics_adjoint as fa
import ufl

import fdm

from fenics_pymc3 import create_fenics_theano_op
from fenics_pymc3 import to_numpy

import pymc3 as pm
import theano.tensor as tt

fenics.set_log_level(fenics.LogLevel.ERROR)

n = 25
mesh = fa.UnitSquareMesh(n, n)
V = fenics.FunctionSpace(mesh, "P", 1)
DG = fenics.FunctionSpace(mesh, "DG", 0)


def solve_fenics(kappa0, kappa1):

    f = fa.Expression(
        "10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2)

    u = fa.Function(V)
    bcs = [fa.DirichletBC(V, fa.Constant(0.0), "on_boundary")]

    inner, grad, dx = ufl.inner, ufl.grad, ufl.dx
    JJ = 0.5 * inner(kappa0 * grad(u), grad(u)) * dx - kappa1 * f * u * dx
    v = fenics.TestFunction(V)