Commit f7747419 authored by Dion Haefner's avatar Dion Haefner

added pfg tests; reworked test handling; fixed some bugs

parent a82ebb7a
......@@ -42,6 +42,7 @@ class BaseConverter(object):
out_path = self.read_parameter('general','outputFile')
overwrite = text_to_bool(self.read_parameter('general','overwrite'))
millerSimilarity = text_to_bool(self.read_parameter('generator','millerSimilarity'))
self.dim = int(self.read_parameter('generator','dimensions'))
self.data_dict = self.read_data(input_file,millerSimilarity)
self.write(out_path,overwrite)
......@@ -58,6 +59,10 @@ class BaseConverter(object):
if not self.data_dict:
raise RuntimeError("Encountered empty data array (call read_data first)")
for v in self.data_dict.values():
if v.ndim != self.dim:
print(v.shape, self.dim)
raise ValueError("Data does not match field dimensions")
h5_attrs = self._get_attributes()
groupname = self.ParClass.classname
write_parameter_file(groupname, self.data_dict, out_path,
......@@ -87,9 +92,12 @@ class BaseConverter(object):
raise RuntimeError("Missing option {}.{} in parameter file".format(section,key))
def _get_attributes(self):
attr_dict = {
extensions = np.array(self.read_parameter("generator","extensions").split(), dtype=np.float64)
if not extensions.size == self.dim:
raise ValueError("Extensions do not match field dimensions")
attr = {
"millerSimilarity": np.bool(text_to_bool(self.read_parameter('generator','millerSimilarity'))),
"variance": np.float64(self.read_parameter("generator","variance")),
"extensions": np.array(self.read_parameter("generator","extensions").split(), dtype=np.float64)
"extensions": extensions
}
return attr_dict
return attr
......@@ -39,21 +39,25 @@ class H5Converter(BaseConverter):
if "/rawField" in f:
raw_array = np.array(f.get("/rawField"), dtype=np.float64)
else:
raw_array = np.zeros_like(media_array, dtype=np.float64)
raw_array = np.zeros_like(media_array)
# check how many different media we are dealing with
uq_media = np.sort(np.unique(media_array.astype(int)))
if uq_media != range(1,np.max(uq_media) + 1):
if not np.array_equal(uq_media,np.arange(1,np.max(uq_media) + 1)):
raise IOError("Media numbering must be continuous, starting from 1")
p_dict = {i: self.create_parclass(i).parameters() for i in uq_media}
p = [self.create_parclass(i).parameters() for i in uq_media]
# create parameter fields
parField = {"raw_field": raw_array}
for pn in self.ParClass.names:
parField[pn] = np.zeros_like(media_array, dtype=float)
parField[pn] = np.zeros_like(media_array)
for i in uq_media:
mask = np.logical_and(media_array >= i, media_array < i + 1)
parField[pn][mask] = p[i][pn] * (media_array[mask] - i) \
+ p[i+1][pn] * (i + 1 - media_array[mask])
mask = media_array == i
parField[pn][mask] = p[i-1][pn]
mask = np.logical_and(media_array > i, media_array < i + 1)
if np.any(mask):
parField[pn][mask] = p[i-1][pn] * (media_array[mask] - i) \
+ p[i][pn] * (i + 1 - media_array[mask])
return parField
from dorie.utilities._load_all_in_folder import load
"""
Import script to allow for statements of the form
from dorie.testtools.dorie_run import ode
"""
load("dorie.testtools.dorie_create",__file__)
import filecmp
from dorie.testtools.utilities.decorators import evaluation_script
@evaluation_script
def evaluate(iniinfo,runtime):
fileset_1 = iniinfo["_compare.created_files"].split()
fileset_2 = iniinfo["_compare.reference_files"].split()
for f1,f2 in zip(fileset_1,fileset_2):
if not filecmp.cmp(f1,f2):
print("File contents of {} and {} do not match".format(f1,f2))
return False
return True
......@@ -2,7 +2,7 @@ from dorie.utilities._load_all_in_folder import load
"""
Import script to allow for statements of the form
from dorie.testtools.evaluation import ode
from dorie.testtools.dorie_pfg import correlation
"""
load("dorie.testtools.dorie_pfg",__file__)
import os
import h5py
import numpy as np
from dorie.testtools.utilities.decorators import evaluation_script
from dorie.parfield.parameter_file import read_parameter_file
@evaluation_script
def evaluate(iniinfo,runtime):
outfile = iniinfo["general.outputFile"]
try:
reference = iniinfo["_reference.path"] + "/" + iniinfo["general.outputFile"]
except KeyError:
raise ValueError("The reference evaluator assumes the key _reference.path to be existent in the inifile")
if not os.path.isfile(reference):
raise RuntimeError("Reference file {} does not exist".format(reference))
f1_data, f1_attrs = read_parameter_file(outfile)
f2_data, f2_attrs = read_parameter_file(reference)
if f1_data.keys() != f2_data.keys() or f1_attrs.keys() != f2_attrs.keys():
print("Keys do not match")
return False
for key in f1_data.keys():
if not np.array_equal(f1_data[key],f2_data[key]):
print("Attributes do not match")
return False
for key in f1_data.keys():
if not np.array_equal(f1_data[key],f2_data[key]):
print("Data does not match in key {}".format(key))
return False
return True
......@@ -2,7 +2,7 @@ from dorie.utilities._load_all_in_folder import load
"""
Import script to allow for statements of the form
from dorie.testtools.evaluation import ode
from dorie.testtools.dorie_run import ode
"""
load("dorie.testtools.dorie",__file__)
load("dorie.testtools.dorie_run",__file__)
......@@ -3,6 +3,7 @@ import os
from dorie.testtools.utilities.decorators import evaluation_script
from dorie.utilities.vtktools import pvd_reader
from dorie.utilities.vtktools import vtkfile
from dorie.utilities.text_to_bool import text_to_bool
from dorie.utilities import fuzzy_compare_grid
......@@ -14,16 +15,18 @@ def evaluate(iniinfo,runtime):
try:
reference = iniinfo["_reference.path"] + "/" + iniinfo["output.fileName"] + ".vtu"
if os.path.isfile(reference):
vtkfile_run = vtkfile.VTKFile(vtks[-1].path)
vtkfile_run.read()
vtkfile_ref = vtkfile.VTKFile(reference)
vtkfile_ref.read()
# demand equal grids when not using adaptivity
strict_compare = iniinfo["adaptivity.useAdaptivity"].lower() in ["false","0","off"]
ret = fuzzy_compare_grid.compare(vtkfile_run.grid, vtkfile_ref.grid, strict=strict_compare)
else:
raise RuntimeError("Reference file {} does not exist".format(reference))
except KeyError:
raise ValueError("The reference evaluator assumes the key _reference.path to be existent in the inifile")
if os.path.isfile(reference):
vtkfile_run = vtkfile.VTKFile(vtks[-1].path)
vtkfile_run.read()
vtkfile_ref = vtkfile.VTKFile(reference)
vtkfile_ref.read()
# demand equal grids when not using adaptivity
strict_compare = text_to_bool(iniinfo["adaptivity.useAdaptivity"])
ret = fuzzy_compare_grid.compare(vtkfile_run.grid, vtkfile_ref.grid, strict=strict_compare)
else:
raise RuntimeError("Reference file {} does not exist".format(reference))
return ret
......@@ -3,18 +3,32 @@ import os
import subprocess
import inspect
import time
import dorie.testtools.dorie as evaluation
import importlib
def test_dorie(iniinfo,inifile,executable):
"""
Parses the parameter file, calls DORiE, calls the evaluation routines and exits with 0 (pass) or 1 (fail)
Parses the parameter file, calls DORiE, calls the evaluation routines and
exits with 0 (pass) or 1 (fail).
You can specify which command of the dorie wrapper to test with the `_test_command` key (e.g. `run`
for `dorie run`). Options to the dorie wrapper can be specified with `_test_command_options`.
The last argument given to the dorie wrapper is specified with `_test_target` (defaults to the ini file).
"""
command = [executable]
if "__inifile_optionkey" in iniinfo:
command.append(iniinfo["__inifile_optionkey"])
command.append(inifile)
if not "_test_command" in iniinfo:
raise ValueError("Key _test_command has to be given")
test_command = iniinfo["_test_command"]
try:
evaluation = importlib.import_module("dorie.testtools.dorie_" + test_command)
except ImportError:
raise ValueError("Unknown test command " + test_command)
command.append(test_command)
if "_test_command_options" in iniinfo:
command.append(iniinfo["_test_command_options"])
if not "_test_target" in iniinfo:
command.append(inifile)
elif iniinfo["_test_target"] != "None":
command.append(iniinfo["_test_target"])
# Run DORiE and measure total execution time
start = time.clock()
......
import sys
import os
import subprocess
import inspect
import time
import dorie.testtools.dorie_pfg as evaluation
def test_dorie_pfg(iniinfo,inifile,executable):
"""
Parses the parameter file, calls DORiE, calls the evaluation routines and exits with 0 (pass) or 1 (fail)
"""
command = [executable]
if "__inifile_optionkey" in iniinfo:
command.append(iniinfo["__inifile_optionkey"])
command.append(inifile)
# Run DORiE and measure total execution time
start = time.clock()
return_code = subprocess.call(command)
end = time.clock()
runtime = end - start
if return_code != 0 or not "_evaluation" in iniinfo:
return return_code
evaluation_tasks = [x.strip() for x in iniinfo["_evaluation"].split(",")]
evaluation_scripts = inspect.getmembers(evaluation, inspect.ismodule)
return_codes = []
for task in evaluation_tasks:
task_module = None
for script_name, script_module in evaluation_scripts:
if task == script_name and hasattr(script_module,"evaluate"):
if script_module.evaluate._isEvaluator: # check if script is decorated with @evaluation_script
task_module = script_module
if task_module is None:
raise ImportError("Evaluation script for evaluation task {0} was not found"\
.format(task))
print("Running {} evaluator".format(task))
return_code = task_module.evaluate(iniinfo,runtime)
if return_code == 0:
print(" - passed\n")
else:
print(" - failed with code {}\n".format(return_code))
return_codes.append(return_code)
if any(r != 0 for r in return_codes):
print("At least one evaluation task failed!")
print("--- test failed ---")
return 1
else:
print("--- test passed ---")
return 0
......@@ -5,7 +5,7 @@ from setuptools import setup
def dorie_testtools_scripts():
return ['./wrapper/test_dorie.py','./wrapper/test_dorie_pfg.py']
return ['./wrapper/test_dorie.py']
setup(name='dorie.testtools',
version='0.1',
......@@ -14,6 +14,6 @@ setup(name='dorie.testtools',
author='Dion Häfner <mail@dionhaefner.de>',
author_email='dorieteam@iup.uni-heidelberg.de',
url='',
packages=['dorie.testtools','dorie.testtools.dorie', 'dorie.testtools.dorie_pfg', 'dorie.testtools.utilities','dorie.testtools.wrapper'],
packages=['dorie.testtools'],
install_requires=['dune.testtools'],
scripts=dorie_testtools_scripts())
configure_file(test_dorie.py.in ${CMAKE_CURRENT_SOURCE_DIR}/test_dorie.py)
configure_file(test_dorie_pfg.py.in ${CMAKE_CURRENT_SOURCE_DIR}/test_dorie_pfg.py)
#!/usr/bin/env python
from __future__ import absolute_import
import sys
import os
from dune.testtools.wrapper.argumentparser import get_args
from dune.testtools.parser import parse_ini_file
from dorie.testtools.wrapper import test_dorie_pfg
# paths set by cmake
DORIEDIR = "@CMAKE_BINARY_DIR@"
#
DORIE_WRAPPER = os.path.join(DORIEDIR, "bin/dorie")
if __name__ == "__main__":
args = get_args()
iniargument = args["ini"]
iniinfo = parse_ini_file(args["ini"])
sys.exit(test_dorie_pfg.test_dorie_pfg(iniinfo,iniargument,DORIE_WRAPPER))
......@@ -10,7 +10,6 @@ def compare(grid1,grid2,abstol=1E-6,reltol=1E-3,strict=False):
def sort_grid(grid):
new_shape = (grid.cellCenters().shape[0],-1)
print(grid.cellCenters().shape)
cells = grid.cellCenters().reshape(*new_shape)
ix = np.lexsort(cells)
sorted_cells = np.array([c[ix] for c in cells])
......
message(STATUS "Handling system tests")
#configure_file(convergence.mini.in ${CMAKE_CURRENT_LIST_DIR}/convergence.mini)
#dune_add_system_test(TARGET dorie INIFILE convergence.mini SCRIPT test_dorie.py)
configure_file(ode.mini.in ${CMAKE_CURRENT_LIST_DIR}/ode.mini)
dune_add_system_test(TARGET dorie INIFILE ode.mini SCRIPT test_dorie.py)
configure_file(ode_higherorder.mini.in ${CMAKE_CURRENT_LIST_DIR}/ode_higherorder.mini)
dune_add_system_test(TARGET dorie INIFILE ode_higherorder.mini SCRIPT test_dorie.py)
configure_file(parfield_muphi.mini.in ${CMAKE_CURRENT_LIST_DIR}/parfield_muphi.mini)
dune_add_system_test(TARGET dorie-pfg INIFILE parfield_muphi.mini SCRIPT test_dorie_pfg.py)
configure_file(muphi.mini.in ${CMAKE_CURRENT_LIST_DIR}/muphi.mini)
dune_add_system_test(TARGET dorie INIFILE muphi.mini SCRIPT test_dorie.py)
set_property(TEST dorie_muphi APPEND PROPERTY DEPENDS dorie-pfg_muphi_pfg)
configure_file(reference_2d.mini.in ${CMAKE_CURRENT_LIST_DIR}/reference_2d.mini)
dune_add_system_test(TARGET dorie INIFILE reference_2d.mini SCRIPT test_dorie.py)
configure_file(reference_3d.mini.in ${CMAKE_CURRENT_LIST_DIR}/reference_3d.mini)
dune_add_system_test(TARGET dorie INIFILE reference_3d.mini SCRIPT test_dorie.py)
configure_file(parfield.mini.in ${CMAKE_CURRENT_LIST_DIR}/parfield.mini)
dune_add_system_test(TARGET dorie-pfg INIFILE parfield.mini SCRIPT test_dorie_pfg.py)
configure_file(correlation.mini.in ${CMAKE_CURRENT_LIST_DIR}/correlation.mini)
dune_add_system_test(TARGET dorie-pfg INIFILE correlation.mini SCRIPT test_dorie_pfg.py)
function(dorie_add_system_test target metaini)
configure_file(${metaini}.in ${CMAKE_CURRENT_LIST_DIR}/${metaini})
dune_add_system_test(TARGET ${target} INIFILE ${metaini} SCRIPT test_dorie.py)
endfunction()
function(dorie_add_system_test_dependency test1 test2)
set_tests_properties(${test1} PROPERTIES DEPENDS ${test2})
endfunction()
# dorie run
dorie_add_system_test(dorie ode.mini)
dorie_add_system_test(dorie ode_higherorder.mini)
dorie_add_system_test(dorie muphi.mini)
dorie_add_system_test_dependency(dorie_muphi dorie-pfg_muphi_pfg)
dorie_add_system_test(dorie reference_2d.mini)
dorie_add_system_test(dorie reference_3d.mini)
# dorie pfg
dorie_add_system_test(dorie-pfg parfield.mini)
dorie_add_system_test(dorie-pfg parfield_muphi.mini)
#dorie_add_system_test(dorie-pfg correlation.mini)
# dorie plot
dorie_add_system_test(dorie plot.mini)
dorie_add_system_test_dependency(dorie_plot dorie_reference_2d_0000)
# dorie create
dorie_add_system_test(dorie create.mini)
include ${CMAKE_BINARY_DIR}/doc/default_files/config.ini
__name = convergence
__inifile_optionkey = run
_asset_path = "${CMAKE_CURRENT_LIST_DIR}"
_evaluation = convergence
_convergence.minTime = 5E5
output.fileName = convergence | unique
output.outputPath = convergence | unique
output.verbose = 0
time.end = 1E6
time.maxTimestep = 1E5
time.startTimestep = 1E4
adaptivity.threshold = 1E-10
adaptivity.useAdaptivity = true
adaptivity.refinementFraction = .05
grid.initialLevel = 0
grid.gridType = gmsh, rectangular | expand
grid.cells = 20 20
grid.gridFile = "{_asset_path}/meshes/mesh.msh"
boundary.file = "{_asset_path}/bcs/bcFile2d.dat"
parameters.arrayFile = "{_asset_path}/parfields/fftfield.h5", "{_asset_path}/parfields/fftfield-miller.h5" | expand
include ${CMAKE_BINARY_DIR}/doc/default_files/parfield.ini
__name = correlation
__inifile_optionkey = pfg
_test_command = pfg
_asset_path = "${CMAKE_CURRENT_LIST_DIR}"
_evaluation = correlation
......
__name = create
_test_command = create
_test_command_options = -f
_test_target = None
_evaluation = compare
__refpath = ${CMAKE_BINARY_DIR}/doc/default_files
[_compare]
created_files = 2d_infiltr.bcdat 3d_infiltr.bcdat config.ini parfield.ini
reference_files = {__refpath}/2d_infiltr.bcdat {__refpath}/3d_infiltr.bcdat {__refpath}/config.ini {__refpath}/parfield.ini
include ${CMAKE_BINARY_DIR}/doc/default_files/config.ini
__name = muphi
__inifile_optionkey = run
_test_command = run
_asset_path = "${CMAKE_CURRENT_LIST_DIR}"
_evaluation = muphi
_reference.path = {_asset_path}/references/muphi/
......
include ${CMAKE_BINARY_DIR}/doc/default_files/config.ini
__name = ode
__inifile_optionkey = run
_test_command = run
_asset_path = "${CMAKE_CURRENT_LIST_DIR}"
_evaluation = ode
......
include ${CMAKE_BINARY_DIR}/doc/default_files/config.ini
__name = ode_higherorder
__inifile_optionkey = run
_test_command = run
_asset_path = "${CMAKE_CURRENT_LIST_DIR}"
_evaluation = ode
......
include ${CMAKE_BINARY_DIR}/doc/default_files/parfield.ini
__name = muphi_pfg
__inifile_optionkey = pfg
__name = pfg
_test_command = pfg
_asset_path = "${CMAKE_CURRENT_LIST_DIR}"
_data_path = {_asset_path}/parfield-data
_evaluation = reference
[_reference]
path = {_asset_path}/references/parfield
[general]
generator = fft
outputFile = fft_field_muphi.h5
generator = fft, image, csv, hdf5 | expand input_type
inputFile = UNDEFINED, {_data_path}/test.png, {_data_path}/test.csv, {_data_path}/test.h5 | expand input_type
outputFile = fft.h5, image.h5, csv.h5, h5.h5 | expand input_type
parameterization = vanGenuchten
overwrite = true
# fix some parameters so we make sure they are the same as in the muphi run
[parameters.vanGenuchten.1]
thetaR = 0.03
thetaS = 0.32
alpha = -2.3
tau = -1.1
n = 4.17
k0 = 2.2e-5
[parameters.vanGenuchten.2]
thetaR = 0.01
thetaS = 0.41
alpha = -0.7
tau = 0
n = 1.3
k0 = 1e-5
[generator]
dimensions = 2, 2, 2, 3 | expand input_type
extensions = 1 1, 1 1, 1 1, 1 1 1 | expand input_type
millerSimilarity = false
variance = 0.2
extensions = 1 1
[generator.fft]
N = 1000 1000
correlationLengths = .4 .05
seed = 223141598
beta = 0
embeddingFactor = .01
variogramModel = gaussian
newEV = true
N = 50 50
newField = true
newEV = true
include ${CMAKE_BINARY_DIR}/doc/default_files/parfield.ini
__name = muphi_pfg
__inifile_optionkey = pfg
_test_command = pfg
[general]
generator = fft
......
__name = plot
_test_command = plot
_test_target = reference_2d_0000.pvd
include ${CMAKE_BINARY_DIR}/doc/default_files/config.ini
__name = reference_2d
__inifile_optionkey = run
_test_command = run
_asset_path = "${CMAKE_CURRENT_LIST_DIR}"
_evaluation = reference
_reference.path = {_asset_path}/references/2d/
......
include ${CMAKE_BINARY_DIR}/doc/default_files/config.ini
__name = reference_3d
__inifile_optionkey = run
_test_command = run
_asset_path = "${CMAKE_CURRENT_LIST_DIR}"
_evaluation = reference
_reference.path = {_asset_path}/references/3d/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment