示例#1
0
    def test_init(self):
        Xd = numpy.array([numpy.linspace(0, 10, 4), numpy.linspace(4, 6, 4)])

        fit = fitter.Fit()
        fit.set_data('mydata', Xd)
        d = fit.data['mydata']
        self.assertEqual(d.id, 'mydata')
        npt.assert_almost_equal(d.values, Xd)
示例#2
0
 def test_solve(self):
     fit = fitter.Fit()
     fit.bind_element_point(9, [0.3], 'datacloud', 0, weight=2)
     fit.bind_element_point(9, [0.8], 'datacloud', 1, weight=2)
     fit.update_from_mesh(self.mesh)
     Xd = numpy.array([[0.3, 0.15], [0.8, 0.4]])
     fit.set_data('datacloud', Xd)
     mesh, err = fit.solve(self.mesh)
     npt.assert_almost_equal(mesh.get_nodes(), [[0, 0], [1.0, 0.5]])
示例#3
0
 def test_bind_element_point(self):
     fit = fitter.Fit()
     fit.bind_element_point(2, [0.1, 0.3], 'datacloud', weight=2)
     pt = fit.points[0]
     self.assertEqual(pt.eid, 2)
     self.assertEqual(pt.xi, [0.1, 0.3])
     self.assertEqual(pt.data, 'datacloud')
     self.assertEqual(pt.bind_weight, 2)
     self.assertEqual(pt.param_ids, None)
     self.assertEqual(pt.param_weights, None)
示例#4
0
 def test_get_data(self):
     fit = fitter.Fit()
     fit.use_sparse = False
     fit.bind_element_point(9, [0.3], 'datacloud', 1, weight=2)
     fit.bind_element_point(9, [0.8], 'datacloud', 0, weight=2)
     fit.update_from_mesh(self.mesh)
     Xd = numpy.array([[0.1, 0.2], [0.3, 0.4]])
     fit.set_data('datacloud', Xd)
     Xr = fit.get_data(self.mesh)
     npt.assert_almost_equal(Xr, [0.3, 0.4, 0.1, 0.2])
示例#5
0
 def test_update_from_mesh_node_vals_sparse(self):
     fit = fitter.Fit()
     fit.use_sparse = True
     fit.bind_node_value(1, 1, 0, 'datacloud', 1, weight=2)
     fit.bind_node_value(2, 0, 0, 'x0', weight=3)
     fit.update_from_mesh(self.mesh)
     self.assertEqual(fit.num_dof, 2)
     self.assertEqual(fit.num_rows, 2)
     npt.assert_almost_equal(fit.A.toarray(), [[2, 0], [0, 3]])
     self.assertEqual(fit.data_map, [[0, 1], [1, 0]])
     Xd = numpy.array([[0.1, 0.2], [0.3, 0.4]])
     fit.set_data('datacloud', Xd)
     fit.set_data('x0', 2.7)
     Xr = fit.get_data(self.mesh)
     npt.assert_almost_equal(Xr, [0.4, 2.7])
示例#6
0
def fitting(mesh):

    x = scipy.linspace(0, 2 * scipy.pi, 200)
    y = 0.8 * scipy.cos(x)
    Xd = scipy.array([x, y]).T

    mesh.nodes[1].fix([True, False])
    mesh.nodes[7].fix([True, False])
    mesh._core.generate_fixed_index()

    fit = fitter.Fit('m2dc')

    res = 20
    fit.X = scipy.zeros((2 * res, 2))
    fit.Xi = scipy.array([scipy.linspace(0, 1, res)]).T
    mesh = fit.optimize(mesh, Xd)

    plotting_fit(mesh, Xd)
示例#7
0
 def test_update_from_mesh_elem_pts(self):
     fit = fitter.Fit()
     fit.use_sparse = False
     fit.bind_element_point(9, [0.3], 'datacloud', 0, weight=2)
     fit.bind_element_point(9, [0.7], 'datacloud', 1, weight=1)
     fit.update_from_mesh(self.mesh)
     self.assertEqual(fit.num_dof, 4)
     self.assertEqual(fit.num_rows, 4)
     pt = fit.points[0]
     self.assertEqual(pt.param_ids, [[0, 2], [1, 3]])
     npt.assert_almost_equal(pt.param_weights, [0.7, 0.3])
     npt.assert_almost_equal(fit.A, [[1.4, 0, 0.6, 0], [0, 1.4, 0, 0.6],
                                     [0.3, 0, 0.7, 0], [0, 0.3, 0, 0.7]])
     self.assertEqual(fit.data_map, [[0, 0], [0, 1], [1, 0], [1, 1]])
     Xd = numpy.array([[0.1, 0.2], [0.3, 0.4]])
     fit.set_data('datacloud', Xd)
     npt.assert_almost_equal(fit.data['datacloud'].values, Xd)
     Xr = fit.get_data(self.mesh)
     npt.assert_almost_equal(Xr, [0.1, 0.2, 0.3, 0.4])
示例#8
0
    def test_bind_subset_of_fields(self):
        mesh = mesher.Mesh()
        mesh.add_stdnode(1, [0, 0, 0])
        mesh.add_stdnode(2, [2, 0.9, 2.4])
        mesh.add_element('el1', ['L1'], [1, 2])
        mesh.generate()

        fit = fitter.Fit()
        fit.bind_element_point('el1', [0.3], 'x3', weight=2, fields=[0])
        fit.bind_element_point('el1', [0.3],
                               'datacloud',
                               weight=2,
                               fields=[1, 2])
        fit.bind_element_point('el1', [0.8],
                               'datacloud',
                               weight=2,
                               fields=[2, 1])
        fit.bind_element_point('el1', [0.9], 'x9', weight=2, fields=[0])
        fit.update_from_mesh(mesh)

        npt.assert_almost_equal(
            fit.A.toarray(),
            [[1.4, 0., 0., 0.6, 0., 0.], [0., 1.4, 0., 0., 0.6, 0.],
             [0., 0., 1.4, 0., 0., 0.6], [0., 0., 0.4, 0., 0., 1.6],
             [0., 0.4, 0., 0., 1.6, 0.], [0.2, 0., 0., 1.8, 0., 0.]])

        Xd = numpy.array([[0, 0, 0], [0.5, 0.3, 1.1], [1.7, 0.8, 2.1],
                          [1, 0.5, 1.5]])
        fit.set_data('datacloud', Xd)
        fit.set_data('x3', 0.6)
        fit.set_data('x9', 1.8)
        fit.generate_fast_data()

        for data in fit.data:
            data.update_point_data(mesh._core.P[fit.param_ids])

        b = fit.get_data(mesh)
        npt.assert_almost_equal(b, [0.6, 0.3, 1.1, 2.1, 0.8, 1.8])

        mesh, err = fit.solve(mesh)
        npt.assert_almost_equal(mesh.get_nodes(),
                                [[0, 0, 0.5], [2.0, 1.0, 2.5]])
示例#9
0
 def test_init(self):
     fit = fitter.Fit()
     self.assertEqual(fit.use_sparse, True)
示例#10
0
 def test_set_data(self):
     fit = fitter.Fit()
     Xd = numpy.array([[0.1, 0.2], [0.3, 0.4]])
     fit.set_data('datacloud', Xd)
     npt.assert_almost_equal(fit.data['datacloud'].values, Xd)
示例#11
0
def generate():
    x = scipy.linspace(0, 2 * scipy.pi, 7)
    y = scipy.cos(x)
    #~ y = scipy.zeros(x.shape[0])
    X = scipy.array([x, y]).T
    
    # Start Generate Mesh
    mesh = mesher.Mesh() # Instantiate a mesh
    
    # Add nodes
    for ind, x in enumerate(X):
        mesh.add_stdnode(ind + 1, x)
    
    # Add two quadratic elements each having 3 nodes
    mesh.add_element(1, ['L3'], [1, 2, 3, 4])
    mesh.add_element(2, ['L3'], [4, 5, 6, 7])
    
    fit = fitter.Fit()
    Xi = scipy.linspace(0, 1, 30)
    did = 0
    for elem in mesh.elements:
        for xi in Xi:
            if elem.id == 1:
                weight = 1 + xi**4 * 100
            else:
                weight = 1 + (1-xi)**4 * 100
            #~ weight = 1
            #~ fit.bind_element_point(elem.id, [xi], 'mydata', data_index=did, weight=weight)
            fit.bind_element_point(elem.id, [xi], 'mydata', weight=weight)
            did += 1
    
    Xi = scipy.linspace(0, 1, 100)
    Xd = mesh.evaluate(1, Xi)
    Xd = scipy.append(Xd, mesh.evaluate(2, Xi), axis=0)
    Xd[:,1] = scipy.cos(Xd[:,0])
    
    fit.set_data('mydata', Xd)
    
    XdA = scipy.array([[0, 0.], [scipy.pi, -1]])
    XdB = scipy.array([[2 * scipy.pi, 0.8]])
    fit.set_data('nodeA', XdA)
    fit.set_data('nodeB', XdB)
    
    fit.bind_node_point(1, 'nodeA', data_index=0, weight=10, param=None)
    #~ fit.bind_node_point(4, 'nodeA', data_index=1, weight=10)
    fit.bind_node_point(7, 'nodeB', data_index=0, weight=10, param=0)
    fit.bind_node_point(7, 'nodeB', data_index=0, weight=10, param=1)
    
    fit.update_from_mesh(mesh)
    fit.invert_matrix()
    mesh = fit.solve(mesh, niter=100, output=True)
    #~ mesh.generate(True)
    
    pylab.figure(1)
    pylab.clf()
    pylab.ion()
    
    pylab.plot(Xd[:,0], Xd[:,1], ',r') 
    
    Xn = mesh.get_nodes()
    pylab.plot(Xn[:,0], Xn[:,1], 'o') 
    
    Xl = mesh.get_lines(res=32)
    for xl in Xl:
        pylab.plot(xl[:,0], xl[:,1], 'b')
    
    pylab.ioff()
    pylab.axis([-0.3, 6.5, -1.2, 1.2])
    pylab.draw()
    pylab.show()
    
    return mesh, fit