Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
def validation_procedure(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 cube
    el = 21
    ## H is the number of GSH coefficients
    H = 15

    M = np.load('M_%s%s_s%s.npy' % (ns_val, set_id_val, step))
    specinfc = np.load('specinfc%s_%s%s_s%s.npy' %
                       (comp, ns_cal, set_id_cal, step))

    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_s%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)
Ejemplo n.º 4
0
def micr_func(ns, set_id, step, 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_s%s.npy' % (ns, set_id, step))
    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_s%s' % (ns, set_id, step), M)

    end = time.time()
    timeE = np.round((end - start), 3)

    msg = "FFT3 conversion of micr to M_%s%s_s%s: %s seconds" % (ns, set_id,
                                                                 step, timeE)
    rr.WP(msg, wrt_file)
    msg = 'Size of M_%s%s_s%s: %s bytes' % (ns, set_id, step, size)
    rr.WP(msg, wrt_file)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
def euler_to_gsh(ns,set_id,step,wrt_file):

    start = time.time()

    el = 21
    H = 15    
    
    euler = np.load('euler_%s%s_s%s.npy' %(ns,set_id,step))
    
    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_s%s.npy' %(ns,set_id,step),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)
Ejemplo n.º 7
0
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))
Ejemplo n.º 8
0
def results_all(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_s%s.npy' %(comp,ns,set_id,step))
        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)
Ejemplo n.º 9
0
def results_all(ns, set_id, typ):

    ## el is the # of elements per side of the cube
    el = 21
    ## specify the file to write messages to
    wrt_file = 'results_all_%s%s_%s.txt' % (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.npy' % (comp, ns, set_id))
        resp[:, :, :, comp, :] = np.load('r%s_%s%s.npy' % (comp, ns, set_id))

    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)

        ### 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, 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=1, 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_comp%s_%s%s.png' % (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=2, 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_comp%s_%s%s.png' % (comp, ns, set_id))