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 <przemo@firszt.eu>
This commit is contained in:
Przemo Firszt 2015-08-04 20:32:53 +01:00
parent 53eb0dd758
commit 5e3dc0c2c9
5 changed files with 45 additions and 10 deletions

View File

@ -75,6 +75,7 @@ SET(FemScripts_SRCS
FemTools.py
mesh_points.csv
mesh_volumes.csv
inp_standard.inp
MechanicalAnalysis.ui
MechanicalAnalysis.py
MechanicalMaterial.ui

View File

@ -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

View File

@ -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")

View File

@ -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')

View File

@ -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
**