def test_mesh_quality_2D():
    mesh, _, _, _, _, _ = cashocs.regular_mesh(2)

    opt_angle = 60 / 360 * 2 * np.pi
    alpha_1 = 90 / 360 * 2 * np.pi
    alpha_2 = 45 / 360 * 2 * np.pi

    q_1 = 1 - np.maximum((alpha_1 - opt_angle) / (np.pi - opt_angle),
                         (opt_angle - alpha_1) / (opt_angle))
    q_2 = 1 - np.maximum((alpha_2 - opt_angle) / (np.pi - opt_angle),
                         (opt_angle - alpha_2) / (opt_angle))
    q = np.minimum(q_1, q_2)

    min_max_angle = MeshQuality.min_maximum_angle(mesh)
    min_radius_ratios = MeshQuality.min_radius_ratios(mesh)
    average_radius_ratios = MeshQuality.avg_radius_ratios(mesh)
    min_condition = MeshQuality.min_condition_number(mesh)
    average_condition = MeshQuality.avg_condition_number(mesh)

    assert abs(min_max_angle - MeshQuality.avg_maximum_angle(mesh)) < 1e-14
    assert abs(min_max_angle - q) < 1e-14
    assert abs(min_max_angle - MeshQuality.avg_skewness(mesh)) < 1e-14
    assert abs(min_max_angle - MeshQuality.min_skewness(mesh)) < 1e-14

    assert abs(min_radius_ratios - average_radius_ratios) < 1e-14
    assert abs(min_radius_ratios -
               np.min(fenics.MeshQuality.radius_ratio_min_max(mesh))) < 1e-14

    assert abs(min_condition - average_condition) < 1e-14
    assert abs(min_condition - 0.4714045207910318) < 1e-14
def test_newton_solver():
    mesh, _, boundaries, dx, ds, _ = cashocs.regular_mesh(5)
    V = FunctionSpace(mesh, 'CG', 1)

    u = Function(V)
    u_fen = Function(V)
    v = TestFunction(V)

    F = inner(grad(u), grad(v)) * dx + Constant(1e2) * pow(
        u, 3) * v * dx - Constant(1) * v * dx
    bcs = cashocs.create_bcs_list(V, Constant(0), boundaries, [1, 2, 3, 4])

    solve(F == 0, u, bcs)
    u_fen.vector()[:] = u.vector()[:]
    u.vector()[:] = 0.0
    cashocs.damped_newton_solve(F,
                                u,
                                bcs,
                                rtol=1e-9,
                                atol=1e-10,
                                max_iter=50,
                                convergence_type='combined',
                                norm_type='l2',
                                damped=False,
                                verbose=True)

    assert np.allclose(u.vector()[:], u_fen.vector()[:])
def test_empty_measure():
    mesh, _, _, dx, ds, dS = cashocs.regular_mesh(5)
    V = fenics.FunctionSpace(mesh, 'CG', 1)
    dm = cashocs.utils.EmptyMeasure(dx)

    trial = fenics.TrialFunction(V)
    test = fenics.TestFunction(V)

    assert fenics.assemble(1 * dm) == 0.0
    assert (fenics.assemble(test * dm).norm('linf')) == 0.0
    assert (fenics.assemble(trial * test * dm).norm('linf')) == 0.0
def test_regular_mesh():
    lens = np.random.uniform(0.5, 2, 2)
    r_mesh, _, _, _, _, _ = cashocs.regular_mesh(2, lens[0], lens[1])

    max_vals = np.random.uniform(0.5, 1, 3)
    min_vals = np.random.uniform(-1, -0.5, 3)

    s_mesh, _, _, _, _, _ = cashocs.regular_box_mesh(2, min_vals[0],
                                                     min_vals[1], min_vals[2],
                                                     max_vals[0], max_vals[1],
                                                     max_vals[2])

    assert np.allclose(c_mesh.coordinates(), u_mesh.coordinates())

    assert np.alltrue((np.max(r_mesh.coordinates(), axis=0) - lens) < 1e-14)
    assert np.alltrue(
        (np.min(r_mesh.coordinates(), axis=0) - np.array([0, 0])) < 1e-14)

    assert np.alltrue(
        abs(np.max(s_mesh.coordinates(), axis=0) - max_vals) < 1e-14)
    assert np.alltrue(
        abs(np.min(s_mesh.coordinates(), axis=0) - min_vals) < 1e-14)
def test_mesh_quality_3D():
    mesh, _, _, _, _, _ = cashocs.regular_mesh(2, 1.0, 1.0, 1.0)
    opt_angle = np.arccos(1 / 3)
    dh_min_max = fenics.MeshQuality.dihedral_angles_min_max(mesh)
    alpha_min = dh_min_max[0]
    alpha_max = dh_min_max[1]

    q_1 = 1 - np.maximum((alpha_max - opt_angle) / (np.pi - opt_angle),
                         (opt_angle - alpha_max) / (opt_angle))
    q_2 = 1 - np.maximum((alpha_min - opt_angle) / (np.pi - opt_angle),
                         (opt_angle - alpha_min) / (opt_angle))
    q = np.minimum(q_1, q_2)

    r_1 = 1 - np.maximum((alpha_max - opt_angle) / (np.pi - opt_angle), 0.0)
    r_2 = 1 - np.maximum((alpha_min - opt_angle) / (np.pi - opt_angle), 0.0)
    r = np.minimum(r_1, r_2)

    min_max_angle = MeshQuality.min_maximum_angle(mesh)
    min_radius_ratios = MeshQuality.min_radius_ratios(mesh)
    min_skewness = MeshQuality.min_skewness(mesh)
    average_radius_ratios = MeshQuality.avg_radius_ratios(mesh)
    min_condition = MeshQuality.min_condition_number(mesh)
    average_condition = MeshQuality.avg_condition_number(mesh)

    assert abs(min_max_angle - MeshQuality.avg_maximum_angle(mesh)) < 1e-14
    assert abs(min_max_angle - r) < 1e-14

    assert abs(min_skewness - MeshQuality.avg_skewness(mesh)) < 1e-14
    assert abs(min_skewness - q) < 1e-14

    assert abs(min_radius_ratios - average_radius_ratios) < 1e-14
    assert abs(min_radius_ratios -
               np.min(fenics.MeshQuality.radius_ratio_min_max(mesh))) < 1e-14

    assert abs(min_condition - average_condition) < 1e-14
    assert abs(min_condition - 0.3162277660168379) < 1e-14
#
# You should have received a copy of the GNU General Public License
# along with CASHOCS.  If not, see <https://www.gnu.org/licenses/>.
"""This demo shows how to use different discretizations for state and
adjoint system.

"""

from fenics import *

import cashocs

set_log_level(LogLevel.CRITICAL)
config = cashocs.load_config('config.ini')

mesh, subdomains, boundaries, dx, ds, dS = cashocs.regular_mesh(50)

# Create different spaces for state and adjoint variables
V = FunctionSpace(mesh, 'CG', 1)
W = FunctionSpace(mesh, 'DG', 1)

y = Function(V)
p = Function(W)
u = Function(V)

# Set up a discontinuous Galerkin method (SIPG) (needed for the adjoint system,
# reduces to the classical Galerkin method for CG elements)
n = FacetNormal(mesh)
h = CellDiameter(mesh)
h_avg = (h('+') + h('-')) / 2
flux = 1 / 2 * (inner(grad(u)('+') + grad(u)('-'), n('+')))
def test_input_error():
    with pytest.raises(InputError):
        cashocs.regular_mesh(-1)

    with pytest.raises(CashocsException):
        cashocs.regular_mesh(0)
Ejemplo n.º 8
0
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CASHOCS.  If not, see <https://www.gnu.org/licenses/>.
"""Tests for the utils module.

"""

import fenics
import numpy as np

import cashocs

mesh, _, boundaries, dx, ds, _ = cashocs.regular_mesh(5)
V = fenics.FunctionSpace(mesh, 'CG', 1)


def test_summation():
    a = [1, 2, 3, 4]

    dim = 3
    funcs = []
    test = fenics.TestFunction(V)

    for i in range(dim):
        temp = fenics.Function(V)
        temp.vector()[:] = np.random.rand(V.dim())
        funcs.append(temp)
#
# You should have received a copy of the GNU General Public License
# along with CASHOCS.  If not, see <https://www.gnu.org/licenses/>.
"""Tests for the geometry module.

"""

import os

import fenics
import numpy as np

import cashocs
from cashocs.geometry import MeshQuality

c_mesh, _, _, _, _, _ = cashocs.regular_mesh(5)
u_mesh = fenics.UnitSquareMesh(5, 5)


def test_mesh_import():
    os.system('cashocs-convert ./mesh/mesh.msh ./mesh/mesh.xdmf')
    mesh, subdomains, boundaries, dx, ds, dS = cashocs.import_mesh(
        './mesh/mesh.xdmf')

    gmsh_coords = np.array([[0, 0], [1, 0], [1, 1], [0, 1],
                            [0.499999999998694, 0], [1, 0.499999999998694],
                            [0.5000000000020591, 1], [0, 0.5000000000020591],
                            [0.2500000000010297, 0.7500000000010296],
                            [0.3749999970924328, 0.3750000029075671],
                            [0.7187499979760099, 0.2812500030636815],
                            [0.6542968741702071, 0.6542968818888233]])