コード例 #1
0
def test_load_config():
    config = cashocs.load_config('./test_config.ini')

    assert config.getint('A', 'a') == 1
    assert config.getfloat('A', 'b') == 3.14
    assert config.getboolean('A', 'c') == True
    assert config.get('A', 'd') == 'algorithm'

    assert config.getint('B', 'a') == 2
    assert config.getfloat('B', 'b') == 6.28
    assert config.getboolean('B', 'c') == False
    assert config.get('B', 'd') == 'cashocs'
# 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/>.
"""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)
コード例 #3
0
# 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 if CASHOCS exceptions are being raised properly.

"""

from fenics import *
import cashocs
from cashocs._exceptions import CashocsException, NotConvergedError, InputError, PETScKSPError, ConfigError
import numpy as np
import pytest

config = cashocs.load_config('./config_ocp.ini')
mesh, subdomains, boundaries, dx, ds, dS = cashocs.regular_mesh(6)
V = FunctionSpace(mesh, 'CG', 1)

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

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

y_d = Expression('sin(2*pi*x[0])*sin(2*pi*x[1])', degree=1, domain=mesh)
alpha = 1e-6
J = Constant(0.5) * (y - y_d) * (y - y_d) * dx + Constant(
    0.5 * alpha) * u * u * dx
コード例 #4
0
# 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 Picard iteration.

"""

import numpy as np
from fenics import *

import cashocs

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

mesh, subdomains, boundaries, dx, ds, dS = cashocs.regular_mesh(10)
V = FunctionSpace(mesh, 'CG', 1)

y = Function(V)
z = Function(V)
p = Function(V)
q = Function(V)
states = [y, z]
adjoints = [p, q]

u = Function(V)
v = Function(V)
controls = [u, v]