Beispiel #1
0
    def test_training_point_array_for_nan_values(self):

        # Model
        interp = om.MetaModelUnStructuredComp()

        # Training Data
        x_train = np.linspace(0, 10, 20)
        y_train = np.linspace(0, 20, 20)

        # Inputs
        interp.add_input('x', 0., training_data=x_train)
        interp.add_input('y', 0., training_data=x_train)

        #Outputs
        interp.add_output('cos_x', 0., training_data=.5 * np.cos(y_train))

        # Surrogate Model
        interp.options['default_surrogate'] = om.ResponseSurface()

        prob = om.Problem()
        prob.model.add_subsystem('interp', interp)
        prob.setup()

        viz = MetaModelVisualization(interp)
        training_points_output = viz._unstructured_training_points()

        for i in range(0, 2):
            self.assertFalse(np.any(np.isnan(training_points_output[:, i])))
Beispiel #2
0
    def test_training_point_array_width(self):

        # Model
        interp = om.MetaModelUnStructuredComp()

        # Training Data
        x_train = np.linspace(0, 10, 20)
        y_train = np.linspace(0, 20, 20)

        # Inputs
        interp.add_input('x', 0., training_data=x_train)
        interp.add_input('y', 0., training_data=x_train)

        #Outputs
        interp.add_output('cos_x', 0., training_data=.5 * np.cos(y_train))

        # Surrogate Model
        interp.options['default_surrogate'] = om.ResponseSurface()

        prob = om.Problem()
        prob.model.add_subsystem('interp', interp)
        prob.setup()

        viz = MetaModelVisualization(interp)
        training_points_output = viz._unstructured_training_points()

        self.assertTrue(training_points_output.shape[1] == 2)
Beispiel #3
0
    def test_five_alpha_points(self):
        filename = os.path.join(self.csv_dir, 'test_five_alpha_points.csv')

        known_points_bottom = np.genfromtxt(filename,
                                            delimiter=',',
                                            usecols=(1))
        known_points_right = np.genfromtxt(filename,
                                           delimiter=',',
                                           usecols=(0))

        adjusted_points = MetaModelVisualization(self.mm)
        adjusted_points.input_point_list = [
            6.632653061224477, 3.36734693877551
        ]
        adjusted_points.dist_range = 0.5

        right_points = adjusted_points._structured_training_points(
            compute_distance=True, source='right')
        right_plot = adjusted_points._right_plot()
        right_transparency = adjusted_points.right_alphas

        bottom_points = adjusted_points._structured_training_points(
            compute_distance=True, source='bottom')
        bottom_plot = adjusted_points._bottom_plot()
        bottom_transparency = adjusted_points.bottom_alphas

        assert_almost_equal(known_points_right, right_transparency, decimal=5)
        assert_almost_equal(known_points_bottom,
                            bottom_transparency,
                            decimal=5)
    def test_single_input_parameter(self):

        # Model
        interp = om.MetaModelUnStructuredComp()

        # Training Data
        x_train = np.linspace(0, 10, 20)
        y_train = np.linspace(0, 20, 20)

        # Inputs
        interp.add_input('simple_x', 0., training_data=x_train)

        #Outputs
        interp.add_output('cos_x', 0., training_data=.5 * np.cos(y_train))

        # Surrogate Model
        interp.options['default_surrogate'] = om.ResponseSurface()

        prob = om.Problem()
        prob.model.add_subsystem('interp', interp)
        prob.setup()

        with self.assertRaises(Exception) as context:
            viz = MetaModelVisualization(interp)

        msg = 'Must have more than one input value'
        self.assertTrue(msg in str(context.exception))
Beispiel #5
0
    def test_working_scipy_cubic(self):

        # create input param training data, of sizes 25, 5, and 10 points resp.
        p1 = np.linspace(0, 100, 25)
        p2 = np.linspace(-10, 10, 5)
        p3 = np.linspace(0, 1, 10)

        # can use meshgrid to create a 3D array of test data
        P1, P2, P3 = np.meshgrid(p1, p2, p3, indexing='ij')
        f = np.sqrt(P1) + P2 * P3

        # Create regular grid interpolator instance
        interp = om.MetaModelStructuredComp(method='scipy_cubic')
        interp.add_input('p1', 0.5, training_data=p1)
        interp.add_input('p2', 0.0, training_data=p2)
        interp.add_input('p3', 3.14, training_data=p3)

        interp.add_output('f', 0.0, training_data=f)

        # Set up the OpenMDAO model
        model = om.Group()
        model.add_subsystem('comp', interp, promotes=["*"])
        prob = om.Problem(model)
        prob.setup()

        MetaModelVisualization(interp)
Beispiel #6
0
    def test_working_slinear(self):

        num_train = 10

        x0_min, x0_max = -5.0, 10.0
        x1_min, x1_max = 0.0, 15.0
        train_x0 = np.linspace(x0_min, x0_max, num_train)
        train_x1 = np.linspace(x1_min, x1_max, num_train)
        t_data = np.array([[308.12909601, 253.61567418, 204.6578079, 161.25549718, 123.40874201, 91.1175424,   64.38189835,  43.20180985,  27.5772769,   17.50829952],
                        [162.89542418, 123.20470795,  89.06954726,  60.48994214,  37.46589257, 19.99739855,   8.08446009,   1.72707719,   0.92524984,   5.67897804,],
                        [ 90.2866907,   63.02637433,  41.32161352,  25.17240826,  14.57875856, 9.54066442,  10.05812583,  16.13114279,  27.75971531,  44.94384339,],
                        [ 55.60211264,  38.37989042,  26.71322375,  20.60211264,  20.04655709, 25.04655709,  35.60211264,  51.71322375,  73.37989042, 100.60211264],
                        [ 22.81724065,  13.24080685,   9.2199286,   10.75460591,  17.84483877, 30.49062719, 48.69197117,  72.4488707,  101.76132579, 136.62933643],
                        [  5.11168719,   0.78873608,   2.02134053,   8.80950054,  21.1532161, 39.05248721,  62.50731389,  91.51769611, 126.0836339,  166.20512723],
                        [ 14.3413983,   12.87962416,  16.97340558,  26.62274256,  41.82763509, 62.58808317,  88.90408682, 120.77564601, 158.20276077, 201.18543108],
                        [ 20.18431209,  19.1914092,   23.75406186,  33.87227009,  49.54603386, 70.77535319,  97.56022808, 129.90065853, 167.79664453, 211.24818608],
                        [  8.48953212,   5.57319475,   8.21241294,  16.40718668,  30.15751598, 49.46340083,  74.32484124, 104.74183721, 140.71438873, 182.2424958 ],
                        [ 10.96088904,   3.72881146,   2.05228945,   5.93132298,  15.36591208, 30.35605673,  50.90175693,  77.00301269, 108.65982401, 145.87219088]])

        prob = om.Problem()
        ivc = om.IndepVarComp()
        ivc.add_output('x0', 0.0)
        ivc.add_output('x1', 0.0)

        prob.model.add_subsystem('p', ivc, promotes=['*'])
        mm = prob.model.add_subsystem('mm', om.MetaModelStructuredComp(method='slinear'),
                                    promotes=['x0', 'x1'])
        mm.add_input('x0', 0.0, train_x0)
        mm.add_input('x1', 0.0, train_x1)
        mm.add_output('f', 0.0, t_data)

        prob.setup()
        prob.final_setup()

        MetaModelVisualization(mm)
Beispiel #7
0
    def test_working_slinear(self):

        num_train = 10

        x0_min, x0_max = -5.0, 10.0
        x1_min, x1_max = 0.0, 15.0
        train_x0 = np.linspace(x0_min, x0_max, num_train)
        train_x1 = np.linspace(x1_min, x1_max, num_train)
        t_data = self.grid_data

        prob = om.Problem()
        ivc = om.IndepVarComp()
        ivc.add_output('x0', 0.0)
        ivc.add_output('x1', 0.0)

        prob.model.add_subsystem('p', ivc, promotes=['*'])
        mm = prob.model.add_subsystem(
            'mm',
            om.MetaModelStructuredComp(method='slinear'),
            promotes=['x0', 'x1'])
        mm.add_input('x0', 0.0, train_x0)
        mm.add_input('x1', 0.0, train_x1)
        mm.add_output('f', 0.0, t_data)

        prob.setup()
        prob.final_setup()

        MetaModelVisualization(mm)
Beispiel #8
0
    def test_working_scipy_slinear(self):

        # Create regular grid interpolator instance
        xor_interp = om.MetaModelStructuredComp(method='scipy_slinear')

        # set up inputs and outputs
        xor_interp.add_input('x',
                             0.0,
                             training_data=np.array([0.0, 1.0]),
                             units=None)
        xor_interp.add_input('y',
                             1.0,
                             training_data=np.array([0.0, 1.0]),
                             units=None)

        xor_interp.add_output('xor',
                              1.0,
                              training_data=np.array([[0.0, 1.0], [1.0, 0.0]]),
                              units=None)

        # Set up the OpenMDAO model
        model = om.Group()
        ivc = om.IndepVarComp()
        ivc.add_output('x', 0.0)
        ivc.add_output('y', 1.0)
        model.add_subsystem('ivc', ivc, promotes=["*"])
        model.add_subsystem('comp', xor_interp, promotes=["*"])
        prob = om.Problem(model)
        prob.setup()

        MetaModelVisualization(xor_interp)
Beispiel #9
0
    def test_in_between_training_points_bottom(self):

        filename = os.path.join(self.csv_dir, 'unstructured_test_points_bottom.csv')

        known_points = np.genfromtxt(
            filename, delimiter=',', skip_header=1)
        known_points = np.delete(known_points, 2, 1)
        known_points = known_points[known_points[:,0].argsort()[::-1]]

        adjusted_points = MetaModelVisualization(self.mm)
        adjusted_points.input_point_list = [0.04203304, 2.043553874897959, 0.02435233]
        adjusted_points.dist_range = 0.75

        new_points = adjusted_points._unstructured_training_points(compute_distance=True, source='bottom')
        new_points = np.delete(new_points, 2, 1)
        new_points = new_points[new_points[:,0].argsort()[::-1]]

        assert_almost_equal(known_points, new_points, decimal=4)
Beispiel #10
0
    def test_in_between_training_points_right(self):

        filename = os.path.join(self.csv_dir, 'unstructured_test_points_right.csv')

        known_points = np.genfromtxt(
            filename, delimiter=',', skip_header=1)
        known_points = np.delete(known_points, 2, 1)
        known_points = known_points[known_points[:,1].argsort()[::-1]]

        adjusted_points = MetaModelVisualization(self.mm)
        adjusted_points.input_point_list = [1.619333019591837, 0.01423411, 0.02435233]
        adjusted_points.dist_range = 0.75

        new_points = adjusted_points._unstructured_training_points(compute_distance=True, source='right')
        new_points = np.delete(new_points, 2, 1)
        new_points = new_points[new_points[:,1].argsort()[::-1]]

        assert_almost_equal(known_points, new_points, decimal=4)
Beispiel #11
0
    def test_basic(self):
        # Tests that semi structured grids load without error.

        grid = np.array([
            [1.0, 5.0, 8.0],
            [1.0, 5.0, 9.0],
            [1.0, 5.0, 10.0],
            [1.0, 5.0, 20.0],
            [1.0, 5.3, 8.0],
            [1.0, 5.3, 9.0],
            [1.0, 5.3, 10.0],
            [1.0, 5.3, 20.0],
            [1.0, 5.6, 8.0],
            [1.0, 5.6, 9.0],
            [1.0, 5.6, 10.0],
            [1.0, 5.6, 20.0],
            [1.0, 6.0, 8.0],
            [1.0, 6.0, 9.0],
            [1.0, 6.0, 10.0],
            [1.0, 6.0, 20.0],
            [2.0, 7.0, 13.0],
            [2.0, 7.0, 14.0],
            [2.0, 7.0, 15.0],
            [2.0, 7.0, 16.0],
            [2.0, 8.0, 13.0],
            [2.0, 8.0, 14.0],
            [2.0, 8.0, 15.0],
            [2.0, 8.0, 16.0],
            [2.0, 8.5, 13.0],
            [2.0, 8.5, 14.0],
            [2.0, 8.5, 15.0],
            [2.0, 8.5, 16.0],
            [2.0, 9.0, 13.0],
            [2.0, 9.0, 14.0],
            [2.0, 9.0, 15.0],
            [2.0, 9.0, 16.0],
        ])

        values = 15.0 + 2 * np.random.random(32)

        prob = om.Problem()
        model = prob.model

        interp = om.MetaModelSemiStructuredComp(vec_size=3,
                                                training_data_gradients=True)
        interp.add_input('x', training_data=grid[:, 0])
        interp.add_input('y', training_data=grid[:, 1])
        interp.add_input('z', training_data=grid[:, 2])
        interp.add_output('f', training_data=values)

        model.add_subsystem('interp', interp)

        prob.setup(force_alloc_complex=True)
        prob.run_model()

        viz = MetaModelVisualization(interp)
Beispiel #12
0
    def test_flip_inputs_aligned_points(self):

        known_points_right = np.array(
            [[6.66666667e+00, 0.00000000e+00, 2.26757370e-03, 2.01843121e+01],
             [6.66666667e+00, 1.66666667e+00, 2.26757370e-03, 1.91914092e+01],
             [6.66666667e+00, 3.33333333e+00, 2.26757370e-03, 2.37540619e+01],
             [6.66666667e+00, 5.00000000e+00, 2.26757370e-03, 3.38722701e+01],
             [6.66666667e+00, 6.66666667e+00, 2.26757370e-03, 4.95460339e+01],
             [6.66666667e+00, 8.33333333e+00, 2.26757370e-03, 7.07753532e+01],
             [6.66666667e+00, 1.00000000e+01, 2.26757370e-03, 9.75602281e+01],
             [6.66666667e+00, 1.16666667e+01, 2.26757370e-03, 1.29900659e+02],
             [6.66666667e+00, 1.33333333e+01, 2.26757370e-03, 1.67796645e+02],
             [6.66666667e+00, 1.50000000e+01, 2.26757370e-03, 2.11248186e+02]])

        known_points_bottom = np.array(
            [[-5.00000000e+00, 3.33333333e+00, 2.26757370e-03, 2.04657808e+02],
             [-3.33333333e+00, 3.33333333e+00, 2.26757370e-03, 8.90695473e+01],
             [-1.66666667e+00, 3.33333333e+00, 2.26757370e-03, 4.13216135e+01],
             [0.00000000e+00, 3.33333333e+00, 2.26757370e-03, 2.67132238e+01],
             [1.66666667e+00, 3.33333333e+00, 2.26757370e-03, 9.21992860e+00],
             [3.33333333e+00, 3.33333333e+00, 2.26757370e-03, 2.02134053e+00],
             [5.00000000e+00, 3.33333333e+00, 2.26757370e-03, 1.69734056e+01],
             [6.66666667e+00, 3.33333333e+00, 2.26757370e-03, 2.37540619e+01],
             [8.33333333e+00, 3.33333333e+00, 2.26757370e-03, 8.21241294e+00],
             [1.00000000e+01, 3.33333333e+00, 2.26757370e-03, 2.05228945e+00]])

        adjusted_points = MetaModelVisualization(self.mm)
        adjusted_points.input_point_list = [
            6.632653061224477, 3.36734693877551
        ]
        right_points = adjusted_points._structured_training_points(
            compute_distance=True, source='right')
        bottom_points = adjusted_points._structured_training_points(
            compute_distance=True, source='bottom')

        assert_almost_equal(known_points_right, right_points, decimal=5)
        assert_almost_equal(known_points_bottom, bottom_points, decimal=5)
    def test_alpha_transparency(self):

        adjusted_points = MetaModelVisualization(self.mm)
        adjusted_points.input_point_list = [
            0.04203304, 2.043553874897959, 0.02435233
        ]
        adjusted_points.dist_range = 0.75

        known_points_right = np.array([
            0.86567989, 0.78348589, 0.639777, 0.62940986, 0.62587447,
            0.56152752, 0.50987878, 0.49376071, 0.48388914, 0.46630121,
            0.4640111, 0.45980945, 0.41160175, 0.39350227, 0.39026132,
            0.38265725, 0.38192591, 0.36878805, 0.34231505, 0.32336551,
            0.30383287, 0.29137243, 0.28730608, 0.2857517, 0.26159429,
            0.26053377, 0.22521007, 0.18561353, 0.17247835, 0.13633153,
            0.10337783, 0.10021358, 0.08660427, 0.08069668, 0.06691626,
            0.05474111, 0.04688808, 0.04670316, 0.01284643, 0.00901992,
            0.00842553, 0.00386693
        ])

        known_points_bottom = np.array([
            7.65526230e-01, 7.38928632e-01, 7.26005432e-01, 7.15400571e-01,
            6.49085815e-01, 6.40394477e-01, 6.28033520e-01, 6.13040213e-01,
            5.83063203e-01, 4.39487058e-01, 3.68150531e-01, 3.43219760e-01,
            3.23457593e-01, 2.99935268e-01, 2.52810393e-01, 2.44806774e-01,
            2.43471983e-01, 2.36658494e-01, 2.33785648e-01, 2.22517218e-01,
            2.08587699e-01, 1.99369532e-01, 1.95640614e-01, 1.80272528e-01,
            1.74355451e-01, 1.52993016e-01, 1.28729050e-01, 1.28385003e-01,
            1.28254220e-01, 1.05787985e-01, 1.01550282e-01, 8.44650788e-02,
            6.39578812e-02, 3.34477398e-02, 1.97405267e-02, 6.40957590e-04
        ])

        right_points = adjusted_points._unstructured_training_points(
            compute_distance=True, source='right')
        right_plot = adjusted_points._right_plot()
        right_transparency = adjusted_points.right_alphas

        bottom_points = adjusted_points._unstructured_training_points(
            compute_distance=True, source='bottom')
        bottom_plot = adjusted_points._bottom_plot()
        bottom_transparency = adjusted_points.bottom_alphas

        assert_rel_error(self, right_transparency, known_points_right, 1.1e-02)
        assert_rel_error(self, bottom_transparency, known_points_bottom,
                         1.6e-02)
Beispiel #14
0
    def test_single_line_of_alpha_points(self):

        adjusted_points = MetaModelVisualization(self.mm)
        adjusted_points.input_point_list = [
            6.632653061224477, 3.36734693877551
        ]

        right_points = adjusted_points._structured_training_points(
            compute_distance=True, source='right')
        right_plot = adjusted_points._right_plot()

        bottom_points = adjusted_points._structured_training_points(
            compute_distance=True, source='bottom')
        bottom_plot = adjusted_points._bottom_plot()

        self.assertTrue(len(adjusted_points.right_alphas) == 10)
        self.assertTrue(len(adjusted_points.bottom_alphas) == 10)
Beispiel #15
0
    def test_updated_scatter_distance(self):

        filename = os.path.join(self.csv_dir, 'updated_scatter_distance.csv')

        known_points_bottom = np.genfromtxt(filename,
                                            delimiter=',',
                                            usecols=(5, 6, 7, 8))
        known_points_right = np.genfromtxt(filename,
                                           delimiter=',',
                                           usecols=(0, 1, 2, 3))

        adjusted_points = MetaModelVisualization(self.mm)
        adjusted_points.input_point_list = [
            6.632653061224477, 3.36734693877551
        ]
        adjusted_points.dist_range = 0.5

        right_points = adjusted_points._structured_training_points(
            compute_distance=True, source='right')
        bottom_points = adjusted_points._structured_training_points(
            compute_distance=True, source='bottom')

        assert_almost_equal(known_points_right, right_points, decimal=5)
        assert_almost_equal(known_points_bottom, bottom_points, decimal=5)