77
88 *** TEST FOR TAGS AND LABELS EQUIVALENCE ***
99
10- Ensure the results of a simulation are the same whether boundary conditions
11- are applied using the corresponding tags or labels .
10+ Ensure the results of a simulation are the same when boundary conditions
11+ are applied using either of the tag or label of a mesh group .
1212
1313 Parametrization:
1414 - Mesh file
2323
2424 - load.yaml: load file (-l)
2525 - material_base.yaml: material file (-m; without orientations)
26- - List of meshes (from all other mesh tests)
26+ - several mesh files (from all other mesh tests)
2727 - numerics.yaml: numerics configuration (-n)
2828
2929 ------------------------------------------------------------------------ """
@@ -33,7 +33,7 @@ def res_path(res_path_base):
3333 """Directory containing testing resources."""
3434 return res_path_base / 'mesh_tags_labels'
3535
36- def bc_setup (tmp_path , load_file , np_rng , n_D ):
36+ def bc_setup (np_rng , n_D ):
3737 """Randomized displacement boundary conditions."""
3838 # Randomly set displacement BC (v in 2D, w in 3D)
3939 dot_u = ['x' , 'x' , 'x' ]
@@ -42,26 +42,24 @@ def bc_setup(tmp_path, load_file, np_rng, n_D):
4242 return dot_u
4343
4444
45- def load_setup (tmp_path , load_file , mesh , np_rng , dot_u , n_D , key , label ):
45+ def load_setup (load_config , mesh_file , np_rng , dot_u , n_D , key , label ):
4646 """Randomized displacement boundary conditions."""
4747 # Set up load file
4848 # Use key 'label' or 'tag'. If the latter, retrieve the tag corresponding
4949 # to 'label' from the mesh file
50- load_config = damask .YAML .load (tmp_path / f'{ load_file } .yaml' )
5150
52- if key == 'by_label ' :
53- load_config ['loadstep' ][0 ]['boundary_conditions' ]['mechanical' ][1 ][ 'label' ] = label
54- load_config [ 'loadstep' ][ 0 ][ 'boundary_conditions' ][ 'mechanical' ][ 1 ][ ' dot_u'] = dot_u
51+ if key == 'label ' :
52+ load_config ['loadstep' ][0 ]['boundary_conditions' ]['mechanical' ][1 ] = \
53+ { 'label' : label , ' dot_u': dot_u }
5554 else :
56- tag = get_tag_from_label (tmp_path , mesh , label )
57- rename_key_nested (load_config , 'label' , 'tag' )
58- load_config ['loadstep' ][0 ]['boundary_conditions' ]['mechanical' ][1 ]['tag' ] = label
59- load_config ['loadstep' ][0 ]['boundary_conditions' ]['mechanical' ][1 ]['dot_u' ] = dot_u
55+ tag = get_tag_from_label (mesh_file , label )
56+ load_config ['loadstep' ][0 ]['boundary_conditions' ]['mechanical' ][1 ] = \
57+ {'tag' : tag , 'dot_u' : dot_u }
6058
61- load_config . save ( tmp_path / f' { load_file } .yaml' )
59+ return load_config
6260
6361
64- def material_setup (tmp_path , mat_file , np_rng ):
62+ def material_setup (mat_config , np_rng ):
6563 """
6664 Randomized material properties.
6765
@@ -88,28 +86,18 @@ def material_setup(tmp_path, mat_file, np_rng):
8886 print ('eigenvalues' , eigvals )
8987
9088 # Set up material file
91- mat_config = damask .YAML .load (tmp_path / f'{ mat_file } _base.yaml' )
9289 mat_config ['phase' ]['phase_0' ]['mechanical' ]['elastic' ] = \
9390 {'type' :'Hooke' , 'C_11' : C_11 , 'C_12' : C_12 , 'C_44' : C_44 ,
9491 'C_33' : C_33 , 'C_13' : C_13 , 'C_66' : C_66 }
95- mat_config .save (tmp_path / f'{ mat_file } .yaml' )
96- mat_config = damask .ConfigMaterial .load (tmp_path / f'{ mat_file } .yaml' )
9792 mat_config = mat_config .material_add (phase = ['phase_0' ],
9893 O = damask .Rotation .from_random (15 ,rng_seed = np_rng ),
9994 homogenization = 'SX' )
100- mat_config . save ( tmp_path / f' { mat_file } .yaml' )
95+ return mat_config
10196
10297
103- def damask_results (tmp_path , job ):
104- """Retrieve numerical solution from DADF5 file"""
105- res = damask .Result (tmp_path / f'{ job } .hdf5' ).view (increments = 1 )
106-
107- return res .get ('u_n' )
108-
109-
110- def get_tag_from_label (tmp_path , mesh , label ):
98+ def get_tag_from_label (mesh_file , label ):
11199 """ Find number of physical groups."""
112- with open (tmp_path / f' { mesh } .msh' , 'r' ) as f :
100+ with open (mesh_file , 'r' ) as f :
113101 while True :
114102 if f .readline ().rstrip () == '$PhysicalNames' :
115103 break
@@ -123,17 +111,6 @@ def get_tag_from_label(tmp_path, mesh, label):
123111
124112 raise ValueError (f'"{ label } " not found' )
125113
126-
127- def rename_key_nested (dictionary , old_key , new_key ):
128- """ Rename a key in a nested dictionary."""
129- # https://www.geeksforgeeks.org/python/how-to-change-the-name-of-a-key-in-dictionary/
130- for key in list (dictionary .keys ()):
131- if isinstance (dictionary [key ], dict ):
132- rename_key_nested (dictionary [key ], old_key , new_key )
133- if key == old_key :
134- dictionary [new_key ] = dictionary .pop (old_key )
135-
136-
137114try :
138115 mark_old_PETSc = pytest .mark .xfail (pytest .petsc_version ()< '3.24.0' , reason = 'Not implemented' )
139116except EnvironmentError :
@@ -152,30 +129,22 @@ def rename_key_nested(dictionary, old_key, new_key):
152129 ('2' , 'singleCrystal' , 'tensionZ' )])
153130def test_mesh_tags_labels (res_path , copy_files , tmp_path , np_rng ,
154131 n_D , mesh_file , label , petsc_version ):
155- copy_files (res_path , tmp_path )
156- mesh = f'{ mesh_file } '
157- load = 'load'
158- mat = 'material'
132+ copy_files (res_path , tmp_path , [f'{ mesh_file } .msh' , 'numerics.yaml' ])
159133
160- dot_u = bc_setup (tmp_path , load , np_rng , n_D )
161- material_setup (tmp_path , mat , np_rng )
134+ load_config = damask .YAML .load (res_path / 'load.yaml' )
135+ mat_config = damask .ConfigMaterial .load (res_path / 'material_base.yaml' )
136+ dot_u = bc_setup (np_rng , n_D )
162137
163- for key in [ 'by_label' , 'by_tag' ]:
164- job = f' { mesh } _ { key } '
165-
166- load_setup ( tmp_path , load , mesh , np_rng , dot_u , n_D , key , label )
138+ material_setup ( mat_config , np_rng ). save ( tmp_path / 'material.yaml' )
139+ for key in [ 'label' , 'tag' ]:
140+ load_setup ( load_config , tmp_path / f' { mesh_file } .msh' , np_rng , dot_u , n_D , key ,
141+ label ). save ( tmp_path / f'load_ { key } .yaml' )
167142
168143 """ Numerical solution """
169- try :
170- if n_D == 3 and dot_u .count ('x' ) == 0 and petsc_version () < '3.24.1' :
171- pytest .xfail ('solved in https://gitlab.com/petsc/petsc/-/merge_requests/8188' )
172- except EnvironmentError :
173- pass
174-
175- damask .util .run (f'damask_mesh -m { mat } .yaml -l { load } .yaml -g { mesh } .msh ' +
176- f'-n numerics.yaml -j { job } ' , wd = tmp_path )
144+ damask .util .run (f'damask_mesh -m material.yaml -l load_{ key } .yaml -g { mesh_file } .msh ' +
145+ f'-n numerics.yaml -j { key } ' , wd = tmp_path )
177146
178147 # Results comparison
179- with_label = damask_results (tmp_path , f' { mesh } _by_label' ),
180- with_tag = damask_results (tmp_path , f' { mesh } _by_tag' ),
181- assert (np .allclose (with_label , with_tag , rtol = 1.0e-14 , atol = 1.0e-14 ))
148+ u_n_label = damask . Result (tmp_path / 'label.hdf5' ). view ( increments = - 1 ). get ( 'u_n' )
149+ u_n_tag = damask . Result (tmp_path / 'tag.hdf5' ). view ( increments = - 1 ). get ( 'u_n' )
150+ assert (np .allclose (u_n_label , u_n_tag , rtol = 1.0e-14 , atol = 1.0e-14 ))
0 commit comments