#Calculate maximum displacement samples using MC simulation disp_samples = np.zeros(num_samples) for i, stiff in enumerate(stiffness_samples): disp_samples[i] = model.get_max_disp(stiff) #Get Monte carlo solution as a sample-based random variable: mc_solution = SampleRandomVector(disp_samples) #-------------SROM----------------------- #generate SROM for random stiffness sromsize = 10 dim = 1 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)
import numpy as np from SROMPy.postprocess import Postprocessor from SROMPy.srom import SROM from SROMPy.target import SampleRandomVector ''' Generate SROM to model input distribution (samples) ''' #Specify input/output files and SROM optimization parameters dim = 3 srom_size = 20 samplesfile = "mc_data/input_samples_MC.txt" outfile = "srom_data/srom_m" + str(srom_size) + ".txt" #Define target random variable from samples MCsamples = np.genfromtxt(samplesfile) target = SampleRandomVector(MCsamples) #Define SROM, determine optimal parameters, store parameters srom = SROM(srom_size, dim) srom.optimize(target, weights=[1, 1, 1], error="SSE", num_test_samples=100) #NOTE - commented out to not overwrite paper data files: #srom.save_params(outfile) #Check out the CDFs pp = Postprocessor(srom, target) pp.compare_CDFs(variablenames=[r'$y_{0}$', r'log$C$', r'$n$'])
# # In above example have two tests both with 60 samples; one with 2 cpus and one with 3 cpus. if num_test_samples not in performance_data: performance_data[num_test_samples] = {} iteration_performance = [] for i in xrange( 10 ): # Repeat performance data collection in order to smooth the chart lines. input_srom = SROM(20, 1) t0 = time.time() input_srom.optimize(random_variable, num_test_samples=num_test_samples, verbose=False) iteration_performance.append(time.time() - t0) mean_performance = np.mean(iteration_performance) if comm.rank != 0: exit() performance_data[num_test_samples][comm.size] = mean_performance print '%s test samples across %s CPUs took %s seconds.' % ( num_test_samples, comm.size, float(mean_performance)) print 'Writing results to disk...' print performance_data
# Copyright 2018 United States Government as represented by the Administrator of # the National Aeronautics and Space Administration. No copyright is claimed in # the United States under Title 17, U.S. Code. All Other Rights Reserved. # The Stochastic Reduced Order Models with Python (SROMPy) platform is licensed # under the Apache License, Version 2.0 (the "License"); you may not use this # file except in compliance with the License. You may obtain a copy of the # License at http://www.apache.org/licenses/LICENSE-2.0. # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. from SROMPy.postprocess import Postprocessor from SROMPy.srom import SROM from SROMPy.target import NormalRandomVariable #Initialize Normal random variable object to be modeled by SROM: normal = NormalRandomVariable(mean=3., std_dev=1.5) #Initialize SROM & optimize to model the normal random variable: srom = SROM(size=10, dim=1) srom.optimize(normal) #Compare the CDF of the SROM & target normal variable: pp = Postprocessor(srom, normal) pp.compare_CDFs()
displacement_samples = np.zeros(num_samples) for i, stiff in enumerate(stiffness_samples): displacement_samples[i] = model.evaluate([stiff]) # Get Monte carlo solution as a sample-based random variable: monte_carlo_solution = SampleRandomVector(displacement_samples) # -------------SROM----------------------- print "Generating SROM for input (stiffness)..." # Generate SROM for random stiffness. srom_size = 10 dim = 1 input_srom = SROM(srom_size, dim) input_srom.optimize(stiffness_random_variable) # Compare SROM vs target stiffness distribution: pp_input = Postprocessor(input_srom, stiffness_random_variable) pp_input.compare_cdfs() print "Computing piecewise constant SROM approximation for output (max disp)..." # 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.evaluate([stiff]) # Form new SROM for the max disp. solution using samples from the model. output_srom = SROM(srom_size, dim)
#Get Monte carlo solution as a sample-based random variable: mc_solution = SampleRandomVector(disp_samples) #-------------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) #----------------------------------------
import numpy from os import path from SROMPy.postprocess import Postprocessor from SROMPy.srom import SROM, SROMSurrogate from SROMPy.target import SampleRandomVector # Define target random vector from samples. monte_carlo_input_samples_filename = path.join("mc_data", "input_samples_MC.txt") monte_carlo_input_samples = numpy.genfromtxt(monte_carlo_input_samples_filename) target_vector = SampleRandomVector(monte_carlo_input_samples) # Define SROM and determine optimal parameters. srom_size = 20 input_srom = SROM(size=srom_size, dim=3) input_srom.optimize(target_vector) # Compare the input CDFs (produces Figure 6). post_processor = Postprocessor(input_srom, target_vector) post_processor.compare_cdfs(variable_names= [r'$y_{0}$', r'log$C$', r'$n$']) # Run the model for each input SROM sample: srom_results = numpy.zeros(srom_size) (srom_samples, srom_probabilities) = input_srom.get_params() # TODO: define model here. model = None if model is None: raise ValueError("model has not been defined.")