from SROMPy.target import SampleRandomVector ''' Ex3 - unimodal 3D Script to generate PW constant SROM approximation to EOL and compare it with the monte carlo solution (w/ surrogate model) ''' mc_eol_file = "mc_data/eol_samples_MC.txt" srom_size = 20 srom_eol_file = "srom_data/srom_eol_m" + str(srom_size) + ".txt" srom_input_file = "srom_data/srom_m" + str(srom_size) + ".txt" # Get MC EOL samples. MC_eols = np.genfromtxt(mc_eol_file) # Get SROM EOL samples & probabilities from input srom. srom_end_of_life_data = np.genfromtxt(srom_eol_file) # Probabilities in last column. srom_probabilities = np.genfromtxt(srom_input_file)[:, -1] # Make MC random variable & SROM to compare. end_of_life_srom = SROM(srom_size, dim=1) end_of_life_srom.set_params(srom_end_of_life_data, srom_probabilities) end_of_life_monte_carlo = SampleRandomVector(MC_eols) pp = Postprocessor(end_of_life_srom, end_of_life_monte_carlo) pp.compare_cdfs(variable_names=["EOL"])
#-------------SROM----------------------- #generate SROM for random vector of stiffness & mass sromsize = 25 dim = 2 #Assume we only have access to samples in this example and want SROM from them: km_samples = np.array([k_samples, m_samples]).T km_random_vector = SampleRandomVector(km_samples) srom = SROM(sromsize, dim) srom.optimize(km_random_vector) (samples, probs) = srom.get_params() #Run model to get max disp for each SROM stiffness sample srom_disps = np.zeros(sromsize) for i in range(sromsize): k = samples[i, 0] m = samples[i, 1] srom_disps[i] = model.get_max_disp(k, m) #Form new SROM for the max displacement solution using samples from the model srom_solution = SROM(sromsize, 1) srom_solution.set_params(srom_disps, probs) #---------------------------------------- #Compare solutions pp = Postprocessor(srom_solution, mc_solution) pp.compare_CDFs()
input_srom = SROM(sromsize, dim) input_srom.optimize(stiffness_rv) #Compare SROM vs target stiffness distribution: pp_input = Postprocessor(input_srom, stiffness_rv) pp_input.compare_CDFs() #Run model to get max disp for each SROM stiffness sample srom_disps = np.zeros(sromsize) (samples, probs) = input_srom.get_params() for i, stiff in enumerate(samples): srom_disps[i] = model.get_max_disp(stiff) #Form new SROM for the max disp. solution using samples from the model output_srom = SROM(sromsize, dim) output_srom.set_params(srom_disps, probs) #Compare solutions pp_output = Postprocessor(output_srom, mc_solution) pp_output.compare_CDFs() #--------Piecewise LINEAR surrogate with gradient info------- #Need to calculate gradient of output wrt input samples first #Perturbation size for finite difference stepsize = 1e-12 samples_fd = FD.get_perturbed_samples(samples, perturb_vals=[stepsize]) #Run model to get perturbed outputs for FD calc. perturbed_disps = np.zeros(sromsize)
input_srom = SROM(srom_size, dim) input_srom.optimize(displacement_samples) # Compare SROM vs target stiffness distribution: pp_input = Postprocessor(input_srom, displacement_samples) pp_input.compare_cdfs() # Run model to get max displacement for each SROM stiffness sample. srom_displacements = np.zeros(srom_size) (samples, probabilities) = input_srom.get_params() for i, stiff in enumerate(samples): srom_displacements[i] = model.get_max_disp(stiff) # Form new SROM for the max disp. solution using samples from the model. output_srom = SROM(srom_size, dim) output_srom.set_params(srom_displacements, probabilities) # Compare solutions. pp_output = Postprocessor(output_srom, monte_carlo_solution) pp_output.compare_cdfs() # --------Piecewise LINEAR surrogate with gradient info------- # Need to calculate gradient of output wrt input samples first. # Perturbation size for finite difference. step_size = 1e-12 samples_fd = FD.get_perturbed_samples(samples, perturbation_values=[step_size]) # Run model to get perturbed outputs for FD calc. perturbed_displacements = np.zeros(srom_size)
from SROMPy.postprocess import Postprocessor from SROMPy.srom import SROM from SROMPy.target import SampleRandomVector ''' Script to generate piecewise constant SROM approximation to EOL and compare it with the Monte Carlo solution - step 3. Uses the stored EOL model outputs from step 2 and the stored input SROM from step 1. ''' mc_eol_file = "mc_data/eol_samples_MC.txt" sromsize = 20 srom_eol_file = "srom_data/srom_eol_m" + str(sromsize) + ".txt" srom_input_file = "srom_data/srom_m" + str(sromsize) + ".txt" #Get MC EOL samples MC_eols = np.genfromtxt(mc_eol_file) #Get SROM EOL samples & probabilities from input srom srom_eols = np.genfromtxt(srom_eol_file) srom_probs = np.genfromtxt(srom_input_file)[:, -1] #probs in last column #Make MC random variable & SROM to compare eol_srom = SROM(sromsize, dim=1) eol_srom.set_params(srom_eols, srom_probs) eol_mc = SampleRandomVector(MC_eols) pp = Postprocessor(eol_srom, eol_mc) pp.compare_CDFs(variablenames=["EOL"])