Page MenuHomec4science

test_dumper.py
No OneTemporary

File Metadata

Created
Mon, May 13, 16:47

test_dumper.py

# -*- coding: utf-8 -*-
# @file
# LICENSE
#
# Copyright (©) 2016-2021 EPFL (École Polytechnique Fédérale de Lausanne),
# Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
#
# This program 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/>.
from __future__ import division
import os
import tamaas as tm
import numpy as np
import pytest
from tamaas.dumpers import NumpyDumper
class Dumper(tm.ModelDumper):
"""Simple numpy dumper"""
def __init__(self):
tm.ModelDumper.__init__(self)
def dump(self, model):
np.savetxt('tractions.txt', np.ravel(model.traction))
np.savetxt('displacement.txt', np.ravel(model.displacement))
def test_dumpers(tamaas_fixture, tmp_path):
os.chdir(tmp_path)
model = tm.ModelFactory.createModel(tm.model_type.volume_2d, [1., 1., 1.],
[16, 4, 8])
dumper = Dumper()
np_dumper = NumpyDumper('test_dump', 'traction', 'displacement')
model.addDumper(np_dumper)
model.dump()
model.dump()
dumper << model
tractions = np.loadtxt('tractions.txt')
displacements = np.loadtxt('displacement.txt')
assert np.all(tractions.reshape(model.traction.shape) == model.traction)
assert np.all(displacements.reshape(model.displacement.shape) == model.displacement)
rmodel = np_dumper.read('numpys/test_dump_0000.npz')
assert np.all(model.traction == rmodel.traction)
assert np.all(model.displacement == rmodel.displacement)
assert model.type == rmodel.type
assert os.path.isfile('numpys/test_dump_0001.npz')
# Protecting test
try:
from tamaas.dumpers import H5Dumper
import h5py
def test_h5dumper(tamaas_fixture, tmp_path):
os.chdir(tmp_path)
model = tm.ModelFactory.createModel(tm.model_type.volume_2d,
[1., 1., 1.],
[16, 4, 8])
model['displacement'][...] = 3.1415
dumper = H5Dumper('test_hdf5', 'traction', 'displacement')
dumper << model
assert os.path.isfile('hdf5/test_hdf5_0000.h5')
fh = h5py.File('hdf5/test_hdf5_0000.h5', 'r')
disp = np.array(fh['displacement'], dtype=tm.dtype)
assert np.all(np.abs(disp - 3.1415) < 1e-15)
assert list(fh.attrs['discretization']) == [16, 4, 8]
assert fh.attrs['model_type'] == str(model.type)
fh.close()
with pytest.raises(RuntimeError):
dumper.read('')
except ImportError:
pass
try:
from tamaas.dumpers import NetCDFDumper
from netCDF4 import Dataset
def test_netcdfdumper(tamaas_fixture, tmp_path):
os.chdir(tmp_path)
model = tm.ModelFactory.createModel(tm.model_type.volume_2d,
[1., 1., 1.],
[16, 4, 8])
model['displacement'][...] = 3.1415
dumper = NetCDFDumper('test_netcdf', 'traction', 'displacement')
# Dumping two frames
dumper << model
dumper << model
with pytest.raises(RuntimeError):
dumper.read('')
assert os.path.isfile('netcdf/test_netcdf.nc')
with Dataset('netcdf/test_netcdf.nc', 'r') as netcdf_file:
disp = netcdf_file['displacement'][:]
assert np.all(np.abs(disp - 3.1415) < 1e-15)
assert disp.shape[1:] == (16, 4, 8, 3)
assert disp.shape[0] == 2
with pytest.raises(Exception):
model = tm.ModelFactory.createModel(tm.model_type.volume_2d,
[1., 1., 1.],
[15, 4, 8])
dumper << model
except ImportError:
pass

Event Timeline