Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F85870976
test_dumper.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Wed, Oct 2, 16:26
Size
4 KB
Mime Type
text/x-python
Expires
Fri, Oct 4, 16:26 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
21269489
Attached To
rTAMAAS tamaas
test_dumper.py
View Options
# -*- coding: utf-8 -*-
# @file
# @section LICENSE
#
# Copyright (©) 2016-2020 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
shutil
import
tamaas
as
tm
import
numpy
as
np
import
warnings
warnings
.
filterwarnings
(
'ignore'
)
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'
,
'netcdf'
]:
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
ref_t
=
model
.
getTraction
()
ref_d
=
model
.
getDisplacement
()
tractions
=
np
.
loadtxt
(
'tractions.txt'
)
displacements
=
np
.
loadtxt
(
'displacement.txt'
)
assert
np
.
all
(
tractions
.
reshape
(
ref_t
.
shape
)
==
ref_t
)
assert
np
.
all
(
displacements
.
reshape
(
ref_d
.
shape
)
==
ref_d
)
with
np
.
load
(
'numpys/test_dump_0000.npz'
,
allow_pickle
=
True
)
as
npfile
:
tractions
=
npfile
[
'traction'
]
displacements
=
npfile
[
'displacement'
]
attributes
=
npfile
[
'attrs'
]
.
item
()
assert
np
.
all
(
tractions
==
ref_t
)
assert
np
.
all
(
displacements
==
ref_d
)
assert
str
(
model
.
type
)
==
attributes
[
'model_type'
]
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'
],
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
()
cleanup
()
except
ImportError
:
pass
try
:
from
tamaas.dumpers
import
NetCDFDumper
import
netCDF4
def
test_netcdfdumper
(
tamaas_fixture
):
model
=
tm
.
ModelFactory
.
createModel
(
tm
.
model_type
.
volume_2d
,
[
1.
,
1.
,
1.
],
[
16
,
4
,
8
])
model
.
getDisplacement
()[
...
]
=
3.1415
dumper
=
NetCDFDumper
(
'test_netcdf'
,
'traction'
,
'displacement'
)
dumper
<<
model
assert
os
.
path
.
isfile
(
'netcdf/test_netcdf_0000.nc'
)
fh
=
netCDF4
.
Dataset
(
'netcdf/test_netcdf_0000.nc'
,
'r'
)
disp
=
fh
[
'displacement'
][:]
assert
np
.
all
(
np
.
abs
(
disp
-
3.1415
)
<
1e-15
)
assert
disp
.
shape
==
(
16
,
4
,
8
,
3
)
fh
.
close
()
cleanup
()
except
ImportError
:
pass
Event Timeline
Log In to Comment