# Vibroacoustics
#
# E.Rohan, V.Lukeš
# Homogenization of the vibro–acoustic transmission on periodically
# perforated elastic plates with arrays of resonators.
# https://arxiv.org/abs/2104.01367 (arXiv:2104.01367v1)

import os.path as op
import numpy as nm
from collections.abc import Iterable
from scipy.io import savemat, loadmat
from sfepy.base.base import output, debug, Struct
from sfepy import data_dir
from sfepy.discrete.fem.periodic import match_y_plane, match_x_plane
from acoustics_macro_utils import eval_phi, post_process,\
    generate_plate_mesh, get_region_entities
from sfepy.discrete.projections import project_by_component
from sfepy.discrete.fem import Mesh, FEDomain

wdir = op.dirname(__file__)


def post_process_macro(out, pb, state, extend=False):
    pbvars = pb.get_variables()

    n1, ng1, c1, cg1, ds1, nmap1 = get_region_entities(pbvars['p1'])
    noff = n1.shape[0]
    n2, ng2, c2, cg2, _, nmap2 = get_region_entities(pbvars['p2'], noff=noff)
    nend = nm.max(c2) + 1
    nmap = nm.hstack([nmap1, nmap2])
    n1[:, 2] += pb.conf.eps0 * 0.5
    n2[:, 2] -= pb.conf.eps0 * 0.5

    mesh2 = Mesh.from_data('m2', nm.vstack([n1, n2]), nm.hstack([ng1, ng2]),
                           [nm.vstack([c1, c2])], [nm.hstack([cg1, cg2])],
                           [ds1])

    oname = op.join(pb.output_dir, pb.ofn_trunk + '_p.vtk')
    out2 = {}

    for ir in ['real.', 'imag.']:
        pdata = nm.zeros((nmap.shape[0], 1), dtype=nm.float64)
        for v, idxs in [('p1', slice(0, noff)), ('p2', slice(noff, nend))]:
            pdata[idxs, :] = out[ir + v].data

        out2[ir + 'p'] = 
Struct(name='p', mode='vertex', data=pdata)