-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregrid_remesh.py
More file actions
287 lines (245 loc) · 10.2 KB
/
regrid_remesh.py
File metadata and controls
287 lines (245 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python3
# -*- coding: UTF-8 no BOM -*-
import os,sys,math,re,time,struct
import h5py
import re
import numpy as np
import os, sys, shutil
import scipy
from scipy import spatial
from scipy.interpolate import griddata
import argparse
import subprocess
import damask
import pandas as pd
import itertools as it
from scipy.linalg import polar
from numpy.linalg import inv
import shutil
from imp import reload
from damask import regriding as rgg
from damask import Rotation
from output_reader import output_reader
# combining the codes from 0_main_all and remesh_coords
class Remesh_for_CA():
"""
This class regrids and remeshes the DAMASK output to be used in CASIPT.
"""
def __init__(self,geom,load,inc,folder):
"""
Initializes the arguments.
Parameter
---------
geom : str
Name of the geom file
load : str
Name of the load file
inc : int
Increment for which regridding is being done
folder : str
Path to the folder
"""
self.geom = geom
self.load = load
self.inc = inc
self.folder = folder
def main_all(self,geom,load,inc,folder):
"""
Regrids the data and generates a HDF5 and text file for CA.
Parameters
----------
geom : str
Name of the geom file
load : str
Name of the load file
inc : int
Increment for which regridding is being done
folder : str
Path to the folder
"""
isElastic = False
scale = 1.0
grid = False
os.chdir('/nethome/v.shah/{}/'.format(folder))
geom_name = rgg.setting_util.remove_fileFormat(geom)
load_name = rgg.setting_util.remove_fileFormat(load)
isElastic = rgg.setting_util.set_elasticDeformation(isElastic)
seedScale = rgg.setting_util.set_scale4gridSeedsRegridding(scale, grid)
hdf5_name = '%s_%s.hdf5'%(geom_name,load_name)
history_name = '%s_%s_regriddingHistory.hdf5'%(geom_name,load_name)
rg = rgg.geom_regridder(geom_name,load_name,increment=inc)
rg.read_geom()
print(rg.microstructureFlatten_0.shape)
rg.read_h5Output(inc) #this function also gets the displacements of nodes and cells apparently
rg.regrid_geom()
self.new_size = rg.sizeRVE_regrid
self.new_grid = rg.gridSeeds_regrid
print('new size type',type(self.new_size))
# Building the new coordinates
elem0 = int(rg.gridSeeds_0.prod())
elem_rg = int(rg.gridSeeds_regrid.prod())
New_RVE_size = rg.sizeRVE_regrid
new_grid_cell = rg.gridSeeds_regrid
origin0 = rg.originRVE_0
Cell_coords = rg.gridCoords_cell_regrid #or it should be after the periodic shift??
print(Cell_coords.shape)
#------------------------------------------
# reading main inputs for processing
out5 = output_reader(hdf5_name)
inc, inc_key = out5.make_incerement(inc)
phase_name = out5.constituents[0]
# reading main inputs
orientations = np.array(out5.data[inc_key]['constituent'][phase_name]['generic']['orientation'])
orientations = np.array(orientations.tolist())
## grain rotation
grain_rotation = np.array(out5.data[inc_key]['constituent'][phase_name]['generic']['grain_rotation']) #need to add [:,3] is using damask generated output
grain_rotation_rg = grain_rotation[rg.get_nearestNeighbors()]
grain_rotation_rg_scalar = grain_rotation_rg
mylist = []
for count,ori in enumerate(orientations):
o = Rotation.fromQuaternion(list(ori))
mylist.append(o.asEulers(degrees=None).tolist())
eulers = np.array(mylist)
eulers_rg = eulers[rg.get_nearestNeighbors()]
## dislocation density
rho_m = np.array(out5.data[inc_key]['constituent'][phase_name]['plastic']['rho_mob'])
rho_d = np.array(out5.data[inc_key]['constituent'][phase_name]['plastic']['rho_dip'])
rho = rho_m + rho_d
## reshaped
rho_m_rg = rho_m[rg.get_nearestNeighbors()]
rho_d_rg = rho_d[rg.get_nearestNeighbors()]
rho_rg = rho[rg.get_nearestNeighbors()]
rho= np.sum(rho_rg,axis=1)
## subgrain sizes
r_s = np.array(out5.data[inc_key]['constituent'][phase_name]['plastic']['r_s'])
r_s_rg = r_s[rg.get_nearestNeighbors()]
#
print('orientation shape before: ',np.shape(orientations))
orientations_rg = orientations[rg.get_nearestNeighbors()] #hopefully it works
print('orientation shape after: ',np.shape(orientations_rg))
#--------------
# make df
#---------------
df = pd.DataFrame()
# coords for new grids
Cell_coords = Cell_coords*1E-06
df['x'] = Cell_coords[:,0]
df['y'] = Cell_coords[:,1]
df['z'] = Cell_coords[:,2]
# initial grain
#df['grain'] = np.array(df_cell['grain'])[rebacked_id]
df['grain'] = rg.microstructureFlatten_0[rg.nearestNeighbors_locs]
## Rotation
df['Rotation'] = grain_rotation_rg_scalar
# # total dislo
df['rho'] = rho
# # subgrain sizes
df['r_s'] = r_s_rg
# euler angles
df['phi1'] = eulers_rg[:,0]
df['PHI'] = eulers_rg[:,1]
df['phi2'] = eulers_rg[:,2]
header_f = '%s %s\n'%(str(int(np.prod(rg.gridSeeds_regrid))),str(max(df['grain']))) #,str(max(df['grain']))
output = '%s_%s'%(geom_name,load_name)
file_rg = '%s_%s.txt'%(output,inc)
if not os.path.exists(os.path.join('/nethome/v.shah',folder,'postProc')):
os.makedirs(os.path.join('/nethome/v.shah',folder,'postProc'))
with open('postProc/'+file_rg,'w') as f:
f.write(header_f)
df.to_string(f,header=False,formatters=["{:.8f}".format,"{:.8f}".format,"{:.8f}".format, \
"{:.8f}".format,"{:.8f}".format,"{:.6E}".format, \
"{:.12f}".format,"{:.8f}".format,"{:.8f}".format, \
"{:.8f}".format],index=False)
#create a hdf5 file
new_hdf_name = 'new_' + geom_name + '_' + load_name + '_' + str(inc) + '.hdf5'
hdf = h5py.File(new_hdf_name,'w')
hdf.attrs['DADF5_version_major'] = 0
hdf.attrs['DADF5_version_minor'] = 6
hdf.attrs['DADF5-version'] = 0.2
hdf.create_group('geometry')
hdf['geometry'].attrs['grid'] = np.array(rg.gridSeeds_regrid, np.int32)
hdf['geometry'].attrs['size'] = np.array(rg.sizeRVE_regrid, np.float64)
hdf['geometry'].attrs['origin'] = np.array(rg.originRVE_0,np.float64)
#
##mapping data
comp_dtype = np.dtype([('Name',np.string_,64),('Position',np.int32)])
#new_len = np.prod(np.int32(rg.new_grid))
new_len = np.prod(np.int32(rg.gridSeeds_regrid))
data_name = [phase_name]*int(new_len)
data_value = [i for i in range(new_len)]
new_data = list(zip(data_name,data_value))
new_data = np.array(new_data,dtype=comp_dtype)
new_data = new_data.reshape(new_len,1)
dataset = hdf.create_dataset("/mapping/cellResults/constituent",(new_len,1),comp_dtype)
dataset[...] = new_data
data_name = ['1_SX']*int(new_len)
new_data = list(zip(data_name,data_value))
new_data = np.array(new_data,dtype=comp_dtype)
new_data = new_data.reshape(new_len,1)
dataset = hdf.create_dataset("/mapping/cellResults/materialpoint",(new_len,1),comp_dtype)
dataset[...] = new_data
#orientation_rg
comp_dtype = np.dtype([('w',np.float64),('x',np.float64),('y',np.float64),('z',np.float64)])
#new_len = np.prod(np.int32(rg.new_grid))
new_len = np.prod(np.int32(rg.gridSeeds_regrid))
dataset_ori = hdf.create_dataset("/{}/constituent/{}/generic/orientation".format(inc_key,phase_name),(new_len,),comp_dtype)
orientations_rg = np.array([tuple(i) for i in orientations_rg[:]],dtype=comp_dtype)
dataset_ori[...] = orientations_rg
hdf["/{}/constituent/{}/generic/orientation".format(inc_key,phase_name)].attrs['Lattice'] = 'bcc'
#rho_rg and grain_rotation_rg_scalar
dataset_rho = hdf.create_dataset("/{}/constituent/{}/plastic/tot_density".format(inc_key,phase_name),(new_len,))
dataset_rho[...] = rho
dataset_rot = hdf.create_dataset("/{}/constituent/{}/generic/grain_rotation".format(inc_key,phase_name),(new_len,))
dataset_rot[...] = grain_rotation_rg_scalar
hdf.create_group('/{}/materialpoint/1_SX/generic'.format(inc_key))
hdf.create_group('/{}/materialpoint/1_SX/plastic'.format(inc_key))
#hdf.create_group('inc{}'.format(inc))
print(inc_key)
return self.new_size,self.new_grid
def remesh_coords(self,filename,unit,folder):
"""
Remeshes the data to equidistant grid.
Parameters
----------
filename : str
file path
unit : float
Our units in comparison to DAMASK
folder : str
simulation folder
"""
os.chdir('/nethome/v.shah/{}/postProc'.format(folder))
dx = np.min(self.new_size/self.new_grid)*1E-06
print(dx)
unit_scale = unit
is2d = 0 # 1 for 2D data
data = np.loadtxt(filename,skiprows=1)
min_x = unit_scale*np.min(data[:,0])
min_y = unit_scale*np.min(data[:,1])
min_z = unit_scale*np.min(data[:,2])
#shift coords to start from 0
data[:,0] = unit_scale*data[:,0] - min_x
data[:,1] = unit_scale*data[:,1] - min_y
data[:,2] = unit_scale*data[:,2] - min_z
nx = int(np.max(data[:,0])/dx)
ny = int(np.max(data[:,1])/dx)
nz = int(np.max(data[:,2])/dx)
print('Nx:',nx)
print('Ny:',ny)
print('Nz:',nz)
x_new = np.mgrid[0:nx+1]*dx
y_new = np.mgrid[0:ny+1]*dx
z_new = np.mgrid[0:nz+1]*dx
#new_coords = np.vstack(np.meshgrid(x_new,y_new,z_new)).reshape(3,-1).T
#new_coords = np.vstack(np.meshgrid(y_new,z_new,x_new)).reshape(3,-1).T
#new_coords = np.vstack(np.meshgrid(z_new,y_new,x_new)).reshape(3,-1).T
new_coords = np.stack(np.meshgrid(x_new,y_new,z_new,indexing='ij'),axis=-1).reshape(((nx+1)*(ny+1)*(nz+1),3),order='F')
new_data = np.zeros((len(new_coords),10))
new_data[:,0:3] = new_coords
new_data[:,3:10] = griddata(data[:,0:3],data[:,3:10],new_data[:,0:3],method='nearest')
new_filename = 'remesh_' + os.path.basename(filename)
dir_file = os.path.dirname(os.path.abspath(filename))
new_path = os.path.join(dir_file,new_filename)
np.savetxt(new_path,new_data,fmt = ' '.join(['%.10e']*3 + ['%i'] + ['%.10e']*6), \
header='{} {}'.format(str(len(new_data)),str(int(np.max(data[:,3])))),comments='')
return nx,ny,nz