Ejemplo n.º 1
0
 def solve_nonlinear(self, inputs, outputs, residuals):
     theta = inputs[self._cycle_names['theta']]
     psi = inputs[self._cycle_names['psi']]
     A = _compute_A(self.size, psi)
     y = A.dot(np.ones(self.size))
     self._vector_to_outputs(y, outputs)
     outputs[self._cycle_names['theta_out']] = theta
Ejemplo n.º 2
0
 def solve_nonlinear(self, inputs, outputs, residuals):
     theta = inputs[self._cycle_names['theta']]
     A = _compute_A(self.size, theta)
     x = self._inputs_to_vector(inputs)
     y = A.dot(x)
     self._vector_to_outputs(y, outputs)
     outputs[self._cycle_names['theta_out']] = theta
Ejemplo n.º 3
0
    def linearize(self, inputs, outputs, resids):
        if self.metadata['jacobian_type'] != 'matvec':
            partials = {}

            angle_param = self._cycle_names[self.angle_param]
            angle = inputs[angle_param]
            num_var = self.num_var
            var_shape = self.var_shape
            var_size = np.prod(var_shape)
            x = self._inputs_to_vector(inputs)
            size = self.size
            A = _compute_A(size, angle)
            dA = _compute_dA(size, angle)
            dA_x = np.atleast_2d(dA.dot(x)).T
            pd_type = self.metadata['partial_type']
            dtheta = np.array([[1.]])

            y_name = self._cycle_names['y']
            x_name = self._cycle_names['x']

            for out_idx in range(num_var):
                out_var = y_name.format(out_idx)
                for in_idx in range(num_var):
                    in_var = x_name.format(in_idx)
                    Aij = A[array_idx(out_idx, var_size),
                            array_idx(in_idx, var_size)]
                    J_y_x = self.make_jacobian_entry(Aij, pd_type)
                    J_y_angle = self.make_jacobian_entry(
                        dA_x[array_idx(out_idx, var_size)], pd_type)

                    partials[out_var, in_var] = J_y_x
                    partials[out_var, angle_param] = J_y_angle

            theta_out = self._cycle_names['theta_out']
            theta = self._cycle_names['theta']
            partials[theta_out,
                     theta] = self.make_jacobian_entry(dtheta, pd_type)

            return partials
Ejemplo n.º 4
0
    def jac_vec(self, inputs, outputs, d_inputs, d_outputs, d_residuals, mode):
        # Turned into apply_linear for matvec tests.
        if self.metadata['jacobian_type'] == 'matvec':
            angle_param = self._cycle_names[self.angle_param]
            x = self._inputs_to_vector(inputs)
            angle = inputs[angle_param]
            A = _compute_A(self.size, angle)
            dA = _compute_dA(self.size, angle)

            var_shape = self.metadata['var_shape']
            var_size = np.prod(var_shape)
            num_var = self.metadata['num_var']
            x_name = self._cycle_names['x']
            y_name = self._cycle_names['y']
            theta_name = self._cycle_names['theta']
            theta_out_name = self._cycle_names['theta_out']

            if mode == 'fwd':
                for j in range(num_var):
                    x_j = x_name.format(j)
                    if x_j in d_inputs:
                        dx = d_inputs[x_j].flat[:]
                        for i in range(num_var):
                            y_i = y_name.format(i)
                            if y_i in d_residuals:
                                Aij = A[array_idx(i, var_size),
                                        array_idx(j, var_size)]
                                d_residuals[y_i] += Aij.dot(dx).reshape(
                                    var_shape)

                if theta_name in d_inputs and theta_out_name in d_residuals:
                    dtheta = d_inputs[theta_name]
                    d_residuals[theta_out_name] += dtheta

                if angle_param in d_inputs:
                    dangle = d_inputs[angle_param]
                    dy_dangle = (dA.dot(x)) * dangle
                    for i in range(num_var):
                        y_i = y_name.format(i)
                        if y_i in d_residuals:
                            d_residuals[y_i] += dy_dangle[array_idx(
                                i, var_size)].reshape(var_shape)

            elif mode == 'rev':
                for i in range(num_var):
                    y_i = y_name.format(i)
                    if y_i in d_residuals:
                        dy_i = d_residuals[y_i].flat[:]
                        for j in range(num_var):
                            x_j = x_name.format(j)
                            if x_j in d_inputs:
                                Aij = A[array_idx(i, var_size),
                                        array_idx(j, var_size)]
                                d_inputs[x_j] += Aij.T.dot(dy_i).reshape(
                                    var_shape)
                            if angle_param in d_inputs:
                                dAij = dA[array_idx(i, var_size),
                                          array_idx(j, var_size)]
                                x_j_vec = inputs[x_j].flat[:]
                                d_inputs[angle_param] += x_j_vec.T.dot(
                                    dAij.T.dot(dy_i))

                if theta_out_name in d_residuals and theta_name in d_inputs:
                    dtheta_out = d_residuals[theta_out_name]
                    d_inputs[theta_name] += dtheta_out