# 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 numpy as np

import damask

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


def test_grid_read_VTI_init_phi(res_path,tmp_path,copy_files,assert_allclose,np_rng):
    load = 'tension'
    grid = 'cube3x3x3'
    material = 'material'
    job = f'{grid}_{load}'

    g = damask.GeomGrid(np.zeros([3,3,3]), [3,3,3])
    phi_0 = np.ones_like(g.material,dtype=float)
    phi_0.ravel()[phi_0.size//2] = np_rng.random()                                                  # random damage at central voxel
    g.initial_conditions['phi'] = phi_0
    g.save(tmp_path/f'{grid}')

    copy_files(res_path,tmp_path)

    damask.util.run(f'damask_grid -l {load}.yaml -g {grid}.vti -m {material}.yaml -j {job}',
                    wd=tmp_path)

    result = damask.Result(tmp_path/f'{job}.hdf5')
    result_phi_0 = result.place('phi')['increment_0'].data

    assert_allclose(phi_0.ravel(), result_phi_0)
