FEM: unit tests, get rid of duplicate defs

This commit is contained in:
Bernd Hahnebach 2017-01-22 15:04:04 +01:00
parent b8403d4645
commit 529e1418f4

View File

@ -143,52 +143,6 @@ class FemTest(unittest.TestCase):
def save_file(self, fc_file_name):
self.active_doc.saveAs(fc_file_name)
def force_unix_line_ends(self, line_list):
new_line_list = []
for l in line_list:
if l.endswith("\r\n"):
l = l[:-2] + '\n'
new_line_list.append(l)
return new_line_list
def compare_inp_files(self, file_name1, file_name2):
file1 = open(file_name1, 'r')
f1 = file1.readlines()
file1.close()
lf1 = [l for l in f1 if not (l.startswith('** written ') or l.startswith('** file '))]
lf1 = self.force_unix_line_ends(lf1)
file2 = open(file_name2, 'r')
f2 = file2.readlines()
file2.close()
lf2 = [l for l in f2 if not (l.startswith('** written ') or l.startswith('** file '))]
lf2 = self.force_unix_line_ends(lf2)
import difflib
diff = difflib.unified_diff(lf1, lf2, n=0)
result = ''
for l in diff:
result += l
if result:
result = "Comparing {} to {} failed!\n".format(file_name1, file_name2) + result
return result
def compare_stats(self, fea, stat_file=None):
if stat_file:
sf = open(stat_file, 'r')
sf_content = sf.readlines()
sf.close()
sf_content = self.force_unix_line_ends(sf_content)
stat_types = ["U1", "U2", "U3", "Uabs", "Sabs"]
stats = []
for s in stat_types:
stats.append("{}: {}\n".format(s, fea.get_stats(s)))
if sf_content != stats:
fcc_print("Expected stats from {}".format(stat_file))
fcc_print(sf_content)
fcc_print("Stats read from {}.frd file".format(fea.base_name))
fcc_print(stats)
return True
return False
def test_new_analysis(self):
# static
fcc_print('--------------- Start of FEM tests ---------------')
@ -247,7 +201,7 @@ class FemTest(unittest.TestCase):
self.assertFalse(error, "Writing failed")
fcc_print('Comparing {} to {}/{}.inp'.format(static_analysis_inp_file, static_analysis_dir, mesh_name))
ret = self.compare_inp_files(static_analysis_inp_file, static_analysis_dir + "/" + mesh_name + '.inp')
ret = compare_inp_files(static_analysis_inp_file, static_analysis_dir + "/" + mesh_name + '.inp')
self.assertFalse(ret, "FemToolsCcx write_inp_file test failed.\n{}".format(ret))
fcc_print('Setting up working directory to {} in order to read simulated calculations'.format(test_file_dir))
@ -270,7 +224,7 @@ class FemTest(unittest.TestCase):
self.assertTrue(fea.results_present, "Cannot read results from {}.frd frd file".format(fea.base_name))
fcc_print('Reading stats from result object for static analysis...')
ret = self.compare_stats(fea, static_expected_values)
ret = compare_stats(fea, static_expected_values)
self.assertFalse(ret, "Invalid results read from .frd file")
fcc_print('Save FreeCAD file for static analysis to {}...'.format(static_save_fc_file))
@ -296,7 +250,7 @@ class FemTest(unittest.TestCase):
self.assertFalse(error, "Writing failed")
fcc_print('Comparing {} to {}/{}.inp'.format(frequency_analysis_inp_file, frequency_analysis_dir, mesh_name))
ret = self.compare_inp_files(frequency_analysis_inp_file, frequency_analysis_dir + "/" + mesh_name + '.inp')
ret = compare_inp_files(frequency_analysis_inp_file, frequency_analysis_dir + "/" + mesh_name + '.inp')
self.assertFalse(ret, "FemToolsCcx write_inp_file test failed.\n{}".format(ret))
fcc_print('Setting up working directory to {} in order to read simulated calculations'.format(test_file_dir))
@ -319,7 +273,7 @@ class FemTest(unittest.TestCase):
self.assertTrue(fea.results_present, "Cannot read results from {}.frd frd file".format(fea.base_name))
fcc_print('Reading stats from result object for frequency analysis...')
ret = self.compare_stats(fea, frequency_expected_values)
ret = compare_stats(fea, frequency_expected_values)
self.assertFalse(ret, "Invalid results read from .frd file")
fcc_print('Save FreeCAD file for frequency analysis to {}...'.format(frequency_save_fc_file))
@ -415,52 +369,6 @@ class TherMechFemTest(unittest.TestCase):
def save_file(self, fc_file_name):
self.active_doc.saveAs(fc_file_name)
def force_unix_line_ends(self, line_list):
new_line_list = []
for l in line_list:
if l.endswith("\r\n"):
l = l[:-2] + '\n'
new_line_list.append(l)
return new_line_list
def compare_inp_files(self, file_name1, file_name2):
file1 = open(file_name1, 'r')
f1 = file1.readlines()
file1.close()
lf1 = [l for l in f1 if not l.startswith('** written ') if not l.startswith('** file ')]
lf1 = self.force_unix_line_ends(lf1)
file2 = open(file_name2, 'r')
f2 = file2.readlines()
file2.close()
lf2 = [l for l in f2 if not l.startswith('** written ') if not l.startswith('** file ')]
lf2 = self.force_unix_line_ends(lf2)
import difflib
diff = difflib.unified_diff(lf1, lf2, n=0)
result = ''
for l in diff:
result += l
if result:
result = "Comparing {} to {} failed!\n".format(file_name1, file_name2) + result
return result
def compare_stats(self, fea, stat_file=None):
if stat_file:
sf = open(stat_file, 'r')
sf_content = sf.readlines()
sf.close()
sf_content = self.force_unix_line_ends(sf_content)
stat_types = ["U1", "U2", "U3", "Uabs", "Sabs"]
stats = []
for s in stat_types:
stats.append("{}: {}\n".format(s, fea.get_stats(s)))
if sf_content != stats:
fcc_print("Expected stats from {}".format(stat_file))
fcc_print(sf_content)
fcc_print("Stats read from {}.frd file".format(fea.base_name))
fcc_print(stats)
return True
return False
def test_new_analysis(self):
fcc_print('--------------- Start of FEM tests ---------------')
fcc_print('Checking FEM new analysis...')
@ -523,7 +431,7 @@ class TherMechFemTest(unittest.TestCase):
self.assertFalse(error, "Writing failed")
fcc_print('Comparing {} to {}/{}.inp'.format(thermomech_analysis_inp_file, thermomech_analysis_dir, mesh_name))
ret = self.compare_inp_files(thermomech_analysis_inp_file, thermomech_analysis_dir + "/" + mesh_name + '.inp')
ret = compare_inp_files(thermomech_analysis_inp_file, thermomech_analysis_dir + "/" + mesh_name + '.inp')
self.assertFalse(ret, "FemToolsCcx write_inp_file test failed.\n{}".format(ret))
fcc_print('Setting up working directory to {} in order to read simulated calculations'.format(test_file_dir))
@ -546,7 +454,7 @@ class TherMechFemTest(unittest.TestCase):
self.assertTrue(fea.results_present, "Cannot read results from {}.frd frd file".format(fea.base_name))
fcc_print('Reading stats from result object for thermomech analysis...')
ret = self.compare_stats(fea, thermomech_expected_values)
ret = compare_stats(fea, thermomech_expected_values)
self.assertFalse(ret, "Invalid results read from .frd file")
fcc_print('Save FreeCAD file for thermomech analysis to {}...'.format(thermomech_save_fc_file))
@ -561,6 +469,55 @@ class TherMechFemTest(unittest.TestCase):
# helpers
def compare_inp_files(file_name1, file_name2):
file1 = open(file_name1, 'r')
f1 = file1.readlines()
file1.close()
lf1 = [l for l in f1 if not (l.startswith('** written ') or l.startswith('** file '))]
lf1 = force_unix_line_ends(lf1)
file2 = open(file_name2, 'r')
f2 = file2.readlines()
file2.close()
lf2 = [l for l in f2 if not (l.startswith('** written ') or l.startswith('** file '))]
lf2 = force_unix_line_ends(lf2)
import difflib
diff = difflib.unified_diff(lf1, lf2, n=0)
result = ''
for l in diff:
result += l
if result:
result = "Comparing {} to {} failed!\n".format(file_name1, file_name2) + result
return result
def compare_stats(fea, stat_file=None):
if stat_file:
sf = open(stat_file, 'r')
sf_content = sf.readlines()
sf.close()
sf_content = force_unix_line_ends(sf_content)
stat_types = ["U1", "U2", "U3", "Uabs", "Sabs"]
stats = []
for s in stat_types:
stats.append("{}: {}\n".format(s, fea.get_stats(s)))
if sf_content != stats:
fcc_print("Expected stats from {}".format(stat_file))
fcc_print(sf_content)
fcc_print("Stats read from {}.frd file".format(fea.base_name))
fcc_print(stats)
return True
return False
def force_unix_line_ends(line_list):
new_line_list = []
for l in line_list:
if l.endswith("\r\n"):
l = l[:-2] + '\n'
new_line_list.append(l)
return new_line_list
def run_fem_unittests():
import unittest
suite = unittest.TestSuite()