def demo(): m_la, std_la = 10., 1.0 m_xi, std_xi = 1.0, 0.1 # discretize the control variable (x-axis) e_arr = np.linspace(0, 2.0, 80) #=========================================================================== # Randomization #=========================================================================== s = SPIRRID( q=fiber_tt_2p(), sampling_type='TGrid', codegen_type='weave', e_arr=e_arr, n_int=10, theta_vars=dict(la=RV('norm', m_la, std_la), xi=RV('norm', m_xi, std_xi)), ) #print current configuration of the integrator #print s.__str__() # print code of the weave (or cython) implementation print s.codegen.code
def create_demo_object(fig_output_dir='fig'): #=========================================================================== # Control variable #=========================================================================== e_arr = np.linspace(0, 0.012, 80) powers = np.linspace(1, math.log(20, 10), 6) n_int_range = np.array(np.power(10, powers), dtype=int) #=========================================================================== # Randomization #=========================================================================== theta_vars = dict( fu=RV('weibull_min', 1200.0e6, 200.), qf=1500.0, # qf = RV('uniform', 1500., 100.), L=0.02, # # L = RV('uniform', 0.02, 0.02 / 2.), A=RV('norm', 5.30929158457e-10, .03 * 5.30929158457e-10), E_mod=RV('uniform', 70.e9, 250.e9), z=RV('uniform', 0.0, 0.03), phi=0.0, # # phi = RV('cos_distr', 0.0, 1.0), # phi = RV('uniform', 0.0, 1.0), f=RV('uniform', 0.0, 0.03)) #=========================================================================== # Integrator object #=========================================================================== s = SPIRRID( q=ConstantFrictionFiniteFiber(), e_arr=e_arr, n_int=10, theta_vars=theta_vars, ) #=========================================================================== # Lab #=========================================================================== slab = SPIRRIDLAB(s=s, save_output=False, show_output=True, dpi=300, fig_output_dir=fig_output_dir, qname='fiber_po_8p', plot_mode='subplots', n_int_range=n_int_range, extra_compiler_args=True, le_sampling_lst=['LHS', 'PGrid'], le_n_int_lst=[10, 10], plot_sampling_idx=[ 0, 3, ]) return slab
def create_demo_object(fig_output_dir='fig'): m_la, std_la = 10., 1.0 m_xi, std_xi = 1.0, 0.1 # discretize the control variable (x-axis) e_arr = np.linspace(0, 2.0, 80) # n_int range for sampling efficiency test powers = np.linspace(1, math.log(500, 10), 50) n_int_range = np.array(np.power(10, powers), dtype=int) #=========================================================================== # Randomization #=========================================================================== s = SPIRRID( q=fiber_tt_2p(), sampling_type='TGrid', codegen_type='cython', e_arr=e_arr, n_int=10, theta_vars=dict(la=RV('norm', m_la, std_la), xi=RV('norm', m_xi, std_xi)), ) #=========================================================================== # Exact solution #=========================================================================== def mu_q_ex(e, m_xi, std_xi, m_la): return e * (0.5 - 0.5 * erf(0.5 * math.sqrt(2) * (e - m_xi) / std_xi)) * m_la #=========================================================================== # Lab #=========================================================================== slab = SPIRRIDLAB(s=s, save_output=False, show_output=True, dpi=300, fig_output_dir=fig_output_dir, exact_arr=mu_q_ex(e_arr, m_xi, std_xi, m_la), plot_mode='subplots', n_int_range=n_int_range, extra_compiler_args=True, le_sampling_lst=['LHS', 'PGrid'], le_n_int_lst=[440, 5000]) return slab
def run(): m_la, std_la = 10., 1.0 m_xi, std_xi = 1.0, 0.1 # discretize the control variable (x-axis) e_arr = np.linspace(0, 2.0, 80) #=========================================================================== # Randomization #=========================================================================== s = SPIRRID( q=fiber_tt_2p(), e_arr=e_arr, n_int=10, theta_vars=dict( la=m_la, #la = RV('norm', m_la, std_la), #xi = m_xi, xi=RV('norm', m_xi, std_xi)), sampling_type='MCS', codegen_type='numpy', ) p.plot(e_arr, s.mu_q_arr, 'b-x') s.codegen_type = 'weave' p.plot(e_arr, s.mu_q_arr, 'r-') p.show()
def _redraw(self): s = SPIRRID( q=self.rf, sampling_type='LHS', e_arr=self.e_arr, n_int=self.n_G_ipts, theta_vars=dict(tau_fr=2.6, l=0.0, d=25.5e-3, E_mod=72.0e3, theta=0.0, xi=0.0179, phi=1., L=30.0), # codegen_type='weave' ) # construct the random variables if self.pdf_xi_on: s.theta_vars['xi'] = RV( 'weibull_min', shape=4.54, scale=0.017 ) # RV( pd = self.pdf_xi, name = 'xi', n_int = self.n_G_ipts ) print self.pdf_theta.interp_ppf([0.01, 0.02]) print YMB_RV('theta', distr=self.pdf_theta, n_int=self.n_G_ipts).distr.interp_ppf([0.01, 0.02]) if self.pdf_theta_on: s.theta_vars['theta'] = YMB_RV('theta', distr=self.pdf_theta, n_int=self.n_G_ipts) if self.pdf_l_on: s.theta_vars['l'] = YMB_RV('l', distr=self.pdf_l, n_int=self.n_G_ipts) if self.pdf_phi_on: s.theta_vars['phi'] = YMB_RV('phi', distr=self.pdf_phi, n_int=self.n_G_ipts) # print 'checking unity', s.mu_q_arr() mu = s.mu_q_arr axes = self.figure.axes[0] # TODO: axes.plot(self.e_arr, mu * self.n_f, linewidth=2, label=self.lab) axes.set_xlabel('crack opening w[mm]') axes.set_ylabel('force P[N]') axes.legend(loc='best') self.data_changed = True
def create_demo_object(): #=========================================================================== # Control variable #=========================================================================== e_arr = np.linspace(0, 0.012, 80) #=========================================================================== # Randomization #=========================================================================== theta_vars = dict(#fu = 1200.0e6, fu = RV('weibull_min', 1200.0e6, 200.), qf = RV('uniform', 1500., 100.), #qf = 1500.0, L = 0.02, # # L = RV('uniform', 0.02, 0.02 / 2.), A = 5.30929158457e-10, #A = RV('norm', 5.30929158457e-10, .03 * 5.30929158457e-10), E_mod = 70.0e9, #E_mod = RV('uniform', 70.e9, 250.e9), z = 0, #z = RV('uniform', 0.0, 0.03), phi = 0.0, # # phi = RV('cos_distr', 0.0, 1.0), # phi = RV('uniform', 0.0, 1.0), f = 0.01, #f = RV('uniform', 0.0, 0.03), ) #=========================================================================== # Integrator object #=========================================================================== s = SPIRRID(q = ConstantFrictionFiniteFiber(), e_arr = e_arr, n_int = 10, theta_vars = theta_vars, ) import pylab as p p.plot(e_arr, s.mu_q_arr) p.show()
def setup_class(cls): np.random.seed(2356) #=========================================================================== # Control variable #=========================================================================== e_arr = np.linspace(0, 0.012, 80) cls.m_la, cls.std_la = 10., 1.0 cls.m_xi, cls.std_xi = 1.0, 0.1 #=========================================================================== # Randomization #=========================================================================== cls.s = SPIRRID(q = fiber_tt_2p(), eps_vars = {'e':e_arr}, codegen_type = 'numpy', n_int = 10, theta_vars = dict(la = RV('norm', cls.m_la, cls.std_la), xi = RV('norm', cls.m_xi, cls.std_xi) ), )
def create_demo_object(fig_output_dir='fig'): D = 26 * 1.0e-6 # m A = (D / 2.0)**2 * math.pi # set the mean and standard deviation of the two random variables la_mean, la_stdev = 0.0, 0.2 xi_mean, xi_stdev = 0.019027, 0.0022891 E_mean, E_stdev = 70.0e+9, 15.0e+9 th_mean, th_stdev = 0.0, 0.01 A_mean, A_stdev = A * 0.3, 0.7 * A do = 'norm' if do == 'general': # set the mean and standard deviation of the two random variables la_mean, la_stdev = 0.0, 0.2 xi_mean, xi_stdev = 0.019027, 0.0022891 E_mean, E_stdev = 70.0e+9, 15.0e+9 th_mean, th_stdev = 0.0, 0.01 A_mean, A_stdev = A * 0.3, 0.7 * A # construct the normal distributions and get the methods # for the evaluation of the probability density functions g_la = RV('uniform', la_mean, la_stdev) g_xi = RV('norm', xi_mean, xi_stdev) g_E = RV('uniform', E_mean, E_stdev) g_th = RV('uniform', th_mean, th_stdev) g_A = RV('uniform', A_mean, A_stdev) mu_ex_file = 'fiber_tt_5p_30.txt' delimiter = ',' elif do == 'uniform': # set the mean and standard deviation of the two random variables la_mean, la_stdev = 0.0, 0.2 xi_mean, xi_stdev = 0.01, 0.02 E_mean, E_stdev = 70.0e+9, 15.0e+9 th_mean, th_stdev = 0.0, 0.01 A_mean, A_stdev = A * 0.3, 0.7 * A # construct the uniform distributions and get the methods # for the evaluation of the probability density functions g_la = RV('uniform', la_mean, la_stdev) g_xi = RV('uniform', xi_mean, xi_stdev) g_E = RV('uniform', E_mean, E_stdev) g_th = RV('uniform', th_mean, th_stdev) g_A = RV('uniform', A_mean, A_stdev) mu_ex_file = 'fiber_tt_5p_n_int_40_norm_exact.txt' # 'fiber_tt_5p_40_unif.txt' delimiter = ' ' elif do == 'norm': # set the mean and standard deviation of the two random variables la_mean, la_stdev = 0.1, 0.02 xi_mean, xi_stdev = 0.019027, 0.0022891 E_mean, E_stdev = 70.0e+9, 15.0e+9 th_mean, th_stdev = 0.005, 0.001 A_mean, A_stdev = 5.3e-10, 1.0e-11 # construct the normal distributions and get the methods # for the evaluation of the probability density functions g_la = RV('norm', la_mean, la_stdev) g_xi = RV('norm', xi_mean, xi_stdev) g_E = RV('norm', E_mean, E_stdev) g_th = RV('norm', th_mean, th_stdev) g_A = RV('norm', A_mean, A_stdev) mu_ex_file = os.path.join(file_dir, 'fiber_tt_5p_n_int_40_norm_exact.txt') delimiter = ' ' # discretize the control variable (x-axis) e_arr = np.linspace(0, 0.04, 40) # n_int range for sampling efficiency test powers = np.linspace(1, math.log(20, 10), 10) n_int_range = np.array(np.power(10, powers), dtype=int) #=========================================================================== # Randomization #=========================================================================== s = SPIRRID( q=fiber_tt_5p(), codegen_type='cython', e_arr=e_arr, n_int=10, theta_vars=dict(lambd=g_la, xi=g_xi, E_mod=g_E, theta=g_th, A=g_A), ) # Exact solution def mu_q_ex(e): data = np.loadtxt(mu_ex_file, delimiter=delimiter) x, y = data[:, 0], data[:, 1] f = interp1d(x, y, kind='linear') return f(e) #=========================================================================== # Lab #=========================================================================== slab = SPIRRIDLAB(s=s, save_output=False, show_output=True, dpi=300, fig_output_dir=fig_output_dir, exact_arr=mu_q_ex(e_arr), plot_mode='subplots', n_int_range=n_int_range, extra_compiler_args=True, le_sampling_lst=['LHS', 'PGrid'], le_n_int_lst=[25, 30]) return slab
def main(): #=========================================================================== # Response function #=========================================================================== class fiber_tt_5p_np(RF): ''' Response function of a single fiber ''' implements(IRF) title = Str('brittle filament') def __call__(self, eps, lambd, xi, E_mod, theta, A): ''' Implements the response function with arrays as variables. first extract the variable discretizations from the orthogonal grid. ''' eps_ = (eps - theta * (1 + lambd)) / ((1 + theta) * (1 + lambd)) eps_ *= Heaviside(eps_) eps_grid = eps_ * Heaviside(xi - eps_) q_grid = E_mod * A * eps_grid return q_grid class fiber_tt_5p_ne(RF): ''' Response function of a single fiber ''' implements(IRF) title = Str('brittle filament') def __call__(self, eps, lambd, xi, E_mod, theta, A): ''' Implements the response function with arrays as variables. first extract the variable discretizations from the orthogonal grid. ''' eps_ = ne.evaluate( "((eps - theta * (1 + lambd)) / ((1 + theta) * (1 + lambd)))") tmp = Heaviside_ne(eps_) eps_ = ne.evaluate('eps_*tmp') tmp = Heaviside_ne(ne.evaluate("(xi - eps_)")) eps_grid = ne.evaluate("eps_ * tmp") q_grid = ne.evaluate("E_mod * A * eps_grid") return q_grid # all in one row, slower alternative #return ne.evaluate("E_mod * A *((eps - theta * (1 + lambd)) / ((1 + theta) * (1 + lambd))) * (((eps - theta * (1 + lambd)) / ((1 + theta) * (1 + lambd)))>=0) * ((xi-((eps - theta * (1 + lambd)) / ((1 + theta) * (1 + lambd))))>=0)") # set the mean and standard deviation of the two random variables la_mean, la_stdev = 0.1, 0.02 xi_mean, xi_stdev = 0.019027, 0.0022891 E_mean, E_stdev = 70.0e+9, 15.0e+9 th_mean, th_stdev = 0.005, 0.001 A_mean, A_stdev = 5.3e-10, 1.0e-11 # construct the normal distributions and get the methods # for the evaluation of the probability density functions g_la = RV('norm', la_mean, la_stdev) g_xi = RV('norm', xi_mean, xi_stdev) g_E = RV('norm', E_mean, E_stdev) g_th = RV('norm', th_mean, th_stdev) g_A = RV('norm', A_mean, A_stdev) # discretize the control variable (x-axis) e_arr = np.linspace(0, 0.04, 80) #=========================================================================== # Randomization #=========================================================================== s_ne = SPIRRID( q=fiber_tt_5p_ne(), e_arr=e_arr, n_int=10, theta_vars=dict(lambd=g_la, xi=g_xi, E_mod=g_E, theta=g_th, A=g_A), ) s_np = SPIRRID( q=fiber_tt_5p_np(), e_arr=e_arr, n_int=10, theta_vars=dict(lambd=g_la, xi=g_xi, E_mod=g_E, theta=g_th, A=g_A), ) print 'Evaluation using NumPy' print 'numpy time', s_np.exec_time print 'Evaluation using numexpr' print 'numexpr time', s_ne.exec_time p.plot(e_arr, s_np.mu_q_arr, label='numpy') p.plot(e_arr, s_ne.mu_q_arr, label='numexpr') p.legend() p.show()