def errfunc(p, x, y): # Assume we have loads of cal data so we can # intepolate it to find values equiv to the # experimental points (using numpys numpy.interp) crit_stress = p[-1:] crit_temp = p[-2:-1] (xfine, yfine, sigma_p, zdiff, u_0, u_max, H_kp, Un, \ zdiff_kp, yderfine, sigma_b, sigma_p_index, kink_energy2) = kinker(x,y, G=60E9,silent=True,method=func,params=p[:-2] ) calc_y = (Un/(kink_energy2*2.0))*crit_temp calc_x = sigma_b + crit_stress # interpolate stresses at experimental data points. Note that we have to # do this backwards, as kinker returns solutions for increing stress, which # is decresing temp, and numpy.interp only likes things to increse (we check # this condition). We don't have to do anything special about high T results # as they get the low stress result (they are > max calc T) which is 0.1% of # sigma_P (in kinker) and this is equal to crit_stress (see above). if (np.all(np.diff(calc_y[::-1]) <= 0)): raise ValueError("Cannot intepolate on temperature") interp_calc_x = np.interp(T_n[::-1],calc_y[::-1],calc_x[::-1]) interp_calc_x = interp_calc_x[::-1] # Can we not do this in place above? # This is useful for debugging - draws a graph to check the interp... #interp_calc_y = T_n # Don't need this unless we are graphing. #plt.figure(5) #plt.plot(calc_y, calc_x, 'o', T_n, tau_n, '.', interp_calc_y, interp_calc_x, 'x') #plt.legend(('fit to expt data', 'expt data', 'init model')) #plt.title('Critical stress / kink energy') #plt.xlabel('Tau* (MPa)') #plt.ylabel('Un/2Uk*T_crit (K)') #plt.show() # Calculate error in stress. error = (tau_n-interp_calc_x) print "----------------------------------------" print "kink energy (J)" print kink_energy2 print "p stress (Pa)" print sigma_p print "Crit T (K)" print crit_temp print "Crit sigma (Pa)" print crit_stress print "Gnorm (Pa)" print (np.sqrt(sum((error*error))))/len(error) print "max error (Pa)" print max(error) print "time (s)" print (time.time() - t0) print "----------------------------------------" return error
# Load data set, x is the displacment (u) and y is the energy (U) #(x,y) = load_data(None) # For test data basename = raw_input("Basename (input is basname.dat): ") if (basename == ""): basename = 'example' filename = None else: filename = basename + '.dat' (x,y) = pot.load_data(filename) x_len = max(x) # Solve for inital potential using bspline interpolation... print "Solving kink nucleation problem with inital potential using bspline interp" (xfine_interp, yfine_interp, sigma_p, zdiff, u_0, u_max, H_kp, Un, \ zdiff_kp, yderfine, sigma_b, sigma_p_index, kink_energy2) = kinker(x,y,G=60E9) # Now do it again using a function... (func, p0) = pot.choose_func(params=True) # Fit this data to a sin function print "Fitting initial potential to function" opt_p = pot.fit_pot(p0, x, y, x_len, func) # Initial solution with this function (xfine_init, yfine, sigma_p, zdiff_init, u_0_init, u_max, H_kp, Un_init, \ zdiff_kp, yderfine_init, sigma_b_init, sigma_p_index, kink_energy2_init) = kinker(x,y,G=60E9,method=func,params=opt_p) # Load experimental data