def qmc(): """qMC implementation. """ instance = Instance({Operator.O_F: ['settings_out[]']}) while instance.reuse_instance(): # o_f settings0 = Settings({'test2': 14.4}) assert instance.is_connected('settings_out') assert instance.is_vector_port('settings_out') assert not instance.is_resizable('settings_out') length = instance.get_port_length('settings_out') assert length == 10 for slot in range(length): instance.send('settings_out', Message(0.0, None, settings0), slot)
def qmc_driver() -> None: """A driver for quasi-Monte Carlo Uncertainty Quantification. This component attaches to a collection of model instances, and feeds in different parameter values generated using a Sobol sequence. """ instance = Instance({ Operator.O_I: ['parameters_out[]'], Operator.S: ['states_in[]']}) while instance.reuse_instance(): # F_INIT # get and check parameter distributions n_samples = instance.get_setting('n_samples', 'int') d_min = instance.get_setting('d_min', 'float') d_max = instance.get_setting('d_max', 'float') k_min = instance.get_setting('k_min', 'float') k_max = instance.get_setting('k_max', 'float') if d_max < d_min: instance.error_shutdown('Invalid settings: d_max < d_min') exit(1) if k_max < k_min: instance.error_shutdown('Invalid settings: k_max < k_min') exit(1) # generate UQ parameter values sobol_sqn = sobol_seq.i4_sobol_generate(2, n_samples) ds = d_min + sobol_sqn[:, 0] * (d_max - d_min) ks = k_min + sobol_sqn[:, 1] * (k_max - k_min) # configure output port if not instance.is_resizable('parameters_out'): instance.error_shutdown('This component needs a resizable' ' parameters_out port, but it is connected to' ' something that cannot be resized. Maybe try' ' adding a load balancer.') exit(1) instance.set_port_length('parameters_out', n_samples) # run ensemble Us = None # O_I for sample in range(n_samples): uq_parameters = Settings({ 'd': ds[sample], 'k': ks[sample]}) msg = Message(0.0, None, uq_parameters) instance.send('parameters_out', msg, sample) # S for sample in range(n_samples): msg = instance.receive_with_settings('states_in', sample) U = np.array(msg.data) # accumulate if Us is None: Us = U else: Us = np.vstack((Us, U)) mean = np.mean(Us, axis=0) # O_F if 'DONTPLOT' not in os.environ: from matplotlib import pyplot as plt t_max = instance.get_setting('t_max', 'float') dt = instance.get_setting('dt', 'float') x_max = instance.get_setting('x_max', 'float') dx = instance.get_setting('dx', 'float') plt.figure() plt.imshow( np.log(Us + 1e-20), origin='upper', extent=[ -0.5*dx, x_max - 0.5*dx, n_samples-0.5, -0.5], interpolation='none', aspect='auto' ) cbar = plt.colorbar() cbar.set_label('log(Concentration)', rotation=270, labelpad=20) plt.xlabel('x') plt.ylabel('Sample') plt.title('Final states') plt.show()
def qmc_driver() -> None: """A driver for quasi-Monte Carlo Uncertainty Quantification. This component attaches to a collection of model instances, and feeds in different parameter values generated using a Sobol sequence. """ instance = Instance({ Operator.O_I: ['parameters_out[]'], Operator.S: ['states_in[]'] }) while instance.reuse_instance(): # F_INIT # get and check parameter distributions n_samples = instance.get_setting('n_samples', 'int') d_min = instance.get_setting('d_min', 'float') d_max = instance.get_setting('d_max', 'float') k_min = instance.get_setting('k_min', 'float') k_max = instance.get_setting('k_max', 'float') if d_max < d_min: instance.error_shutdown('Invalid settings: d_max < d_min') exit(1) if k_max < k_min: instance.error_shutdown('Invalid settings: k_max < k_min') exit(1) # generate UQ parameter values sobol_sqn = sobol_seq.i4_sobol_generate(2, n_samples) ds = d_min + sobol_sqn[:, 0] * (d_max - d_min) ks = k_min + sobol_sqn[:, 1] * (k_max - k_min) # configure output port if not instance.is_resizable('parameters_out'): instance.error_shutdown( 'This component needs a resizable' ' parameters_out port, but it is connected to' ' something that cannot be resized. Maybe try' ' adding a load balancer.') exit(1) instance.set_port_length('parameters_out', n_samples) # run ensemble Us = None # O_I for sample in range(n_samples): uq_parameters = Settings({'d': ds[sample], 'k': ks[sample]}) msg = Message(0.0, None, uq_parameters) instance.send('parameters_out', msg, sample) # S for sample in range(n_samples): msg = instance.receive_with_settings('states_in', sample) U = np.array(msg.data) # accumulate if Us is None: Us = U else: Us = np.vstack((Us, U)) mean = np.mean(Us, axis=0) plt.figure() plt.imshow(np.log(Us + 1e-20)) plt.show()