def test_extract_strain_yz(self): '''Correctly extract StrainYZ''' field_name = 'StrainYZ' image, spacing, origin, n_elements = field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), field_name) npt.assert_array_equal(image.shape, [25, 25, 25]) npt.assert_array_almost_equal(spacing, [0.03399992, 0.03399992, 0.03400004]) npt.assert_array_almost_equal(origin, [6.64700055, 7.22500014, 1.71700007]) self.assertEqual(n_elements, 7087) test_points = [[(0, 0, 0), 0.00025088153779506683], [(0, 0, 24), 0], [(20, 21, 23), 0], [(6, 18, 23), 0.003849506378173828], [(15, 9, 24), 0.0], [(5, 6, 17), -0.004284568130970001]] for index, value in test_points: self.assertAlmostEqual(image[index], value)
def test_extract_strain_zz(self): '''Correctly extract StrainZZ''' field_name = 'StrainZZ' image, spacing, origin, n_elements = field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), field_name) npt.assert_array_equal(image.shape, [25, 25, 25]) npt.assert_array_almost_equal(spacing, [0.03399992, 0.03399992, 0.03400004]) npt.assert_array_almost_equal(origin, [6.64700055, 7.22500014, 1.71700007]) self.assertEqual(n_elements, 7087) test_points = [[(0, 0, 0), -0.00832836702466011], [(0, 0, 24), 0], [(20, 21, 23), 0], [(6, 18, 23), -0.019146479666233063], [(15, 9, 24), 0.0], [(5, 6, 17), -0.017154186964035034]] for index, value in test_points: self.assertAlmostEqual(image[index], value)
def test_extract_strain_xy(self): '''Correctly extract StrainXY''' field_name = 'StrainXY' image, spacing, origin, n_elements = field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), field_name) npt.assert_array_equal(image.shape, [25, 25, 25]) npt.assert_array_almost_equal(spacing, [0.03399992, 0.03399992, 0.03400004]) npt.assert_array_almost_equal(origin, [6.64700055, 7.22500014, 1.71700007]) self.assertEqual(n_elements, 7087) test_points = [[(0, 0, 0), 7.61914998292923e-05], [(0, 0, 24), 0], [(20, 21, 23), 0], [(6, 18, 23), 0.008264606818556786], [(15, 9, 24), 0.0], [(5, 6, 17), -0.012141803279519081]] for index, value in test_points: self.assertAlmostEqual(image[index], value)
def test_extract_strain_xx(self): '''Correctly extract StrainXX''' field_name = 'StrainXX' image, spacing, origin, n_elements = field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), field_name) npt.assert_array_equal(image.shape, [25, 25, 25]) npt.assert_array_almost_equal(spacing, [0.03399992, 0.03399992, 0.03400004]) npt.assert_array_almost_equal(origin, [6.64700055, 7.22500014, 1.71700007]) self.assertEqual(n_elements, 7087) test_points = [[(0, 0, 0), 0.0024997536092996597], [(0, 0, 24), 0], [(20, 21, 23), 0], [(6, 18, 23), 0.006212107837200165], [(15, 9, 24), 0.0], [(5, 6, 17), 0.004377299919724464]] for index, value in test_points: self.assertAlmostEqual(image[index], value)
def test_extract_stress_xx(self): '''Correctly extract StressXX''' field_name = 'StressXX' image, spacing, origin, n_elements = field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), field_name) npt.assert_array_equal(image.shape, [25, 25, 25]) npt.assert_array_almost_equal(spacing, [0.03399992, 0.03399992, 0.03400004]) npt.assert_array_almost_equal(origin, [6.64700055, 7.22500014, 1.71700007]) self.assertEqual(n_elements, 7087) test_points = [[(0, 0, 0), 0.3343238830566406], [(0, 0, 24), 0], [(20, 21, 23), 0], [(6, 18, 23), -8.079221725463867], [(15, 9, 24), 0.0], [(5, 6, 17), -16.37940216064453]] for index, value in test_points: self.assertAlmostEqual(image[index], value)
def test_extract_stress_yz(self): '''Correctly extract StressYZ''' field_name = 'StressYZ' image, spacing, origin, n_elements = field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), field_name) npt.assert_array_equal(image.shape, [25, 25, 25]) npt.assert_array_almost_equal(spacing, [0.03399992, 0.03399992, 0.03400004]) npt.assert_array_almost_equal(origin, [6.64700055, 7.22500014, 1.71700007]) self.assertEqual(n_elements, 7087) test_points = [[(0, 0, 0), 0.6589500308036804], [(0, 0, 24), 0], [(20, 21, 23), 0], [(6, 18, 23), 10.11087703704834], [(15, 9, 24), 0.0], [(5, 6, 17), -11.253583908081055]] for index, value in test_points: self.assertAlmostEqual(image[index], value)
def test_extract_stress_xz(self): '''Correctly extract StressXZ''' field_name = 'StressXZ' image, spacing, origin, n_elements = field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), field_name) npt.assert_array_equal(image.shape, [25, 25, 25]) npt.assert_array_almost_equal(spacing, [0.03399992, 0.03399992, 0.03400004]) npt.assert_array_almost_equal(origin, [6.64700055, 7.22500014, 1.71700007]) self.assertEqual(n_elements, 7087) test_points = [[(0, 0, 0), -0.899536669254303], [(0, 0, 24), 0], [(20, 21, 23), 0], [(6, 18, 23), 0.7890536785125732], [(15, 9, 24), 0.0], [(5, 6, 17), -4.734708309173584]] for index, value in test_points: self.assertAlmostEqual(image[index], value)
def test_extract_stress_xy(self): '''Correctly extract StressXY''' field_name = 'StressXY' image, spacing, origin, n_elements = field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), field_name) npt.assert_array_equal(image.shape, [25, 25, 25]) npt.assert_array_almost_equal(spacing, [0.03399992, 0.03399992, 0.03400004]) npt.assert_array_almost_equal(origin, [6.64700055, 7.22500014, 1.71700007]) self.assertEqual(n_elements, 7087) test_points = [[(0, 0, 0), 0.20011991262435913], [(0, 0, 24), 0], [(20, 21, 23), 0], [(6, 18, 23), 21.707307815551758], [(15, 9, 24), 0.0], [(5, 6, 17), -31.890914916992188]] for index, value in test_points: self.assertAlmostEqual(image[index], value)
def test_extract_stress_zz(self): '''Correctly extract StressZZ''' field_name = 'StressZZ' image, spacing, origin, n_elements = field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), field_name) npt.assert_array_equal(image.shape, [25, 25, 25]) npt.assert_array_almost_equal(spacing, [0.03399992, 0.03399992, 0.03400004]) npt.assert_array_almost_equal(origin, [6.64700055, 7.22500014, 1.71700007]) self.assertEqual(n_elements, 7087) test_points = [[(0, 0, 0), -56.54663848876953], [(0, 0, 24), 0], [(20, 21, 23), 0], [(6, 18, 23), -141.2898406982422], [(15, 9, 24), 0.0], [(5, 6, 17), -129.48597717285156]] for index, value in test_points: self.assertAlmostEqual(image[index], value)
def test_extract_stress_yy(self): '''Correctly extract StressYY''' field_name = 'StressYY' image, spacing, origin, n_elements = field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), field_name) npt.assert_array_equal(image.shape, [25, 25, 25]) npt.assert_array_almost_equal(spacing, [0.03399992, 0.03399992, 0.03400004]) npt.assert_array_almost_equal(origin, [6.64700055, 7.22500014, 1.71700007]) self.assertEqual(n_elements, 7087) test_points = [[(0, 0, 0), 0.7583122253417969], [(0, 0, 24), 0], [(20, 21, 23), 0], [(6, 18, 23), -27.049179077148438], [(15, 9, 24), 0.0], [(5, 6, 17), -24.753982543945312]] for index, value in test_points: self.assertAlmostEqual(image[index], value)
def test_extract_strain_xz(self): '''Correctly extract StrainXZ''' field_name = 'StrainXZ' image, spacing, origin, n_elements = field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), field_name) npt.assert_array_equal(image.shape, [25, 25, 25]) npt.assert_array_almost_equal(spacing, [0.03399992, 0.03399992, 0.03400004]) npt.assert_array_almost_equal(origin, [6.64700055, 7.22500014, 1.71700007]) self.assertEqual(n_elements, 7087) test_points = [[(0, 0, 0), -0.000342479906976223], [(0, 0, 24), 0], [(20, 21, 23), 0], [(6, 18, 23), 0.0003004157915711403], [(15, 9, 24), 0.0], [(5, 6, 17), -0.001802641898393631]] for index, value in test_points: self.assertAlmostEqual(image[index], value)
def test_extract_strain_yy(self): '''Correctly extract StrainYY''' field_name = 'StrainYY' image, spacing, origin, n_elements = field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), field_name) npt.assert_array_equal(image.shape, [25, 25, 25]) npt.assert_array_almost_equal(spacing, [0.03399992, 0.03399992, 0.03400004]) npt.assert_array_almost_equal(origin, [6.64700055, 7.22500014, 1.71700007]) self.assertEqual(n_elements, 7087) test_points = [[(0, 0, 0), 0.002580466214567423], [(0, 0, 24), 0], [(20, 21, 23), 0], [(6, 18, 23), 0.0026008994318544865], [(15, 9, 24), 0.0], [(5, 6, 17), 0.0027830759063363075]] for index, value in test_points: self.assertAlmostEqual(image[index], value)
def test_extract_von_mises_stress(self): '''Correctly extract VonMisesStress''' field_name = 'VonMisesStress' image, spacing, origin, n_elements = field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), field_name) npt.assert_array_equal(image.shape, [25, 25, 25]) npt.assert_array_almost_equal(spacing, [0.03399992, 0.03399992, 0.03400004]) npt.assert_array_almost_equal(origin, [6.64700055, 7.22500014, 1.71700007]) self.assertEqual(n_elements, 7087) test_points = [[(0, 0, 0), 57.127845764160156], [(0, 0, 24), 0], [(20, 21, 23), 0], [(6, 18, 23), 131.52987670898438], [(15, 9, 24), 0.0], [(5, 6, 17), 124.15428161621094]] for index, value in test_points: self.assertAlmostEqual(image[index], value)
def test_extract_strain_energy_density(self): '''Correctly extract SED''' field_name = 'StrainEnergyDensity' image, spacing, origin, n_elements = field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), field_name) npt.assert_array_equal(image.shape, [25, 25, 25]) npt.assert_array_almost_equal(spacing, [0.03399992, 0.03399992, 0.03400004]) npt.assert_array_almost_equal(origin, [6.64700055, 7.22500014, 1.71700007]) self.assertEqual(n_elements, 7087) test_points = [[(0, 0, 0), 0.2371111661195755], [(0, 0, 24), 0], [(20, 21, 23), 0], [(6, 18, 23), 1.401611566543579], [(15, 9, 24), 0.0], [(5, 6, 17), 1.262300968170166]] for index, value in test_points: self.assertAlmostEqual(image[index], value)
def ExtractFields(input_model, output_image, field_name, outside_value=0.0, lower_threshold=0.0, upper_threshold=0.0, output_type='float'): # Python 2/3 compatible input from six.moves import input # Check requested output type output_type = output_type.lower() if output_type not in EXTRACT_FIELDS_SUPPORTED_TYPES.keys(): print('Valid output types: {}'.format(', '.join( EXTRACT_FIELDS_SUPPORTED_TYPES.keys()))) os.sys.exit( '[ERROR] Requested type {} not supported'.format(output_type)) # Read input if not os.path.isfile(input_model): os.sys.exit('[ERROR] Cannot find file \"{}\"'.format(input_model)) # Test field name if field_name not in valid_fields(): print('Valid field names: {}'.format(valid_fields())) os.sys.exit('[ERROR] Please provide a valid field name') print('Reading elements into image array') image, spacing, origin, n_elements = field_to_image( input_model, field_name, outside_value) print(' Spacing: {}'.format(spacing)) print(' Origin: {}'.format(origin)) print(' Dimensions: {}'.format(image.shape)) print(' Number of elements: {}'.format(n_elements)) print('') # Convert to VTK print('Converting to vtkImageData') vtkImage = numpy_to_vtkImageData(image, spacing, origin) print('') if output_type == 'float': print('Not rescaling data') else: print('Rescaling data into {} dynamic range'.format(output_type)) # Hack to get data type range source = vtk.vtkImageEllipsoidSource() source.SetWholeExtent(0, 1, 0, 1, 0, 0) source.SetCenter(0, 0, 0) source.SetRadius(0, 0, 0) source.SetOutputScalarType(EXTRACT_FIELDS_SUPPORTED_TYPES[output_type]) source.Update() # Compute min/max if lower_threshold == upper_threshold: scalar_range = vtkImage.GetScalarRange() else: scalar_range = [lower_threshold, upper_threshold] dtype_range = [0, source.GetOutput().GetScalarTypeMax()] print(' Image range: {}'.format(vtkImage.GetScalarRange())) print(' Input range: {}'.format(scalar_range)) print(' Output range: {}'.format(dtype_range)) # Note the equation for shift/scale: # u = (v + ScalarShift)*ScalarScale m = float(dtype_range[1] - dtype_range[0]) / float(scalar_range[1] - scalar_range[0]) b = float(dtype_range[1] - m * scalar_range[1]) b = b / m print(' Shift: {}'.format(b)) print(' Scale: {}'.format(m)) scaler = vtk.vtkImageShiftScale() scaler.SetInputData(vtkImage) scaler.SetShift(b) scaler.SetScale(m) scaler.ClampOverflowOn() scaler.SetOutputScalarType(EXTRACT_FIELDS_SUPPORTED_TYPES[output_type]) scaler.Update() vtkImage = scaler.GetOutput() print(' Output image range: {}'.format(vtkImage.GetScalarRange())) print('') # Write image print('Saving image ' + output_image) writer = get_vtk_writer(output_image) if writer is None: os.sys.exit( '[ERROR] Cannot find writer for file \"{}\"'.format(output_image)) writer.SetInputData(vtkImage) writer.SetFileName(output_image) writer.Update()
def instantiate(): field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial_solved.n88model'), 'fake_field_name')
def instantiate(): field_to_image( os.path.join(self.test_dir, 'test25a_uniaxial.n88model'), 'StrainEnergyDensity')