def fe_grab(ns, set_id, newdir, wrt_file): ## el is the # of elements per side of the cube el = 21 ### FINITE ELEMENT RESPONSES ### ## change to directory with the .vtk files cwd = os.getcwd() # os.chdir(cwd + '\\' + newdir) os.chdir(cwd + '/' + newdir) #for unix [r_real_all, msg] = rr.load_fe('orientation_%s%s.mat' %(ns, set_id),set_id,ns,el) ## return to the original directory os.chdir('..') start = time.time() for comp in xrange(6): np.save('r%s_%s%s' %(comp,ns,set_id), r_real_all[:,:,:,comp,:]) rr.WP(msg,wrt_file) ## responses in frequency space r_fft = np.fft.fftn(r_real_all[:,:,:,comp,:], axes = [0,1,2]) np.save('r%s_fft_%s%s' %(comp,ns,set_id),r_fft) end = time.time() timeE = np.round((end - start),3) msg = 'Convert FE results to frequency space: %s seconds' %timeE rr.WP(msg,wrt_file)
def validation_procedure(ns_cal, ns_val, set_id_cal, set_id_val, comp, wrt_file): start = time.time() ## el is the # of elements per side of the cube el = 21 ## H is the number of GSH coefficients H = 15 M = np.load('M_%s%s.npy' % (ns_val, set_id_val)) specinfc = np.load('specinfc%s_%s%s.npy' % (comp, ns_cal, set_id_cal)) mks_R = np.zeros([el, el, el, ns_val]) for sn in xrange(ns_val): mks_R[:, :, :, sn] = rr.validate(M[:, :, :, sn, :], specinfc, H, el) np.save('mksR%s_%s%s' % (comp, ns_val, set_id_val), mks_R) end = time.time() timeE = np.round((end - start), 3) msg = 'validation performed for component %s: %s seconds' % (comp, timeE) rr.WP(msg, wrt_file)
def calibration_procedure(ns,set_id,comp,wrt_file): ## el is the # of elements per side of the cube el = 21 ## specify the number of local states you are using H = 15 M = np.load('M_%s%s.npy' %(ns,set_id)) r_fft = np.load('r%s_fft_%s%s.npy' %(comp,ns,set_id)) start = time.time() specinfc = np.zeros((el**3,H),dtype = 'complex64') ## here we perform the calibration for the scalar FIP specinfc[0,:] = rr.calib(0,M,r_fft,0,H,el,ns) [specinfc[1,:],p] = rr.calib(1,M,r_fft,0,H,el,ns) ## calib_red is simply calib with some default arguments calib_red = partial(rr.calib,M=M,r_fft=r_fft, p=p,H=H,el=el,ns=ns) specinfc[2:(el**3),:] = np.asarray(map(calib_red,range(2,el**3))) np.save('specinfc%s_%s%s' %(comp,ns,set_id),specinfc) end = time.time() timeE = np.round((end - start),3) msg = 'Calibration, component %s: %s seconds' %(comp, timeE) rr.WP(msg,wrt_file)
def read_meas(el, ns, set_id, step, comp, vtk_filename, tensor_id, wrt_file): start = time.time() r_real = np.zeros([el,el,el,ns]) for sn in xrange(ns): l_sn = str(sn+1).zfill(5) r_temp = rr.read_vtk_tensor(filename = vtk_filename %l_sn, el = el, tensor_id = tensor_id, comp = comp) r_real[:,:,:,sn] = np.swapaxes(np.reshape(np.flipud(r_temp), [el,el,el]),1,2) np.save('r%s_%s%s_s%s' %(comp,ns,set_id,step), r_real) ## fftn of response fields r_fft = np.fft.fftn(r_real, axes = [0,1,2]) del r_real np.save('r%s_fft_%s%s_s%s' %(comp,ns,set_id,step),r_fft) end = time.time() timeE = np.round((end - start),3) msg = 'The measure of interest has been read from .vtk file for %s, set %s: %s seconds' %(set_id,comp,timeE) rr.WP(msg,wrt_file)
def read_euler(ns, set_id, vtk_filename, newdir, wrt_file): start = time.time() ## el is the # of elements per side of the cube el = 21 euler = np.zeros([el**3, ns, 3]) ## change to directory with the .vtk files cwd = os.getcwd() # os.chdir(cwd + '\\' + newdir) os.chdir(cwd + '/' + newdir) #for unix for sn in xrange(ns): l_sn = str(sn + 1).zfill(5) euler[:, sn, :] = rr.read_vtk_vector(filename=vtk_filename % l_sn) ## return to the original directory os.chdir('..') np.save('euler_%s%s' % (ns, set_id), euler) end = time.time() timeE = np.round((end - start), 3) msg = 'euler angles read from .vtk file for %s: %s seconds' % (set_id, timeE) rr.WP(msg, wrt_file)
def validation_zero_pad(el_large, ns_cal, ns_val, set_id_cal, set_id_val, step, comp, wrt_file): start = time.time() ## el is the # of elements per side of the calibration cube el = 21 ## H is the number of GSH coefficients H = 15 ## zero-pad the influence coefficients pre_specinfc = np.load('specinfc%s_%s%s_s%s.npy' % (comp, ns_cal, set_id_cal, step)) pre_specinfc = np.fft.ifftn(np.reshape(pre_specinfc, [el, el, el, H])).real shift = np.floor(0.5 * el).astype(int) pre_specinfc = np.roll(np.roll(np.roll(pre_specinfc, shift, 0), shift, 1), shift, 2) specinfc_pad = np.zeros([el_large, el_large, el_large, H]) specinfc_pad[:el, :el, :el, :] = pre_specinfc specinfc_pad = np.roll( np.roll(np.roll(specinfc_pad, -shift, 0), -shift, 1), -shift, 2) del pre_specinfc specinfc = np.reshape(np.fft.fftn(specinfc_pad, axes=[0, 1, 2]), [el_large**3, H]) del specinfc_pad ## perform the prediction procedure M = np.load('M_%s%s.npy' % (ns_val, set_id_val)) mks_R = np.zeros([el_large, el_large, el_large, ns_val]) for sn in xrange(ns_val): specinfc_sqr = np.reshape(specinfc, [el_large, el_large, el_large, H]) mks_F = np.sum(np.conjugate(specinfc_sqr) * M[:, :, :, sn, :], 3) mks_R[:, :, :, sn] = np.fft.ifftn(mks_F).real np.save('mksR%s_%s%s_step%s' % (comp, ns_val, set_id_val, step), mks_R) end = time.time() timeE = np.round((end - start), 3) msg = 'validation performed for component %s: %s seconds' % (comp, timeE) rr.WP(msg, wrt_file)
def micr_func(ns, set_id, wrt_file): start = time.time() el = 21 ## specify the number of local states you are using H = 15 ## import microstructures micr = np.zeros([el, el, el, ns, H], dtype='complex128') pre_micr = np.load('euler_GSH_%s%s.npy' % (ns, set_id)) for h in xrange(H): for sn in range(ns): micr[:, :, :, sn, h] = np.swapaxes( np.reshape(np.flipud(pre_micr[:, sn, h]), [el, el, el]), 1, 2) del pre_micr end = time.time() timeE = np.round((end - start), 3) msg = "generate real-space microstructure function from GSH-coefficients: %s seconds" % timeE rr.WP(msg, wrt_file) ## Microstructure functions in frequency space start = time.time() M = np.fft.fftn(micr, axes=[0, 1, 2]) del micr size = M.nbytes np.save('M_%s%s' % (ns, set_id), M) end = time.time() timeE = np.round((end - start), 3) msg = "FFT3 conversion of micr to M_%s%s: %s seconds" % (ns, set_id, timeE) rr.WP(msg, wrt_file) msg = 'Size of M_%s%s: %s bytes' % (ns, set_id, size) rr.WP(msg, wrt_file)
def read_euler(el, ns, set_id, vtk_filename, wrt_file): start = time.time() euler = np.zeros([el**3,ns,3]) for sn in xrange(ns): l_sn = str(sn+1).zfill(5) euler[:,sn,:] = rr.read_vtk_vector(filename = vtk_filename %l_sn, el = el) np.save('euler_%s%s' %(ns,set_id), euler) end = time.time() timeE = np.round((end - start),3) msg = 'euler angles read from .vtk file for %s: %s seconds' %(set_id, timeE) rr.WP(msg,wrt_file)
def read_meas(ns, set_id, step, comp, vtk_filename, tensor_id, newdir, wrt_file): start = time.time() ## el is the # of elements per side of the cube el = 21 r_real = np.zeros([el, el, el, ns]) ## change to directory with the .vtk files cwd = os.getcwd() # os.chdir(cwd + '\\' + newdir) os.chdir(cwd + '/' + newdir) #for unix for sn in xrange(ns): l_sn = str(sn + 1).zfill(5) r_temp = rr.read_vtk_tensor(filename=vtk_filename % l_sn, tensor_id=tensor_id, comp=comp) r_real[:, :, :, sn] = np.swapaxes(np.reshape(np.flipud(r_temp), [el, el, el]), 1, 2) ## return to the original directory os.chdir('..') np.save('r%s_%s%s_s%s' % (comp, ns, set_id, step), r_real) ## fftn of response fields r_fft = np.fft.fftn(r_real, axes=[0, 1, 2]) del r_real np.save('r%s_fft_%s%s_s%s' % (comp, ns, set_id, step), r_fft) end = time.time() timeE = np.round((end - start), 3) msg = 'The measure of interest has been read from .vtk file for %s, set %s: %s seconds' % ( set_id, comp, timeE) rr.WP(msg, wrt_file)
def euler_to_gsh(el, ns, set_id, wrt_file): start = time.time() H = 15 euler = np.load('euler_%s%s.npy' % (ns, set_id)) euler_GSH = np.zeros([el**3, ns, H], dtype='complex128') for sn in range(ns): for k in range(el**3): euler_GSH[k, sn, :] = gsh.GSH_Hexagonal_Triclinic(euler[k, sn, :]) print sn np.save('euler_GSH_%s%s.npy' % (ns, set_id), euler_GSH) end = time.time() timeE = np.round((end - start), 3) msg = "Conversion from Euler angles to GSH coefficients completed: %s seconds" % timeE rr.WP(msg, wrt_file)
def validation_zero_pad(el_cal, el_val, ns_cal, ns_val, H, set_id_cal, set_id_val, step, wrt_file): for comp in xrange(9): start = time.time() ## zero-pad the influence coefficients pre_pre_specinfc = np.load('specinfc%s_%s%s_s%s.npy' % (comp, ns_cal, set_id_cal, step)) pre_specinfc = np.zeros([el_cal, el_cal, el_cal, H], dtype='complex64') for h in xrange(H): pre_specinfc[:, :, :, h] = np.fft.ifftn( np.reshape(pre_pre_specinfc[:, h], [el_cal, el_cal, el_cal])) h_comp = 1 plt.figure(num=1, figsize=[12, 8]) dmin = np.amin(pre_specinfc[0, :, :, h_comp].real) dmax = np.amax(pre_specinfc[0, :, :, h_comp].real) plt.subplot(221) ax = plt.imshow(pre_specinfc[0, :, :, h_comp].real, origin='lower', interpolation='none', cmap='jet', vmin=dmin, vmax=dmax) plt.colorbar(ax) plt.title('original influence coefficients') pre_specinfc = np.fft.fftshift(pre_specinfc, axes=[0, 1, 2]) # edgeHvec = pre_specinfc[0,0,0,:] plt.subplot(222) slc = np.floor(0.5 * el_cal).astype(int) ax = plt.imshow(pre_specinfc[slc, :, :, h_comp].real, origin='lower', interpolation='none', cmap='jet', vmin=dmin, vmax=dmax) plt.colorbar(ax) plt.title('centered influence coefficients') specinfc_pad = np.zeros([el_val, el_val, el_val, H], dtype='complex64') # for h in xrange(H): # specinfc_pad[:,:,:,h] = edgeHvec[h] el_gap = int(0.5 * (el_val - el_cal)) el_end = el_val - el_gap specinfc_pad[el_gap:el_end, el_gap:el_end, el_gap:el_end, :] = pre_specinfc del pre_specinfc plt.subplot(223) slc = np.floor(0.5 * el_val).astype(int) ax = plt.imshow(specinfc_pad[slc, :, :, h_comp].real, origin='lower', interpolation='none', cmap='jet', vmin=dmin, vmax=dmax) plt.colorbar(ax) plt.title('padded/centered influence coefficients') specinfc_pad = np.fft.ifftshift(specinfc_pad, axes=[0, 1, 2]) plt.subplot(224) ax = plt.imshow(specinfc_pad[0, :, :, h_comp].real, origin='lower', interpolation='none', cmap='jet', vmin=dmin, vmax=dmax) plt.colorbar(ax) plt.title('padded influence coefficients') specinfc = np.fft.fftn(specinfc_pad, axes=[0, 1, 2]) ## perform the prediction procedure M = np.load('M_%s%s.npy' % (ns_val, set_id_val)) mks_R = np.zeros([el_val, el_val, el_val, ns_val]) for sn in xrange(ns_val): mks_F = np.sum(np.conjugate(specinfc) * M[:, :, :, sn, :], 3) mks_R[:, :, :, sn] = np.fft.ifftn(mks_F).real np.save('mksR%s_%s%s_step%s' % (comp, ns_val, set_id_val, step), mks_R) end = time.time() timeE = np.round((end - start), 3) msg = 'validation performed: %s seconds' % timeE rr.WP(msg, wrt_file)
def results_comp(ns,set_id,comp,typ,real_comp): ## el is the # of elements per side of the cube el = 21 ## specify the file to write messages to wrt_file = 'results_comp%s_%s%s_%s.txt' %(comp,ns,set_id,time.strftime("%Y-%m-%d_h%Hm%M")) mks_R = np.load('mksR%s_%s%s.npy' %(comp,ns,set_id)) resp = np.load('r%s_%s%s.npy' %(comp,ns,set_id)) #### MEAN ABSOLUTE STRAIN ERROR (MASE) ### avgr_fe_tot = 0 avgr_mks_tot = 0 MASE_tot = 0 max_err_sum = 0 max_err_all = np.zeros(ns) for sn in xrange(ns): [avgr_fe_indv,avgr_mks_indv, MASE_indv] = rr.eval_meas(mks_R[:,:,:,sn], resp[:,:,:,sn],el) avgr_fe_tot += avgr_fe_indv avgr_mks_tot += avgr_mks_indv MASE_tot += MASE_indv max_err_sum += np.amax(resp[:,:,:,sn]-mks_R[:,:,:,sn]) max_err_all[sn] = np.amax(resp[:,:,:,sn]-mks_R[:,:,:,sn]) avgr_fe = avgr_fe_tot/ns avgr_mks = avgr_mks_tot/ns MASE = MASE_tot/ns max_err = np.amax(resp-mks_R)/avgr_fe max_err_avg = max_err_sum/(ns * avgr_fe) msg = 'Average, %s%s, CPFEM: %s' %(typ,real_comp,avgr_fe) rr.WP(msg,wrt_file) msg = 'Average, %s%s, MKS: %s' %(typ,real_comp,avgr_mks) rr.WP(msg,wrt_file) msg = 'Standard deviation, %s%s, CPFEM: %s' %(typ,real_comp,np.std(resp)) rr.WP(msg,wrt_file) msg = 'Standard deviation, %s%s, MKS: %s' %(typ,real_comp,np.std(mks_R)) rr.WP(msg,wrt_file) resp_min = np.mean(np.amin(np.reshape(resp,[el**3,ns]), axis=0)) mks_R_min = np.mean(np.amin(np.reshape(mks_R,[el**3,ns]), axis=0)) resp_max = np.mean(np.amax(np.reshape(resp,[el**3,ns]), axis=0)) mks_R_max = np.mean(np.amax(np.reshape(mks_R,[el**3,ns]), axis=0)) msg = 'Mean minimum, %s%s, CPFEM: %s' %(typ,real_comp,resp_min) rr.WP(msg,wrt_file) msg = 'Mean minimum, %s%s, MKS: %s' %(typ,real_comp,mks_R_min) rr.WP(msg,wrt_file) msg = 'Mean maximum, %s%s, CPFEM: %s' %(typ,real_comp,resp_max) rr.WP(msg,wrt_file) msg = 'Mean maximum, %s%s, MKS: %s' %(typ,real_comp,mks_R_max) rr.WP(msg,wrt_file) msg = 'mean absolute error (MAE): %s%%' %(MASE*100) rr.WP(msg,wrt_file) msg = 'Maximum error in all samples: %s%%' %(max_err*100) rr.WP(msg,wrt_file) msg = 'Average maximum error over all samples: %s%%' %(max_err_avg*100) rr.WP(msg,wrt_file) resp_range_all = np.max(resp) - np.min(resp) mean_diff_meas = np.mean(resp-mks_R)/resp_range_all mean_max_diff_meas = np.mean(max_err_all)/resp_range_all max_diff_meas_all = np.amax(resp-mks_R)/resp_range_all msg = 'Mean voxel difference over all microstructures (divided by total FE response range): %s%%' %(mean_diff_meas*100) rr.WP(msg,wrt_file) msg = 'Average Maximum voxel difference per microstructure (divided by total FE response range): %s%%' %(mean_max_diff_meas*100) rr.WP(msg,wrt_file) msg = 'Maximum voxel difference in all microstructures (divided by total FE response range): %s%%' %(max_diff_meas_all*100) rr.WP(msg,wrt_file) ### VISUALIZATION OF MKS VS. FEM ### plt.close('all') ## pick a slice perpendicular to the x-direction slc = 11 sn = 20 ## find the min and max of both datasets for the slice of interest #(needed to scale both images the same) dmin = np.amin([resp[:,:,slc,sn],mks_R[:,:,slc,sn]]) dmax = np.amax([resp[:,:,slc,sn],mks_R[:,:,slc,sn]]) ## Plot slices of the response plt.figure(num=1,figsize=[12,4]) plt.subplot(121) ax = plt.imshow(mks_R[:,:,slc,sn], origin='lower', interpolation='none', cmap='jet', vmin=dmin, vmax=dmax) plt.colorbar(ax) plt.title('MKS $\%s_{%s}$ response, slice %s' %(typ,real_comp,slc)) plt.subplot(122) ax = plt.imshow(resp[:,:,slc,sn], origin='lower', interpolation='none', cmap='jet', vmin=dmin, vmax=dmax) plt.colorbar(ax) plt.title('CPFEM $\%s_{%s}$ response, slice %s' %(typ,real_comp,slc)) # Plot a histogram representing the frequency of strain levels with separate # channels for each phase of each type of response. plt.figure(num=2,figsize=[12,5]) ## find the min and max of both datasets (in full) dmin = np.amin([resp,mks_R]) dmax = np.amax([resp,mks_R]) fe = np.reshape(resp,ns*(el**3)) mks = np.reshape(mks_R,ns*(el**3)) # select the desired number of bins in the histogram bn = 40 weight = np.ones_like(fe)/(el**3) # FEM histogram n, bins, patches = plt.hist(fe, bins = bn, histtype = 'step', hold = True, range = (dmin, dmax), weights=weight, color = 'white') bincenters = 0.5*(bins[1:]+bins[:-1]) fe, = plt.plot(bincenters,n,'k', linestyle = '-', lw = 0.5) # 1st order terms MKS histogram n, bins, patches = plt.hist(mks, bins = bn, histtype = 'step', hold = True, range = (dmin, dmax), weights=weight, color = 'white') mks, = plt.plot(bincenters,n,'b', linestyle = '-', lw = 0.5) plt.grid(True) plt.legend([fe, mks], ["CPFEM response", "MKS predicted response"]) plt.xlabel("$\%s_{%s}$" %(typ,real_comp)) plt.ylabel("Number Fraction") plt.title("Frequency comparison of MKS and CPFEM $\%s_{%s}$ strain responses" %(typ,real_comp))
def results_all(ns_cal,ns,set_id,step,typ): ## el is the # of elements per side of the cube el = 21 ## specify the file to write messages to wrt_file = 'results_all_step%s_%s%s_%s.txt' %(step,ns,set_id,time.strftime("%Y-%m-%d_h%Hm%M")) ## vector of the indicial forms of the tensor components real_comp_desig = ['11','12','13','21','22','23','31','32','33','vm'] mks_R = np.zeros([el,el,el,10,ns]) resp = np.zeros([el,el,el,10,ns]) for comp in xrange(9): mks_R[:,:,:,comp,:] = np.load('mksR%s_%s%s_step%s_cal%s.npy' %(comp,ns,set_id,step,ns_cal)) resp[:,:,:,comp,:] = np.load('r%s_%s%s_s%s.npy' %(comp,ns,set_id,step)) resp[:,:,:,9,:] = np.sqrt( 0.5*( (resp[:,:,:,0,:]-resp[:,:,:,4,:])**2 +(resp[:,:,:,4,:]-resp[:,:,:,8,:])**2 + (resp[:,:,:,8,:]-resp[:,:,:,0,:])**2 + 6*(resp[:,:,:,5,:]**2 + resp[:,:,:,6,:]**2 + resp[:,:,:,1,:]**2) ) ) mks_R[:,:,:,9,:] = np.sqrt( 0.5*( (mks_R[:,:,:,0,:]-mks_R[:,:,:,4,:])**2 +(mks_R[:,:,:,4,:]-mks_R[:,:,:,8,:])**2 + (mks_R[:,:,:,8,:]-mks_R[:,:,:,0,:])**2 + 6*(mks_R[:,:,:,5,:]**2 + mks_R[:,:,:,6,:]**2 + mks_R[:,:,:,1,:]**2) ) ) mean_resp_vm = np.mean(resp[:,:,:,9,:]) for comp in xrange(10): real_comp = real_comp_desig[comp] ### WRITE HEADER TO FILE ### msg = '' rr.WP(msg,wrt_file) rr.WP(msg,wrt_file) msg = 'Results report for %s%s' %(typ,real_comp) rr.WP(msg,wrt_file) msg = '' rr.WP(msg,wrt_file) #### MEAN ABSOLUTE STRAIN ERROR (MASE) ### avgr_fe_tot = 0 avgr_mks_tot = 0 MASE_tot = 0 max_err_sum = 0 max_diff_all = np.zeros(ns) for sn in xrange(ns): [avgr_fe_indv,avgr_mks_indv, MASE_indv] = rr.eval_meas(mks_R[:,:,:,comp,sn], resp[:,:,:,comp,sn],el) avgr_fe_tot += avgr_fe_indv avgr_mks_tot += avgr_mks_indv MASE_tot += MASE_indv max_err_sum += np.amax(abs(resp[:,:,:,comp,sn]-mks_R[:,:,:,comp,sn])) max_diff_all[sn] = np.amax(abs(resp[:,:,:,comp,sn]-mks_R[:,:,:,comp,sn])) avgr_fe = avgr_fe_tot/ns avgr_mks = avgr_mks_tot/ns MASE = MASE_tot/ns max_err = np.amax(abs(resp[:,:,:,comp,:]-mks_R[:,:,:,comp,:]))/avgr_fe max_err_avg = max_err_sum/(ns * avgr_fe) msg = 'mean absolute error (MAE), %s%s: %s%%' %(typ,real_comp,MASE*100) rr.WP(msg,wrt_file) msg = 'Maximum error in all samples, %s%s: %s%%' %(typ,real_comp,max_err*100) rr.WP(msg,wrt_file) msg = 'Average maximum error over all samples, %s%s: %s%%' %(typ,real_comp,max_err_avg*100) rr.WP(msg,wrt_file) ### DIFFERENCE MEASURES ### mean_diff_meas = np.mean(abs(resp[:,:,:,comp,:]-mks_R[:,:,:,comp,:]))/mean_resp_vm mean_max_diff_meas = np.mean(max_diff_all)/mean_resp_vm max_diff_meas_all = np.amax(abs(resp[:,:,:,comp,:]-mks_R[:,:,:,comp,:]))/mean_resp_vm msg = 'Mean voxel difference over all microstructures (divided by mean von-Mises meas), %s%s: %s%%' %(typ,real_comp,mean_diff_meas*100) rr.WP(msg,wrt_file) msg = 'Average Maximum voxel difference per microstructure (divided by mean von-Mises meas), %s%s: %s%%' %(typ,real_comp,mean_max_diff_meas*100) rr.WP(msg,wrt_file) msg = 'Maximum voxel difference in all microstructures (divided by mean von-Mises meas), %s%s: %s%%' %(typ,real_comp,max_diff_meas_all*100) rr.WP(msg,wrt_file) ### STANDARD STATISTICS ### msg = 'Average, %s%s, CPFEM: %s' %(typ,real_comp,avgr_fe) rr.WP(msg,wrt_file) msg = 'Average, %s%s, MKS: %s' %(typ,real_comp,avgr_mks) rr.WP(msg,wrt_file) msg = 'Standard deviation, %s%s, CPFEM: %s' %(typ,real_comp,np.std(resp[:,:,:,comp,:])) rr.WP(msg,wrt_file) msg = 'Standard deviation, %s%s, MKS: %s' %(typ,real_comp,np.std(mks_R[:,:,:,comp,:])) rr.WP(msg,wrt_file) resp_min = np.mean(np.amin(np.reshape(resp[:,:,:,comp,:],[el**3,ns]), axis=0)) mks_R_min = np.mean(np.amin(np.reshape(mks_R[:,:,:,comp,:],[el**3,ns]), axis=0)) resp_max = np.mean(np.amax(np.reshape(resp[:,:,:,comp,:],[el**3,ns]), axis=0)) mks_R_max = np.mean(np.amax(np.reshape(mks_R[:,:,:,comp,:],[el**3,ns]), axis=0)) msg = 'Mean minimum, %s%s, CPFEM: %s' %(typ,real_comp,resp_min) rr.WP(msg,wrt_file) msg = 'Mean minimum, %s%s, MKS: %s' %(typ,real_comp,mks_R_min) rr.WP(msg,wrt_file) msg = 'Mean maximum, %s%s, CPFEM: %s' %(typ,real_comp,resp_max) rr.WP(msg,wrt_file) msg = 'Mean maximum, %s%s, MKS: %s' %(typ,real_comp,mks_R_max) rr.WP(msg,wrt_file)
def results_all(el, ns, set_id, step, typ): ## specify the file to write messages to wrt_file = 'results_all_step%s_%s%s_%s.txt' % ( step, ns, set_id, time.strftime("%Y-%m-%d_h%Hm%M")) ## vector of the indicial forms of the tensor components real_comp_desig = [ '11', '12', '13', '21', '22', '23', '31', '32', '33', 'vm' ] mks_R = np.zeros([el, el, el, 10, ns]) resp = np.zeros([el, el, el, 10, ns]) for comp in xrange(9): mks_R[:, :, :, comp, :] = np.load('r%s_%s%s_s%s.npy' % (comp, ns, set_id_old, step)) resp[:, :, :, comp, :] = np.load('r%s_%s%s_s%s.npy' % (comp, ns, set_id_new, step)) resp[:, :, :, 9, :] = np.sqrt( 0.5 * ((resp[:, :, :, 0, :] - resp[:, :, :, 4, :])**2 + (resp[:, :, :, 4, :] - resp[:, :, :, 8, :])**2 + (resp[:, :, :, 8, :] - resp[:, :, :, 0, :])**2 + 6 * (resp[:, :, :, 5, :]**2 + resp[:, :, :, 6, :]**2 + resp[:, :, :, 1, :]**2))) mks_R[:, :, :, 9, :] = np.sqrt( 0.5 * ((mks_R[:, :, :, 0, :] - mks_R[:, :, :, 4, :])**2 + (mks_R[:, :, :, 4, :] - mks_R[:, :, :, 8, :])**2 + (mks_R[:, :, :, 8, :] - mks_R[:, :, :, 0, :])**2 + 6 * (mks_R[:, :, :, 5, :]**2 + mks_R[:, :, :, 6, :]**2 + mks_R[:, :, :, 1, :]**2))) mean_resp_vm = np.mean(resp[:, :, :, 9, :]) for comp in xrange(10): real_comp = real_comp_desig[comp] ### WRITE HEADER TO FILE ### msg = '' rr.WP(msg, wrt_file) rr.WP(msg, wrt_file) msg = 'Results report for %s%s' % (typ, real_comp) rr.WP(msg, wrt_file) msg = '' rr.WP(msg, wrt_file) #### MEAN ABSOLUTE STRAIN ERROR (MASE) ### avgr_fe_tot = 0 avgr_mks_tot = 0 MASE_tot = 0 max_err_sum = 0 max_diff_all = np.zeros(ns) for sn in xrange(ns): [avgr_fe_indv, avgr_mks_indv, MASE_indv] = rr.eval_meas(mks_R[:, :, :, comp, sn], resp[:, :, :, comp, sn], el) avgr_fe_tot += avgr_fe_indv avgr_mks_tot += avgr_mks_indv MASE_tot += MASE_indv max_err_sum += np.amax( abs(resp[:, :, :, comp, sn] - mks_R[:, :, :, comp, sn])) max_diff_all[sn] = np.amax( abs(resp[:, :, :, comp, sn] - mks_R[:, :, :, comp, sn])) avgr_fe = avgr_fe_tot / ns avgr_mks = avgr_mks_tot / ns ### DIFFERENCE MEASURES ### mean_diff_meas = np.mean( abs(resp[:, :, :, comp, :] - mks_R[:, :, :, comp, :])) / mean_resp_vm mean_max_diff_meas = np.mean(max_diff_all) / mean_resp_vm max_diff_meas_all = np.amax( abs(resp[:, :, :, comp, :] - mks_R[:, :, :, comp, :])) / mean_resp_vm msg = 'Mean voxel difference over all microstructures (divided by mean von-Mises meas), %s%s: %s%%' % ( typ, real_comp, mean_diff_meas * 100) rr.WP(msg, wrt_file) msg = 'Average Maximum voxel difference per microstructure (divided by mean von-Mises meas), %s%s: %s%%' % ( typ, real_comp, mean_max_diff_meas * 100) rr.WP(msg, wrt_file) msg = 'Maximum voxel difference in all microstructures (divided by mean von-Mises meas), %s%s: %s%%' % ( typ, real_comp, max_diff_meas_all * 100) rr.WP(msg, wrt_file) ### STANDARD STATISTICS ### msg = 'Average, %s%s, CPFEM: %s' % (typ, real_comp, avgr_fe) rr.WP(msg, wrt_file) msg = 'Average, %s%s, MKS: %s' % (typ, real_comp, avgr_mks) rr.WP(msg, wrt_file) msg = 'Standard deviation, %s%s, CPFEM: %s' % ( typ, real_comp, np.std(resp[:, :, :, comp, :])) rr.WP(msg, wrt_file) msg = 'Standard deviation, %s%s, MKS: %s' % ( typ, real_comp, np.std(mks_R[:, :, :, comp, :])) rr.WP(msg, wrt_file) resp_min = np.mean( np.amin(np.reshape(resp[:, :, :, comp, :], [el**3, ns]), axis=0)) mks_R_min = np.mean( np.amin(np.reshape(mks_R[:, :, :, comp, :], [el**3, ns]), axis=0)) resp_max = np.mean( np.amax(np.reshape(resp[:, :, :, comp, :], [el**3, ns]), axis=0)) mks_R_max = np.mean( np.amax(np.reshape(mks_R[:, :, :, comp, :], [el**3, ns]), axis=0)) msg = 'Mean minimum, %s%s, CPFEM: %s' % (typ, real_comp, resp_min) rr.WP(msg, wrt_file) msg = 'Mean minimum, %s%s, MKS: %s' % (typ, real_comp, mks_R_min) rr.WP(msg, wrt_file) msg = 'Mean maximum, %s%s, CPFEM: %s' % (typ, real_comp, resp_max) rr.WP(msg, wrt_file) msg = 'Mean maximum, %s%s, MKS: %s' % (typ, real_comp, mks_R_max) rr.WP(msg, wrt_file) ### VISUALIZATION OF MKS VS. FEM ### plt.close('all') ## pick a slice perpendicular to the x-direction slc = 11 sn = 0 ## find the min and max of both datasets for the slice of interest #(needed to scale both images the same) dmin = np.amin([resp[:, :, slc, comp, sn], mks_R[:, :, slc, comp, sn]]) dmax = np.amax([resp[:, :, slc, comp, sn], mks_R[:, :, slc, comp, sn]]) ## Plot slices of the response plt.figure(num=2, figsize=[12, 4]) plt.subplot(121) ax = plt.imshow(mks_R[slc, :, :, comp, sn], origin='lower', interpolation='none', cmap='jet', vmin=dmin, vmax=dmax) plt.colorbar(ax) plt.title('MKS $\%s_{%s}$ response, slice %s' % (typ, real_comp, slc)) plt.subplot(122) ax = plt.imshow(resp[slc, :, :, comp, sn], origin='lower', interpolation='none', cmap='jet', vmin=dmin, vmax=dmax) plt.colorbar(ax) plt.title('CPFEM $\%s_{%s}$ response, slice %s' % (typ, real_comp, slc)) plt.savefig('field_step%s_comp%s_%s%s.png' % (step, comp, ns, set_id)) # Plot a histogram representing the frequency of strain levels with separate # channels for each phase of each type of response. plt.figure(num=3, figsize=[12, 5]) ## find the min and max of both datasets (in full) dmin = np.amin([resp[:, :, :, comp, :], mks_R[:, :, :, comp, :]]) dmax = np.amax([resp[:, :, :, comp, :], mks_R[:, :, :, comp, :]]) fe = np.reshape(resp[:, :, :, comp, :], ns * (el**3)) mks = np.reshape(mks_R[:, :, :, comp, :], ns * (el**3)) # select the desired number of bins in the histogram bn = 40 weight = np.ones_like(fe) / (el**3) # FEM histogram n, bins, patches = plt.hist(fe, bins=bn, histtype='step', hold=True, range=(dmin, dmax), weights=weight, color='white') bincenters = 0.5 * (bins[1:] + bins[:-1]) fe, = plt.plot(bincenters, n, 'k', linestyle='-', lw=0.5) # 1st order terms MKS histogram n, bins, patches = plt.hist(mks, bins=bn, histtype='step', hold=True, range=(dmin, dmax), weights=weight, color='white') mks, = plt.plot(bincenters, n, 'b', linestyle='-', lw=0.5) plt.grid(True) plt.legend([fe, mks], ["CPFEM response", "MKS predicted response"]) plt.xlabel("$\%s_{%s}$" % (typ, real_comp)) plt.ylabel("Number Fraction") plt.title( "Frequency comparison of MKS and CPFEM $\%s_{%s}$ strain responses" % (typ, real_comp)) plt.savefig('hist_step%s_comp%s_%s%s.png' % (step, comp, ns, set_id))