Page MenuHomec4science

test_dumper.py
No OneTemporary

File Metadata

Created
Sat, May 11, 13:49

test_dumper.py

# -*- coding: utf-8 -*-
# @file
# @section LICENSE
#
# Copyright (©) 2016-19 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 print_function, division
import tamaas as tm
import numpy as np
import os
import shutil
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.getField('traction')))
np.savetxt('displacement.txt', np.ravel(model.getField('displacement')))
def cleanup():
for name in ['tractions.txt',
'displacement.txt',
'numpys',
'hdf5']:
if os.path.exists(name) and os.path.isdir(name):
shutil.rmtree(name)
elif os.path.exists(name):
os.remove(name)
def test_dumpers(tamaas_fixture):
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 tractions.size == model.getTraction().size
assert displacements.size == model.getDisplacement().size
with np.load('numpys/test_dump_0000.npz') as npfile:
tractions = npfile['traction']
displacements = npfile['displacement']
t_shape = list(model.getTraction().shape)
t_shape[0] += 1
t_shape[1] += 1
d_shape = list(model.getDisplacement().shape)
d_shape[1] += 1
d_shape[2] += 1
assert tractions.shape == tuple(t_shape)
assert displacements.shape == tuple(d_shape)
assert os.path.isfile('numpys/test_dump_0001.npz')
cleanup()
# Protecting test
try:
from tamaas.dumpers import H5Dumper
import h5py
def test_h5dumper(tamaas_fixture):
model = tm.ModelFactory.createModel(tm.model_type.volume_2d,
[1., 1., 1.],
[16, 4, 8])
model.getDisplacement()[...] = 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'])
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)
cleanup()
except ImportError:
pass

Event Timeline