# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright 2024-2025 Max-Planck-Institut für Nachhaltige Materialien GmbH
# Copyright 2011-2024 Max-Planck-Institut für Eisenforschung GmbH
#
# DAMASK is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
import pytest

import damask

@pytest.fixture
def res_path(res_path_base):
    """Directory containing testing resources."""
    return res_path_base/'grid_displacements'


def test_grid_displacement(res_path,tmp_path,copy_files,assert_allclose,np_rng):

    copy_files(res_path,tmp_path)
    damask.ConfigMaterial\
        .load(tmp_path/'material.yaml') \
        .material_add(O=damask.Rotation.from_random(2,rng_seed=np_rng),phase='IF',homogenization='SX') \
        .save(tmp_path/'material.yaml')
    size = np_rng.random(3)+1.0
    cells = np_rng.integers(5,12,(3))
    damask.GeomGrid(np_rng.integers(0,2,cells),size).save(tmp_path/'random_2phase.vti')

    damask.util.run(f'damask_grid -l mixed.yaml -g random_2phase.vti -m material.yaml -w {tmp_path} '\
                     '--jobname random_2phase_mixed')

    r = damask.Result(f'{tmp_path}/random_2phase_mixed.hdf5').view(increments=-1)
    F = r.place('F').reshape(tuple(r.cells)+(3,3),order='F')
    u = r.place('u_p').reshape(tuple(r.cells)+(3,),order='F')

    assert_allclose(u,damask.grid_filters.displacement_point(r.size,F))
