Commit 4aef2182 authored by Patrick Buchfink's avatar Patrick Buchfink Committed by Stephan Rave
Browse files

Move test to model.py

parent b7415e17
......@@ -3,12 +3,18 @@
# License: BSD 2-Clause License (https://opensource.org/licenses/BSD-2-Clause)
import numpy as np
import pytest
from pymor.algorithms.basic import almost_equal
from pymor.algorithms.timestepping import ImplicitMidpointTimeStepper
from pymor.analyticalproblems.functions import ExpressionFunction, ConstantFunction
from pymor.analyticalproblems.thermalblock import thermal_block_problem
from pymor.discretizers.builtin import discretize_stationary_cg
from pymor.core.pickle import dumps, loads
from pymor.models.symplectic import QuadraticHamiltonianModel
from pymor.operators.block import BlockDiagonalOperator
from pymor.operators.constructions import IdentityOperator
from pymor.vectorarrays.numpy import NumpyVectorSpace
from pymortests.base import runmodule
from pymortests.pickling import assert_picklable, assert_picklable_without_dumps_function
......@@ -49,5 +55,30 @@ def test_StationaryModel_deaffinize():
assert np.allclose(m.output(mu), m_deaff.output(mu))
@pytest.mark.parametrize('block_phase_space', (False, True))
def test_quadratic_hamiltonian_model(block_phase_space):
"""Check QuadraticHamiltonianModel with implicit midpoint rule."""
if block_phase_space:
H_op = BlockDiagonalOperator([IdentityOperator(NumpyVectorSpace(1))] * 2)
else:
phase_space = NumpyVectorSpace(2)
H_op = IdentityOperator(phase_space)
model = QuadraticHamiltonianModel(
3,
H_op.source.ones(),
H_op,
time_stepper=ImplicitMidpointTimeStepper(3),
name='test_mass_spring_model'
)
res = model.solve()
ham = model.eval_hamiltonian(res).to_numpy()
# check preservation of the Hamiltonian
assert np.allclose(ham, ham[0])
if __name__ == "__main__":
runmodule(filename=__file__)
# This file is part of the pyMOR project (https://www.pymor.org).
# Copyright pyMOR developers and contributors. All rights reserved.
# License: BSD 2-Clause License (https://opensource.org/licenses/BSD-2-Clause)
import numpy as np
import pytest
from pymor.algorithms.timestepping import ImplicitMidpointTimeStepper
from pymor.models.symplectic import QuadraticHamiltonianModel
from pymor.operators.block import BlockDiagonalOperator
from pymor.operators.constructions import IdentityOperator
from pymor.vectorarrays.numpy import NumpyVectorSpace
@pytest.mark.parametrize('block_phase_space', (False, True))
def test_quadratic_hamiltonian_model(block_phase_space):
"""Check QuadraticHamiltonianModel with implicit midpoint rule."""
if block_phase_space:
H_op = BlockDiagonalOperator([IdentityOperator(NumpyVectorSpace(1))] * 2)
else:
phase_space = NumpyVectorSpace(2)
H_op = IdentityOperator(phase_space)
model = QuadraticHamiltonianModel(
3,
H_op.source.ones(),
H_op,
time_stepper=ImplicitMidpointTimeStepper(3),
name='test_mass_spring_model'
)
res = model.solve()
ham = model.eval_hamiltonian(res).to_numpy()
# check preservation of the Hamiltonian
assert np.allclose(ham, ham[0])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment