From 5e3dc0c2c984edfde8fb19d680ac251b2fa77a74 Mon Sep 17 00:00:00 2001 From: Przemo Firszt Date: Tue, 4 Aug 2015 20:32:53 +0100 Subject: [PATCH] Tests, FEM: Fix inp file writing test It's a compare line-by-line without comment lines now instead of previously used md5 sum comparison. inp pre filtering method suggested by Far-Zer File location fixes by wmayer Signed-off-by: Przemo Firszt --- src/Mod/Fem/App/CMakeLists.txt | 1 + src/Mod/Fem/CMakeLists.txt | 4 +++ src/Mod/Fem/TestFem.py | 30 +++++++++++++------ src/Mod/Fem/ccxInpWriter.py | 9 ++++++ .../Fem/{test_file.inp => inp_standard.inp} | 11 ++++++- 5 files changed, 45 insertions(+), 10 deletions(-) rename src/Mod/Fem/{test_file.inp => inp_standard.inp} (98%) diff --git a/src/Mod/Fem/App/CMakeLists.txt b/src/Mod/Fem/App/CMakeLists.txt index 37c8df3eb..b78e835d8 100755 --- a/src/Mod/Fem/App/CMakeLists.txt +++ b/src/Mod/Fem/App/CMakeLists.txt @@ -75,6 +75,7 @@ SET(FemScripts_SRCS FemTools.py mesh_points.csv mesh_volumes.csv + inp_standard.inp MechanicalAnalysis.ui MechanicalAnalysis.py MechanicalMaterial.ui diff --git a/src/Mod/Fem/CMakeLists.txt b/src/Mod/Fem/CMakeLists.txt index 241616cd8..f4e1f749d 100755 --- a/src/Mod/Fem/CMakeLists.txt +++ b/src/Mod/Fem/CMakeLists.txt @@ -13,6 +13,10 @@ INSTALL( ccxFrdReader.py ccxInpWriter.py FemTools.py + TestFem.py + mesh_points.csv + mesh_volumes.csv + inp_standard.inp FemExample.py MechanicalAnalysis.py MechanicalMaterial.py diff --git a/src/Mod/Fem/TestFem.py b/src/Mod/Fem/TestFem.py index 271314e2b..f1674aa01 100644 --- a/src/Mod/Fem/TestFem.py +++ b/src/Mod/Fem/TestFem.py @@ -29,17 +29,12 @@ import FemTools import FreeCAD import MechanicalAnalysis import csv -import hashlib import tempfile import unittest -# md5sum of src/Mod/Fem/test_file.inp -# All changes in ccxInpWriter resulting in changes of the .inp file should -# be reflected in src/Mod/Fem/test_file.inp and the m5d_standard variable -# should be updated -md5_standard = "55b91f57fdc13ec471689e63f9529d38" mesh_name = 'Mesh' working_dir = tempfile.gettempdir() + '/FEM/' +standard_inp_file = FreeCAD.getHomePath() + 'Mod/Fem/inp_standard.inp' mesh_points_file = FreeCAD.getHomePath() + 'Mod/Fem/mesh_points.csv' mesh_volumes_file = FreeCAD.getHomePath() + 'Mod/Fem/mesh_volumes.csv' @@ -104,6 +99,22 @@ class FemTest(unittest.TestCase): self.pressure_constraint.Pressure = 10.000000 self.pressure_constraint.Reversed = True + def compare_inp_files(self, file_name1, file_name2): + file1 = open(file_name1, 'r') + file2 = open(file_name2, 'r') + f1 = file1.readlines() + f2 = file2.readlines() + lf1 = [l for l in f1 if not l.startswith('**')] + lf2 = [l for l in f2 if not l.startswith('**')] + import difflib + diff = difflib.unified_diff(lf1, lf2, n=0) + result = '' + for l in diff: + result += l + file1.close() + file2.close() + return result + def test_new_analysis(self): FreeCAD.Console.PrintMessage('\nChecking FEM new analysis...\n') self.create_new_analysis() @@ -141,10 +152,11 @@ class FemTest(unittest.TestCase): FreeCAD.Console.PrintMessage('\nChecking FEM inp file write...\n') fea.setup_working_dir(working_dir) + FreeCAD.Console.PrintMessage('\nWriting {}/{}.inp\n'.format(working_dir, mesh_name)) error = fea.write_inp_file() - md5_test = hashlib.md5(open(working_dir + mesh_name + '.inp', 'rb').read()).hexdigest() - self.assertEqual(md5_standard, md5_test, "FemTools write_inp_file failed. md5 \ - sums don't match. md5 for the test file is {}".format(md5_test)) + FreeCAD.Console.PrintMessage('\nComparing {} to {}/{}.inp\n'.format(standard_inp_file, working_dir, mesh_name)) + ret = self.compare_inp_files(standard_inp_file, working_dir + "/" + mesh_name + '.inp') + self.assertFalse(ret, "FemTools write_inp_file test failed.\n{}".format(ret)) def tearDown(self): FreeCAD.closeDocument("FemTest") diff --git a/src/Mod/Fem/ccxInpWriter.py b/src/Mod/Fem/ccxInpWriter.py index 7a2c398ba..2c08a5e93 100644 --- a/src/Mod/Fem/ccxInpWriter.py +++ b/src/Mod/Fem/ccxInpWriter.py @@ -1,11 +1,13 @@ import FreeCAD import os import sys +import time class inp_writer: def __init__(self, analysis_obj, mesh_obj, mat_obj, fixed_obj, force_obj, pressure_obj, dir_name=None): self.dir_name = dir_name + self.analysis = analysis_obj self.mesh_object = mesh_obj self.material_objects = mat_obj self.fixed_objects = fixed_obj @@ -17,6 +19,7 @@ class inp_writer: os.mkdir(self.dir_name) self.base_name = self.dir_name + '/' + self.mesh_object.Name self.file_name = self.base_name + '.inp' + self.fc_ver = FreeCAD.Version() def write_calculix_input_file(self): self.mesh_object.FemMesh.writeABAQUS(self.file_name) @@ -346,6 +349,12 @@ class inp_writer: f.write('\n***********************************************************\n') f.write('** CalculiX Input file\n') f.write('** written by {} function\n'.format(sys._getframe().f_code.co_name)) + f.write('** written by --> FreeCAD ' + self.fc_ver[0] + '.' + self.fc_ver[1] + '.' + self.fc_ver[2] + '\n') + f.write('** written on --> ' + time.ctime() + '\n') + f.write('** file name --> ' + os.path.basename(FreeCAD.ActiveDocument.FileName) + '\n') + f.write('** analysis name --> ' + self.analysis.Name + '\n') + f.write('**\n') + f.write('**\n') f.write('**\n') f.write('** Units\n') f.write('**\n') diff --git a/src/Mod/Fem/test_file.inp b/src/Mod/Fem/inp_standard.inp similarity index 98% rename from src/Mod/Fem/test_file.inp rename to src/Mod/Fem/inp_standard.inp index 6ee687d24..9341a866b 100644 --- a/src/Mod/Fem/test_file.inp +++ b/src/Mod/Fem/inp_standard.inp @@ -451,7 +451,10 @@ Eall ** Young's modulus unit is MPa = N/mm2 *MATERIAL, NAME=Test Material *ELASTIC -20000 , 0.360 +20000 , +0.360 +*DENSITY +1e-09 , *SOLID SECTION, ELSET=MechanicalMaterial, MATERIAL=Test Material *********************************************************** @@ -521,6 +524,12 @@ S *********************************************************** ** CalculiX Input file ** written by write_footer function +** written by --> FreeCAD 0.16.5287 (Git) +** written on --> Thu Jul 30 11:22:52 2015 +** file name --> +** analysis name --> MechanicalAnalysis +** +** ** ** Units **