def test_dimensionReduction(self):

        systemsize = 4
        eigen_decayrate = 2.0

        # Create Hadmard Quadratic object
        QoI = examples.HadamardQuadratic(systemsize, eigen_decayrate)

        # Create stochastic collocation object
        collocation = StochasticCollocation(3, "Normal")

        # Create dimension reduction object
        threshold_factor = 0.9
        dominant_space = DimensionReduction(threshold_factor=threshold_factor,
                                            exact_Hessian=True)

        # Initialize chaospy distribution
        std_dev = 0.2 * np.ones(QoI.systemsize)
        x = np.ones(QoI.systemsize)
        jdist = cp.MvNormal(x, np.diag(std_dev))

        # Get the eigenmodes of the Hessian product and the dominant indices
        dominant_space.getDominantDirections(QoI, jdist)
        true_eigenvals = np.array([0.08, 0.02, 0.005, 0.00888888888888889])
        err_eigenvals = abs(dominant_space.iso_eigenvals - true_eigenvals)

        true_eigenvecs = np.array([[0.5, 0.5, -0.5, -0.5],
                                   [0.5, -0.5, 0.5,
                                    -0.5], [0.5, 0.5, 0.5, 0.5],
                                   [0.5, -0.5, -0.5, 0.5]])
        err_eigenvecs = abs(dominant_space.iso_eigenvecs - true_eigenvecs)

        self.assertTrue((err_eigenvals < 1.e-15).all())
        self.assertTrue((err_eigenvecs < 1.e-15).all())
        self.assertEqual(dominant_space.dominant_indices, [0, 1])
def run_hadamard(systemsize, eigen_decayrate, std_dev, n_eigenmodes):
    n_collocation_pts = 2

    # Create Hadmard Quadratic object
    QoI = examples.HadamardQuadratic(systemsize, eigen_decayrate)

    # # Create stochastic collocation object
    # collocation = StochasticCollocation(n_collocation_pts, "Normal")

    # Initialize chaospy distribution
    x = np.random.rand(QoI.systemsize)
    jdist = cp.MvNormal(x, np.diag(std_dev))

    threshold_factor = 0.5
    dominant_space = DimensionReduction(threshold_factor=threshold_factor,
                                        exact_Hessian=False,
                                        n_arnoldi_sample=71,
                                        min_eigen_accuracy=1.e-2)
    dominant_space.getDominantDirections(QoI,
                                         jdist,
                                         max_eigenmodes=n_eigenmodes)

    # if systemsize == 64:
    # print('x = \n', repr(x))
    # print('std_dev = \n', repr(std_dev))
    # print('iso_eigenvals = ', dominant_space.iso_eigenvals)
    # print("dominant_indices = ", dominant_space.dominant_indices)

    # Collocate
    # collocation = StochasticCollocation(n_collocation_pts, "Normal")
    # mu_j = collocation.normal.reduced_mean(QoI.eval_QoI, jdist, dominant_space)
    # print "mu_j = ", mu_j

    QoI_dict = {
        'Hadamard': {
            'QoI_func': QoI.eval_QoI,
            'output_dimensions': 1,
        },
    }
    sc_obj = StochasticCollocation2(jdist,
                                    n_collocation_pts,
                                    'MvNormal',
                                    QoI_dict,
                                    include_derivs=False,
                                    reduced_collocation=True,
                                    dominant_dir=dominant_space.dominant_dir)
    sc_obj.evaluateQoIs(jdist)
    mu_j_dict = sc_obj.mean(of=['Hadamard'])
    mu_j = mu_j_dict['Hadamard']

    # Evaluate the analytical value of the Hadamard Quadratic
    covariance = cp.Cov(jdist)
    mu_analytic = QoI.eval_analytical_QoI_mean(x, covariance)
    # print "mu_analytic = ", mu_analytic

    relative_error = np.linalg.norm((mu_j - mu_analytic) / mu_analytic)
    # print "relative_error = ", relative_error

    return relative_error
Exemplo n.º 3
0
def run_hadamard(systemsize, eigen_decayrate, std_dev, n_sample):
    # n_collocation_pts = 2

    # Create Hadmard Quadratic object
    QoI = examples.HadamardQuadratic(systemsize, eigen_decayrate)

    # Create stochastic collocation object
    # collocation = StochasticCollocation(n_collocation_pts, "Normal")

    # Initialize chaospy distribution
    x = np.random.randn(QoI.systemsize)
    jdist = cp.MvNormal(x, np.diag(std_dev))

    threshold_factor = 0.5
    dominant_space_exact = DimensionReduction(
        threshold_factor=threshold_factor, exact_Hessian=True)
    dominant_space = DimensionReduction(threshold_factor=threshold_factor,
                                        exact_Hessian=False,
                                        n_arnoldi_sample=n_sample)

    dominant_space.getDominantDirections(QoI, jdist, max_eigenmodes=20)
    dominant_space_exact.getDominantDirections(QoI, jdist)

    # Sort the exact eigenvalues in descending order
    sort_ind = dominant_space_exact.iso_eigenvals.argsort()[::-1]

    # Compare the eigenvalues of the 10 most dominant spaces
    lambda_exact = dominant_space_exact.iso_eigenvals[sort_ind]
    error_arr = dominant_space.iso_eigenvals[0:10] - lambda_exact[0:10]
    # print 'error_arr = ', error_arr
    rel_error_norm = np.linalg.norm(error_arr) / np.linalg.norm(
        lambda_exact[0:10])

    return rel_error_norm
Exemplo n.º 4
0
    def test_dominant_directions(self):
        uq_systemsize = 5
        mean_v = 248.136  # Mean value of input random variable
        mean_alpha = 5  #
        mean_Ma = 0.84
        mean_re = 1.e6
        mean_rho = 0.38
        mean_cg = np.zeros((3))

        std_dev = np.diag([1.0, 0.2, 0.01, 1.e2,
                           0.01])  # np.eye(uq_systemsize)
        mu_init = np.array([mean_v, mean_alpha, mean_Ma, mean_re, mean_rho])
        rv_dict = {
            'v': mean_v,
            'alpha': mean_alpha,
            'Mach_number': mean_Ma,
            're': mean_re,
            'rho': mean_rho,
        }

        QoI = examples.OASAerodynamicWrapper(uq_systemsize, rv_dict)
        jdist = cp.MvNormal(mu_init, std_dev)
        dominant_space = DimensionReduction(n_arnoldi_sample=uq_systemsize + 1,
                                            exact_Hessian=False)
        dominant_space.getDominantDirections(QoI, jdist, max_eigenmodes=3)

        expected_eigenvals = np.array([0.00001345, -0.00000043, 0., 0., -0.])
        expected_eigenvecs = np.array([[-0.00000003, 0.00000002],
                                       [-1., 0.00000001],
                                       [-0.00000001, -0.99999992],
                                       [0., -0.00039715], [0.00000004, 0.]])

        # We will only test the first 2 eigenvectors since the remaining 4 eigenvalues
        # are 0.
        np.testing.assert_array_almost_equal(dominant_space.iso_eigenvals,
                                             expected_eigenvals,
                                             decimal=6)
        np.testing.assert_array_almost_equal(dominant_space.iso_eigenvecs[:,
                                                                          0:2],
                                             expected_eigenvecs,
                                             decimal=6)
    def test_reducedCollocation(self):

        systemsize = 4
        eigen_decayrate = 2.0

        # Create Hadmard Quadratic object
        QoI = examples.HadamardQuadratic(systemsize, eigen_decayrate)

        # Create stochastic collocation object
        collocation = StochasticCollocation(3, "Normal")

        # Create dimension reduction object
        threshold_factor = 0.9
        dominant_space = DimensionReduction(threshold_factor=threshold_factor,
                                            exact_Hessian=True)

        # Initialize chaospy distribution
        std_dev = 0.2 * np.ones(QoI.systemsize)
        x = np.ones(QoI.systemsize)
        jdist = cp.MvNormal(x, np.diag(std_dev))

        # Get the eigenmodes of the Hessian product and the dominant indices
        dominant_space.getDominantDirections(QoI, jdist)

        QoI_func = QoI.eval_QoI
        # Check the mean
        mu_j = collocation.normal.reduced_mean(QoI_func, jdist, dominant_space)
        true_value_mu_j = 4.05
        err = abs(mu_j - true_value_mu_j)
        self.assertTrue(err < 1.e-13)

        # Check the variance
        red_var_j = collocation.normal.reduced_variance(
            QoI_func, jdist, dominant_space, mu_j)
        # var_j = collocation.normal.variance(QoI_func, jdist, mu_j)
        analytical_var_j = QoI.eval_analytical_QoI_variance(x, cp.Cov(jdist))
        err = abs(analytical_var_j - red_var_j)
        self.assertTrue(err < 1.e-4)
Exemplo n.º 6
0
 def test_angles_2QoI(self):
     systemsize = 2
     theta = np.pi / 3
     mu = np.random.randn(systemsize)
     std_dev = np.eye(systemsize)  # np.diag(np.random.rand(systemsize))
     jdist = cp.MvNormal(mu, std_dev)
     QoI1 = examples.Paraboloid2D(systemsize, (theta, ))
     QoI2 = examples.PolyRVDV()
     QoI_dict = {
         'paraboloid2': {
             'quadrature_degree': 3,
             'reduced_collocation': False,
             'QoI_func': QoI1.eval_QoI,
             'output_dimensions': 1,
             'include_derivs': False,
         },
         'PolyRVDV': {
             'quadrature_degree': 3,
             'reduced_collocation': False,
             'QoI_func': QoI2.eval_QoI,
             'output_dimensions': 1,
             'include_derivs': False,
         }
     }
     # Create the dominant space for the 2 dominant directions
     threshold_factor = 0.8
     QoI1_dominant_space = DimensionReduction(
         threshold_factor=threshold_factor, exact_Hessian=True)
     QoI2_dominant_space = DimensionReduction(
         threshold_factor=threshold_factor, exact_Hessian=True)
     QoI1_dominant_space.getDominantDirections(QoI1, jdist)
     QoI2_dominant_space.getDominantDirections(QoI2, jdist)
     S1 = QoI1_dominant_space.iso_eigenvecs[:, QoI1_dominant_space.
                                            dominant_indices]
     S2 = QoI2_dominant_space.iso_eigenvecs[:, QoI2_dominant_space.
                                            dominant_indices]
     # Finally, compute the angles
     angles = utils.compute_subspace_angles(S1, S2)
     self.assertTrue(abs(angles[0] - theta) < 1.e-14)
    def test_dimensionReduction_arnoldi_enlarge(self):
        systemsize = 128
        eigen_decayrate = 1.0

        # Create Hadmard Quadratic object
        QoI = examples.HadamardQuadratic(systemsize, eigen_decayrate)

        # Create stochastic collocation object
        collocation = StochasticCollocation(3, "Normal")

        # Create dimension reduction object
        threshold_factor = 0.9
        dominant_space_exactHess = DimensionReduction(
            threshold_factor=threshold_factor, exact_Hessian=True)
        dominant_space_arnoldi = DimensionReduction(exact_Hessian=False,
                                                    n_arnoldi_sample=71)

        # Initialize chaospy distribution
        std_dev = np.random.rand(QoI.systemsize)
        x = np.random.rand(QoI.systemsize)
        jdist = cp.MvNormal(x, np.diag(std_dev))

        # Get the eigenmodes of the Hessian product and the dominant indices
        dominant_space_exactHess.getDominantDirections(QoI, jdist)
        dominant_space_arnoldi.getDominantDirections(QoI, jdist)

        # Print iso_eigenvals
        sort_ind1 = dominant_space_exactHess.iso_eigenvals.argsort()[::-1]
        sort_ind2 = dominant_space_arnoldi.iso_eigenvals.argsort()[::-1]
        lambda_exact = dominant_space_exactHess.iso_eigenvals[sort_ind1]
        lambda_arnoldi = dominant_space_arnoldi.iso_eigenvals[sort_ind2]

        energy_err = np.linalg.norm(lambda_arnoldi[0:10] -
                                    lambda_exact[0:10]) / np.linalg.norm(
                                        lambda_exact[0:10])

        self.assertTrue(energy_err < 1.e-8)
    def test_nonrv_derivatives_reduced_collocation(self):
        # This test checks the analytical derivative w.r.t complex step
        systemsize = 2
        n_parameters = 2
        mu = mean_2dim  # np.random.randn(systemsize)
        std_dev = std_dev_2dim  # abs(np.diag(np.random.randn(systemsize)))
        jdist = cp.MvNormal(mu, std_dev)
        QoI = examples.PolyRVDV(data_type=complex)

        dv = np.random.randn(systemsize) + 0j
        QoI.set_dv(dv)
        # Create dimension reduction object
        threshold_factor = 0.9
        dominant_space = DimensionReduction(threshold_factor=threshold_factor,
                                            exact_Hessian=True)
        # Get the eigenmodes of the Hessian product and the dominant indices
        dominant_space.getDominantDirections(QoI, jdist)
        dominant_dir = dominant_space.iso_eigenvecs[:, dominant_space.
                                                    dominant_indices]
        # Create the Stochastic Collocation object
        deriv_dict = {
            'dv': {
                'dQoI_func': QoI.eval_QoIGradient_dv,
                'output_dimensions': n_parameters
            }
        }
        QoI_dict = {
            'PolyRVDV': {
                'quadrature_degree': 3,
                'reduced_collocation': True,
                'QoI_func': QoI.eval_QoI,
                'output_dimensions': 1,
                'dominant_dir': dominant_dir,
                'include_derivs': True,
                'deriv_dict': deriv_dict
            }
        }
        sc_obj = StochasticCollocation3(jdist,
                                        'MvNormal',
                                        QoI_dict,
                                        data_type=complex)
        sc_obj.evaluateQoIs(jdist)
        dmu_j = sc_obj.dmean(of=['PolyRVDV'], wrt=['dv'])
        dvar_j = sc_obj.dvariance(of=['PolyRVDV'], wrt=['dv'])
        dstd_dev = sc_obj.dStdDev(of=['PolyRVDV'], wrt=['dv'])

        # Lets do complex step
        pert = complex(0, 1e-30)
        dmu_j_complex = np.zeros(n_parameters, dtype=complex)
        dvar_j_complex = np.zeros(n_parameters, dtype=complex)
        dstd_dev_complex = np.zeros(n_parameters, dtype=complex)
        for i in range(0, n_parameters):
            dv[i] += pert
            QoI.set_dv(dv)
            sc_obj.evaluateQoIs(jdist)
            mu_j = sc_obj.mean(of=['PolyRVDV'])
            var_j = sc_obj.variance(of=['PolyRVDV'])
            std_dev_j = np.sqrt(var_j['PolyRVDV'][0, 0])
            dmu_j_complex[i] = mu_j['PolyRVDV'].imag / pert.imag
            dvar_j_complex[i] = var_j['PolyRVDV'].imag / pert.imag
            dstd_dev_complex[i] = std_dev_j.imag / pert.imag
            dv[i] -= pert

        err1 = dmu_j['PolyRVDV']['dv'] - dmu_j_complex
        self.assertTrue((err1 < 1.e-13).all())

        err2 = dvar_j['PolyRVDV']['dv'] - dvar_j_complex
        self.assertTrue((err2 < 1.e-10).all())

        err3 = dstd_dev['PolyRVDV']['dv'] - dstd_dev_complex
        self.assertTrue((err2 < 1.e-12).all())
Exemplo n.º 9
0
    return mu_j, var_j


if __name__ == "__main__":
    # Step 1: Instantiate all objects needed for test
    uq_systemsize = 6
    UQObj = UQScanEagleOpt(uq_systemsize, all_rv=True)
    UQObj.QoI.p['oas_scaneagle.wing.thickness_cp'] = 1.e-3 * np.array(
        [5.5, 5.5, 5.5])
    UQObj.QoI.p['oas_scaneagle.wing.twist_cp'] = 2.5 * np.ones(3)
    UQObj.QoI.p.final_setup()

    # Dominant dominant
    dominant_space = DimensionReduction(n_arnoldi_sample=uq_systemsize + 1,
                                        exact_Hessian=False,
                                        sample_radius=1.e-2)
    dominant_space.getDominantDirections(UQObj.QoI,
                                         UQObj.jdist,
                                         max_eigenmodes=4)
    # # Full collocation
    # sc_obj = StochasticCollocation2(UQObj.jdist, 3, 'MvNormal', UQObj.QoI_dict,
    #                                 include_derivs=False)
    # sc_obj.evaluateQoIs(UQObj.jdist, include_derivs=False)

    # REduced collocation
    dominant_dir = dominant_space.iso_eigenvecs[:, dominant_space.
                                                dominant_indices]
    sc_obj = StochasticCollocation2(UQObj.jdist,
                                    3,
                                    'MvNormal',
Exemplo n.º 10
0
    'surrogate info full path':
    os.environ['HOME'] +
    '/UserApps/pyStatReduce/pystatreduce/optimize/dymos_interceptor/quadratic_surrogate/'
    + fname,
    'surrogate_type':
    'kriging',
    'kriging_theta':
    1.e-4,
    'correlation function':
    'squar_exp',
}
surrogate_QoI = InterceptorSurrogateQoI(systemsize, surrogate_input_dict)

# Get the dominant directions
dominant_space = DimensionReduction(n_arnoldi_sample=systemsize + 1,
                                    exact_Hessian=False,
                                    sample_radius=1.e-1)
dominant_space.getDominantDirections(surrogate_QoI, jdist, max_eigenmodes=10)

# Get the Active Subspace
active_subspace = ActiveSubspace(surrogate_QoI,
                                 n_dominant_dimensions=20,
                                 n_monte_carlo_samples=1000,
                                 read_rv_samples=False,
                                 use_svd=True,
                                 use_iso_transformation=True)
active_subspace.getDominantDirections(surrogate_QoI, jdist)

# Now get the two angles
n_bases = 2
eigenvecs_dom = dominant_space.iso_eigenvecs[:, 0:n_bases]
def run_hadamard(systemsize, eigen_decayrate, std_dev, n_eigenmodes):
    n_collocation_pts = 3

    # Create Hadmard Quadratic object
    QoI = examples.HadamardQuadratic(systemsize, eigen_decayrate)

    # Initialize chaospy distribution
    x = np.random.rand(QoI.systemsize)
    jdist = cp.MvNormal(x, np.diag(std_dev))

    threshold_factor = 1.0
    use_exact_Hessian = False
    if use_exact_Hessian:
        dominant_space = DimensionReduction(threshold_factor=threshold_factor,
                                            exact_Hessian=True)
        dominant_space.getDominantDirections(QoI, jdist)
        dominant_dir = dominant_space.iso_eigenvecs[:, 0:n_eigenmodes]
    else:
        dominant_space = DimensionReduction(threshold_factor=threshold_factor,
                                            exact_Hessian=False,
                                            n_arnoldi_sample=71,
                                            min_eigen_accuracy=1.e-2)
        dominant_space.getDominantDirections(QoI,
                                             jdist,
                                             max_eigenmodes=n_eigenmodes)
        dominant_dir = dominant_space.dominant_dir
    # print "dominant_indices = ", dominant_space.dominant_indices

    # Create stochastic collocation object
    # collocation = StochasticCollocation(n_collocation_pts, "Normal")
    QoI_dict = {
        'Hadamard': {
            'QoI_func': QoI.eval_QoI,
            'output_dimensions': 1,
        },
    }
    sc_obj = StochasticCollocation2(jdist,
                                    n_collocation_pts,
                                    'MvNormal',
                                    QoI_dict,
                                    include_derivs=False,
                                    reduced_collocation=True,
                                    dominant_dir=dominant_dir)
    sc_obj.evaluateQoIs(jdist)

    # Collocate
    # mu_j = collocation.normal.reduced_mean(QoI, jdist, dominant_space)
    mu_j = sc_obj.mean(of=['Hadamard'])
    var_j = sc_obj.variance(of=['Hadamard'])
    std_dev_j = np.sqrt(var_j['Hadamard'])
    # print "mu_j = ", mu_j

    # Evaluate the analytical value of the Hadamard Quadratic
    covariance = cp.Cov(jdist)
    mu_analytic = QoI.eval_analytical_QoI_mean(x, covariance)
    var_analytic = QoI.eval_analytical_QoI_variance(x, covariance)
    std_dev_analytic = np.sqrt(var_analytic)
    # print "mu_analytic = ", mu_analytic

    relative_error_mu = np.linalg.norm(
        (mu_j['Hadamard'] - mu_analytic) / mu_analytic)
    relative_err_var = np.linalg.norm(
        (var_j['Hadamard'] - var_analytic) / var_analytic)
    relative_err_std_dev = np.linalg.norm(
        (std_dev_j - std_dev_analytic) / std_dev_analytic)
    # print "relative_error = ", relative_error

    return relative_error_mu, relative_err_var, relative_err_std_dev
    def __init__(self,
                 rv_dict,
                 design_point,
                 rdo_factor=2.0,
                 krylov_pert=1.e-1,
                 active_subspace=False,
                 max_eigenmodes=2,
                 n_as_samples=1000):

        self.rdo_factor = rdo_factor

        # Total number of nodes to use in the spanwise (num_y) and
        # chordwise (num_x) directions. Vary these to change the level of fidelity.
        num_y = 21
        num_x = 3
        mesh_dict = {
            'num_y': num_y,
            'num_x': num_x,
            'wing_type': 'rect',
            'symmetry': True,
            'span_cos_spacing': 0.5,
            'span': 3.11,
            'root_chord': 0.3,
        }

        self.uq_systemsize = len(rv_dict)

        dv_dict = {
            'n_twist_cp': 3,
            'n_thickness_cp': 3,
            'n_CM': 3,
            'n_thickness_intersects': 10,
            'n_constraints': 1 + 10 + 1 + 3 + 3,
            'ndv': 3 + 3 + 2,
            'mesh_dict': mesh_dict,
            'rv_dict': rv_dict
        }

        mu, std_dev = self.get_input_rv_statistics(rv_dict)
        self.jdist = cp.MvNormal(mu, std_dev)
        self.QoI = examples.oas_scaneagle2.OASScanEagleWrapper2(
            self.uq_systemsize, dv_dict)
        self.QoI.p['oas_scaneagle.wing.thickness_cp'] = design_point[
            'thickness_cp']
        self.QoI.p['oas_scaneagle.wing.twist_cp'] = design_point['twist_cp']
        self.QoI.p['oas_scaneagle.wing.sweep'] = design_point['sweep']
        self.QoI.p['oas_scaneagle.alpha'] = design_point['alpha']
        self.QoI.p.final_setup()

        # Figure out which dimension reduction technique to use
        start_time = time.time()
        if active_subspace == False:
            self.dominant_space = DimensionReduction(
                n_arnoldi_sample=self.uq_systemsize + 1,
                exact_Hessian=False,
                sample_radius=krylov_pert)
            self.dominant_space.getDominantDirections(
                self.QoI, self.jdist, max_eigenmodes=max_eigenmodes)
        else:
            self.dominant_space = ActiveSubspace(
                self.QoI,
                n_dominant_dimensions=max_eigenmodes,
                n_monte_carlo_samples=n_as_samples,
                read_rv_samples=False,
                use_svd=True,
                use_iso_transformation=True)
            self.dominant_space.getDominantDirections(self.QoI, self.jdist)
            # Reset the design point
            self.QoI.p['oas_scaneagle.wing.thickness_cp'] = design_point[
                'thickness_cp']
            self.QoI.p['oas_scaneagle.wing.twist_cp'] = design_point[
                'twist_cp']
            self.QoI.p['oas_scaneagle.wing.sweep'] = design_point['sweep']
            self.QoI.p['oas_scaneagle.alpha'] = design_point['alpha']
            self.QoI.p.final_setup()
            # Reset the random variables
            self.QoI.update_rv(mu)
        time_elapsed = time.time() - start_time
        print('time_elapsed =', time_elapsed)

        dfuelburn_dict = {
            'dv': {
                'dQoI_func': self.QoI.eval_ObjGradient_dv,
                'output_dimensions': dv_dict['ndv'],
            }
        }
        dcon_dict = {
            'dv': {
                'dQoI_func': self.QoI.eval_ConGradient_dv,
                'output_dimensions': dv_dict['ndv']
            }
        }
        dcon_failure_dict = {
            'dv': {
                'dQoI_func': self.QoI.eval_ConFailureGradient_dv,
                'output_dimensions': dv_dict['ndv'],
            }
        }
        self.QoI_dict = {
            'fuelburn': {
                'QoI_func': self.QoI.eval_QoI,
                'output_dimensions': 1,
                'deriv_dict': dfuelburn_dict
            },
            'constraints': {
                'QoI_func': self.QoI.eval_AllConstraintQoI,
                'output_dimensions': dv_dict['n_constraints'],
                'deriv_dict': dcon_dict
            },
            'con_failure': {
                'QoI_func': self.QoI.eval_confailureQoI,
                'output_dimensions': 1,
                'deriv_dict': dcon_failure_dict
            }
        }
    print('mu_j_mc = ', mu_j['fuelburn'][0])
    print('var_j_mc = ', var_j['fuelburn'][0])

    print("time for sampling = ", t_sampling - start_time)
    print('time mu = ', time_elapsed_mu)
    print('time var = ', time_elapsed_var)

elif sys.argv[1] == 'reduced':
    # mu_j_full = 5.341619712754059  # RV :- Ma, CT, W0, R, load_factor, mrho
    # var_j_full = 3.786547863834123 #
    mu_j_full = 5.342395801650296  # RV :- Ma, CT, W0, R, load_factor, mrho, altitude
    var_j_full = 3.790677219208799  # (3 collocation pts per ditection)

    # Create dimension reduction based on system arguments
    dominant_space = DimensionReduction(n_arnoldi_sample=uq_systemsize + 1,
                                        exact_Hessian=False,
                                        sample_radius=sample_radii[int(
                                            sys.argv[2])])
    dominant_space.getDominantDirections(QoI,
                                         jdist,
                                         max_eigenmodes=int(sys.argv[3]))
    print('iso_eigenvals = ', dominant_space.iso_eigenvals)
    print('iso_eigenvecs = \n', dominant_space.iso_eigenvecs)
    # Create a stochastic collocation object
    sc_obj = StochasticCollocation2(jdist,
                                    2,
                                    'MvNormal',
                                    QoI_dict,
                                    include_derivs=False,
                                    reduced_collocation=True,
                                    dominant_dir=dominant_space.dominant_dir)
    sc_obj.evaluateQoIs(jdist, include_derivs=False)
Exemplo n.º 14
0
    'rv_dict': rv_dict
}

# Create the base openaerostruct problem wrapper that will be used by the
# different quantity of interests
oas_obj = OASScanEagleWrapper(uq_systemsize, input_dict, include_dict_rv=True)
# Create the QoI objects
obj_QoI = Fuelburn(uq_systemsize, oas_obj)
failure_QoI = StressConstraint(uq_systemsize, oas_obj)
lift_con_QoI = LiftConstraint(uq_systemsize, oas_obj)
moment_con_QoI = MomentConstraint(uq_systemsize, oas_obj)

# Create the dimension reduction objects for all of the different quantity of interest
# Get the dominant directions of the different QoIs here
dominant_space_obj = DimensionReduction(n_arnoldi_sample=uq_systemsize + 1,
                                        exact_Hessian=False,
                                        sample_radius=1.e-2)
dominant_space_obj.getDominantDirections(obj_QoI, jdist, max_eigenmodes=4)
dominant_space_failure = DimensionReduction(n_arnoldi_sample=uq_systemsize + 1,
                                            exact_Hessian=False,
                                            sample_radius=1.e-2)
dominant_space_failure.getDominantDirections(failure_QoI,
                                             jdist,
                                             max_eigenmodes=4)
dominant_space_liftcon = DimensionReduction(n_arnoldi_sample=uq_systemsize + 1,
                                            exact_Hessian=False,
                                            sample_radius=1.e-2)
dominant_space_liftcon.getDominantDirections(lift_con_QoI,
                                             jdist,
                                             max_eigenmodes=4)
dominant_space_CM = DimensionReduction(n_arnoldi_sample=uq_systemsize + 1,
    def __init__(self, uq_systemsize, all_rv=False):
        self.rdo_factor = 2.0

        # Total number of nodes to use in the spanwise (num_y) and
        # chordwise (num_x) directions. Vary these to change the level of fidelity.
        num_y = 21
        num_x = 3
        mesh_dict = {'num_y' : num_y,
                     'num_x' : num_x,
                     'wing_type' : 'rect',
                     'symmetry' : True,
                     'span_cos_spacing' : 0.5,
                     'span' : 3.11,
                     'root_chord' : 0.3,
                     }

        rv_dict = {'Mach_number' : mean_Ma,
                   'CT' : mean_TSFC,
                   'W0' : mean_W0,
                   'E' : mean_E, # surface RV
                   'G' : mean_G, # surface RV
                   'mrho' : mean_mrho, # surface RV
                    }

        dv_dict = {'n_twist_cp' : 3,
                   'n_thickness_cp' : 3,
                   'n_CM' : 3,
                   'n_thickness_intersects' : 10,
                   'n_constraints' : 1 + 10 + 1 + 3 + 3,
                   'ndv' : 3 + 3 + 2,
                   'mesh_dict' : mesh_dict,
                   'rv_dict' : rv_dict
                    }


        mu = np.array([mean_Ma, mean_TSFC, mean_W0, mean_E, mean_G, mean_mrho])
        std_dev = np.diag([0.005, 0.00607/3600, 0.2, 5.e9, 1.e9, 50])
        self.jdist = cp.MvNormal(mu, std_dev)
        self.QoI = examples.OASScanEagleWrapper(uq_systemsize, dv_dict)

        # This setup is according to the one in the scaneagle paper
        self.QoI.p['oas_scaneagle.wing.thickness_cp'] = 1.e-3 * np.array([5.5, 5.5, 5.5])
        self.QoI.p['oas_scaneagle.wing.twist_cp'] = 2.5*np.ones(3)
        self.QoI.p.final_setup()

        # Compute the dominant directions
        self.dominant_space = DimensionReduction(n_arnoldi_sample=uq_systemsize+1,
                                                 exact_Hessian=False,
                                                 sample_radius=1.e-2)
        self.dominant_space.getDominantDirections(self.QoI, self.jdist, max_eigenmodes=2)


        dfuelburn_dict = {'dv' : {'dQoI_func' : self.QoI.eval_ObjGradient_dv,
                                  'output_dimensions' : dv_dict['ndv'],
                                  }
                         }
        dcon_dict = {'dv' : {'dQoI_func' : self.QoI.eval_ConGradient_dv,
                             'output_dimensions' : dv_dict['ndv']
                            }
                    }
        dcon_failure_dict = {'dv' : {'dQoI_func' : self.QoI.eval_ConFailureGradient_dv,
                                     'output_dimensions' : dv_dict['ndv'],
                                    }
                            }
        self.QoI_dict = {'fuelburn' : {'QoI_func' : self.QoI.eval_QoI,
                                       'output_dimensions' : 1,
                                       'deriv_dict' : dfuelburn_dict
                                      },
                         'constraints' : {'QoI_func' : self.QoI.eval_AllConstraintQoI,
                                          'output_dimensions' : dv_dict['n_constraints'],
                                          'deriv_dict' : dcon_dict
                                         },
                         'con_failure' : {'QoI_func' : self.QoI.eval_confailureQoI,
                                          'output_dimensions' : 1,
                                          'deriv_dict' : dcon_failure_dict
                                         }
                        }
Exemplo n.º 16
0
    0.02550976, 0.01783919, 0.0125073, 0.01226541
])
jdist = cp.MvNormal(mu, np.diag(std_dev[:-1]))

# Create the QoI object
QoI = InterceptorSurrogateQoI(systemsize, input_dict)
QoI_dict = {
    'time_duration': {
        'QoI_func': QoI.eval_QoI,
        'output_dimensions': 1,
    }
}

# Get the dominant directions
dominant_space = DimensionReduction(n_arnoldi_sample=int(sys.argv[1]),
                                    exact_Hessian=False,
                                    sample_radius=1.e-1)
dominant_space.getDominantDirections(QoI, jdist, max_eigenmodes=15)
n_dominant_dir = 10  # int(sys.argv[2])
dominant_dir = dominant_space.iso_eigenvecs[:, 0:n_dominant_dir]

# Do the monte carlo integration
nsample = 10000
mc_obj = MonteCarlo(nsample,
                    jdist,
                    QoI_dict,
                    reduced_collocation=False,
                    include_derivs=False)
mc_obj.getSamples(jdist, include_derivs=False)

mu_j = mc_obj.mean(jdist, of=['time_duration'])