File size: 2,594 Bytes
8f59bb3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | """Output error method test module."""
import functools
import numpy as np
import pytest
import sympy
import sym2num.model
import sym2num.var
from ceacoest import oem
from ceacoest.modelling import symoem
from .test_optim import (test_merit_gradient, test_merit_hessian,
test_constraint_jacobian, test_constraint_hessian,
seed, dec)
from ceacoest.testsupport.array_cmp import ArrayDiff
@pytest.fixture(scope='module', params=[2, 4], ids=lambda i: f'{i}ord-col')
def collocation_order(request):
"""Order of collocation method."""
return request.param
@pytest.fixture(scope='module')
def model(collocation_order):
"""Output error method collocation model."""
@symoem.collocate(order=collocation_order)
class SymbolicModel:
"""Symbolic output error method test model."""
@property
@functools.lru_cache()
def variables(self):
"""Model variables definition."""
var_list = [
sym2num.var.SymbolArray('x', ['x1', 'x2', 'x3']),
sym2num.var.SymbolArray('u', ['u1']),
sym2num.var.SymbolArray('y', ['x1_meas', 'x2_meas']),
sym2num.var.SymbolArray('p', ['p1', 'p2']),
]
return sym2num.var.make_dict(var_list)
@sym2num.model.collect_symbols
def f(self, x, u, p, *, s):
"""ODE function."""
return sympy.Array(
[s.x1 * sympy.cos(s.x2),
s.u1 * s.p1 * s.x2**2,
s.u1**2 * s.x1**3 * sympy.exp(s.p1)]
)
@sym2num.model.collect_symbols
def L(self, y, x, u, p, *, s):
"""Lagrange (running) cost."""
L = (s.x1 ** 2 + s.p2**4 + s.p1 * (s.x1 + 2)
+ (s.p2 + s.u1 + s.u1 + s.x1) ** 2 ) + s.x1_meas
return sympy.Array(L)
symb_mdl = SymbolicModel()
GeneratedModel = sym2num.model.compile_class(symb_mdl)
return GeneratedModel()
@pytest.fixture(params=[1, 2, 4], ids=lambda i: f'{i}piece')
def npieces(request):
"""Number of collocation pieces."""
return request.param
@pytest.fixture
def y(model, seed, npieces):
"""OEM measurements."""
return np.random.randn(npieces + 1, model.ny)
@pytest.fixture
def u():
"""OEM input function."""
return lambda t: np.cos(t)[:, None]
@pytest.fixture
def problem(model, npieces, y, u):
"""Output error method problem."""
t = np.linspace(0, 1, npieces + 1)
return oem.Problem(model, t, y, u)
|