void MooseVariable::add(NumericVector<Number> & residual) { if (_has_nodal_value) residual.add_vector(&_nodal_u[0], _dof_indices); if (_has_nodal_value_neighbor) residual.add_vector(&_nodal_u_neighbor[0], _dof_indices_neighbor); }
void AssembleOptimization::assemble_A_and_F() { A_matrix->zero(); F_vector->zero(); const MeshBase & mesh = _sys.get_mesh(); const unsigned int dim = mesh.mesh_dimension(); const unsigned int u_var = _sys.variable_number ("u"); const DofMap & dof_map = _sys.get_dof_map(); FEType fe_type = dof_map.variable_type(u_var); UniquePtr<FEBase> fe (FEBase::build(dim, fe_type)); QGauss qrule (dim, fe_type.default_quadrature_order()); fe->attach_quadrature_rule (&qrule); const std::vector<Real> & JxW = fe->get_JxW(); const std::vector<std::vector<Real> > & phi = fe->get_phi(); const std::vector<std::vector<RealGradient> > & dphi = fe->get_dphi(); std::vector<dof_id_type> dof_indices; DenseMatrix<Number> Ke; DenseVector<Number> Fe; MeshBase::const_element_iterator el = mesh.active_local_elements_begin(); const MeshBase::const_element_iterator end_el = mesh.active_local_elements_end(); for ( ; el != end_el; ++el) { const Elem * elem = *el; dof_map.dof_indices (elem, dof_indices); const unsigned int n_dofs = dof_indices.size(); fe->reinit (elem); Ke.resize (n_dofs, n_dofs); Fe.resize (n_dofs); for (unsigned int qp=0; qp<qrule.n_points(); qp++) { for (unsigned int dof_i=0; dof_i<n_dofs; dof_i++) { for (unsigned int dof_j=0; dof_j<n_dofs; dof_j++) { Ke(dof_i, dof_j) += JxW[qp] * (dphi[dof_j][qp]* dphi[dof_i][qp]); } Fe(dof_i) += JxW[qp] * phi[dof_i][qp]; } } A_matrix->add_matrix (Ke, dof_indices); F_vector->add_vector (Fe, dof_indices); } A_matrix->close(); F_vector->close(); }
void SparseMatrix<T>::vector_mult_add (NumericVector<T>& dest, const NumericVector<T>& arg) const { /* This functionality is actually implemented in the \p NumericVector class. */ dest.add_vector(arg,*this); }
void Assembly::addResidualBlock(NumericVector<Number> & residual, DenseVector<Number> & res_block, const std::vector<dof_id_type> & dof_indices, Real scaling_factor) { if (dof_indices.size() > 0 && res_block.size()) { _temp_dof_indices = dof_indices; _dof_map.constrain_element_vector(res_block, _temp_dof_indices, false); if (scaling_factor != 1.0) { _tmp_Re = res_block; _tmp_Re *= scaling_factor; residual.add_vector(_tmp_Re, _temp_dof_indices); } else { residual.add_vector(res_block, _temp_dof_indices); } } }
void Assembly::addCachedResidual(NumericVector<Number> & residual, Moose::KernelType type) { std::vector<Real> & cached_residual_values = _cached_residual_values[type]; std::vector<unsigned int> & cached_residual_rows = _cached_residual_rows[type]; mooseAssert(cached_residual_values.size() == cached_residual_rows.size(), "Number of cached residuals and number of rows must match!"); residual.add_vector(cached_residual_values, cached_residual_rows); if (_max_cached_residuals < cached_residual_values.size()) _max_cached_residuals = cached_residual_values.size(); // Try to be more efficient from now on // The 2 is just a fudge factor to keep us from having to grow the vector during assembly cached_residual_values.clear(); cached_residual_values.reserve(_max_cached_residuals*2); cached_residual_rows.clear(); cached_residual_rows.reserve(_max_cached_residuals*2); }
// Here we compute the residual R(x) = K(x)*x - f. The current solution // x is passed in the soln vector void compute_residual (const NumericVector<Number>& soln, NumericVector<Number>& residual, NonlinearImplicitSystem& sys) { EquationSystems &es = *_equation_system; // Get a constant reference to the mesh object. const MeshBase& mesh = es.get_mesh(); // The dimension that we are running const unsigned int dim = mesh.mesh_dimension(); libmesh_assert_equal_to (dim, 2); // Get a reference to the NonlinearImplicitSystem we are solving NonlinearImplicitSystem& system = es.get_system<NonlinearImplicitSystem>("Laplace-Young"); // A reference to the \p DofMap object for this system. The \p DofMap // object handles the index translation from node and element numbers // to degree of freedom numbers. We will talk more about the \p DofMap // in future examples. const DofMap& dof_map = system.get_dof_map(); // Get a constant reference to the Finite Element type // for the first (and only) variable in the system. FEType fe_type = dof_map.variable_type(0); // Build a Finite Element object of the specified type. Since the // \p FEBase::build() member dynamically creates memory we will // store the object as an \p AutoPtr<FEBase>. This can be thought // of as a pointer that will clean up after itself. AutoPtr<FEBase> fe (FEBase::build(dim, fe_type)); // A 5th order Gauss quadrature rule for numerical integration. QGauss qrule (dim, FIFTH); // Tell the finite element object to use our quadrature rule. fe->attach_quadrature_rule (&qrule); // Declare a special finite element object for // boundary integration. AutoPtr<FEBase> fe_face (FEBase::build(dim, fe_type)); // Boundary integration requires one quadraure rule, // with dimensionality one less than the dimensionality // of the element. QGauss qface(dim-1, FIFTH); // Tell the finte element object to use our // quadrature rule. fe_face->attach_quadrature_rule (&qface); // Here we define some references to cell-specific data that // will be used to assemble the linear system. // We begin with the element Jacobian * quadrature weight at each // integration point. const std::vector<Real>& JxW = fe->get_JxW(); // The element shape functions evaluated at the quadrature points. const std::vector<std::vector<Real> >& phi = fe->get_phi(); // The element shape function gradients evaluated at the quadrature // points. const std::vector<std::vector<RealGradient> >& dphi = fe->get_dphi(); // Define data structures to contain the resdual contributions DenseVector<Number> Re; // This vector will hold the degree of freedom indices for // the element. These define where in the global system // the element degrees of freedom get mapped. std::vector<unsigned int> dof_indices; // Now we will loop over all the active elements in the mesh which // are local to this processor. // We will compute the element residual. residual.zero(); MeshBase::const_element_iterator el = mesh.active_local_elements_begin(); const MeshBase::const_element_iterator end_el = mesh.active_local_elements_end(); for ( ; el != end_el; ++el) { // Store a pointer to the element we are currently // working on. This allows for nicer syntax later. const Elem* elem = *el; // Get the degree of freedom indices for the // current element. These define where in the global // matrix and right-hand-side this element will // contribute to. dof_map.dof_indices (elem, dof_indices); // Compute the element-specific data for the current // element. This involves computing the location of the // quadrature points (q_point) and the shape functions // (phi, dphi) for the current element. fe->reinit (elem); // We use the resize member here because // the number of degrees of freedom might have changed from // the last element. Note that this will be the case if the // element type is different (i.e. the last element was a // triangle, now we are on a quadrilateral). Re.resize (dof_indices.size()); // Now we will build the residual. This involves // the construction of the matrix K and multiplication of it // with the current solution x. We rearrange this into two loops: // In the first, we calculate only the contribution of // K_ij*x_j which is independent of the row i. In the second loops, // we multiply with the row-dependent part and add it to the element // residual. for (unsigned int qp=0; qp<qrule.n_points(); qp++) { Number u = 0; Gradient grad_u; for (unsigned int j=0; j<phi.size(); j++) { u += phi[j][qp]*soln(dof_indices[j]); grad_u += dphi[j][qp]*soln(dof_indices[j]); } const Number K = 1./std::sqrt(1. + grad_u*grad_u); for (unsigned int i=0; i<phi.size(); i++) Re(i) += JxW[qp]*( K*(dphi[i][qp]*grad_u) + kappa*phi[i][qp]*u ); } // At this point the interior element integration has // been completed. However, we have not yet addressed // boundary conditions. // The following loops over the sides of the element. // If the element has no neighbor on a side then that // side MUST live on a boundary of the domain. for (unsigned int side=0; side<elem->n_sides(); side++) if (elem->neighbor(side) == NULL) { // The value of the shape functions at the quadrature // points. const std::vector<std::vector<Real> >& phi_face = fe_face->get_phi(); // The Jacobian * Quadrature Weight at the quadrature // points on the face. const std::vector<Real>& JxW_face = fe_face->get_JxW(); // Compute the shape function values on the element face. fe_face->reinit(elem, side); // Loop over the face quadrature points for integration. for (unsigned int qp=0; qp<qface.n_points(); qp++) { // This is the right-hand-side contribution (f), // which has to be subtracted from the current residual for (unsigned int i=0; i<phi_face.size(); i++) Re(i) -= JxW_face[qp]*sigma*phi_face[i][qp]; } } dof_map.constrain_element_vector (Re, dof_indices); residual.add_vector (Re, dof_indices); } // That's it. }
void AssembleOptimization::assemble_A_and_F() { A_matrix->zero(); F_vector->zero(); const MeshBase & mesh = _sys.get_mesh(); const unsigned int dim = mesh.mesh_dimension(); const unsigned int u_var = _sys.variable_number ("u"); const DofMap & dof_map = _sys.get_dof_map(); FEType fe_type = dof_map.variable_type(u_var); UniquePtr<FEBase> fe (FEBase::build(dim, fe_type)); QGauss qrule (dim, fe_type.default_quadrature_order()); fe->attach_quadrature_rule (&qrule); const std::vector<Real> & JxW = fe->get_JxW(); const std::vector<std::vector<Real> > & phi = fe->get_phi(); const std::vector<std::vector<RealGradient> > & dphi = fe->get_dphi(); std::vector<dof_id_type> dof_indices; DenseMatrix<Number> Ke; DenseVector<Number> Fe; MeshBase::const_element_iterator el = mesh.active_local_elements_begin(); const MeshBase::const_element_iterator end_el = mesh.active_local_elements_end(); for ( ; el != end_el; ++el) { const Elem * elem = *el; dof_map.dof_indices (elem, dof_indices); const unsigned int n_dofs = dof_indices.size(); fe->reinit (elem); Ke.resize (n_dofs, n_dofs); Fe.resize (n_dofs); for (unsigned int qp=0; qp<qrule.n_points(); qp++) { for (unsigned int dof_i=0; dof_i<n_dofs; dof_i++) { for (unsigned int dof_j=0; dof_j<n_dofs; dof_j++) { Ke(dof_i, dof_j) += JxW[qp] * (dphi[dof_j][qp]* dphi[dof_i][qp]); } Fe(dof_i) += JxW[qp] * phi[dof_i][qp]; } } // This will zero off-diagonal entries of Ke corresponding to // Dirichlet dofs. dof_map.constrain_element_matrix_and_vector (Ke, Fe, dof_indices); // We want the diagonal of constrained dofs to be zero too for (unsigned int local_dof_index=0; local_dof_index<n_dofs; local_dof_index++) { dof_id_type global_dof_index = dof_indices[local_dof_index]; if (dof_map.is_constrained_dof(global_dof_index)) { Ke(local_dof_index, local_dof_index) = 0.; } } A_matrix->add_matrix (Ke, dof_indices); F_vector->add_vector (Fe, dof_indices); } A_matrix->close(); F_vector->close(); }
/** * Evaluate the residual of the nonlinear system. */ virtual void residual (const NumericVector<Number> & soln, NumericVector<Number> & residual, NonlinearImplicitSystem & /*sys*/) { const Real young_modulus = es.parameters.get<Real>("young_modulus"); const Real poisson_ratio = es.parameters.get<Real>("poisson_ratio"); const Real forcing_magnitude = es.parameters.get<Real>("forcing_magnitude"); const MeshBase & mesh = es.get_mesh(); const unsigned int dim = mesh.mesh_dimension(); NonlinearImplicitSystem & system = es.get_system<NonlinearImplicitSystem>("NonlinearElasticity"); const unsigned int u_var = system.variable_number ("u"); const DofMap & dof_map = system.get_dof_map(); FEType fe_type = dof_map.variable_type(u_var); std::unique_ptr<FEBase> fe (FEBase::build(dim, fe_type)); QGauss qrule (dim, fe_type.default_quadrature_order()); fe->attach_quadrature_rule (&qrule); std::unique_ptr<FEBase> fe_face (FEBase::build(dim, fe_type)); QGauss qface (dim-1, fe_type.default_quadrature_order()); fe_face->attach_quadrature_rule (&qface); const std::vector<Real> & JxW = fe->get_JxW(); const std::vector<std::vector<Real>> & phi = fe->get_phi(); const std::vector<std::vector<RealGradient>> & dphi = fe->get_dphi(); DenseVector<Number> Re; DenseSubVector<Number> Re_var[3] = {DenseSubVector<Number>(Re), DenseSubVector<Number>(Re), DenseSubVector<Number>(Re)}; std::vector<dof_id_type> dof_indices; std::vector<std::vector<dof_id_type>> dof_indices_var(3); residual.zero(); for (const auto & elem : mesh.active_local_element_ptr_range()) { dof_map.dof_indices (elem, dof_indices); for (unsigned int var=0; var<3; var++) dof_map.dof_indices (elem, dof_indices_var[var], var); const unsigned int n_dofs = dof_indices.size(); const unsigned int n_var_dofs = dof_indices_var[0].size(); fe->reinit (elem); Re.resize (n_dofs); for (unsigned int var=0; var<3; var++) Re_var[var].reposition (var*n_var_dofs, n_var_dofs); for (unsigned int qp=0; qp<qrule.n_points(); qp++) { DenseVector<Number> u_vec(3); DenseMatrix<Number> grad_u(3, 3); for (unsigned int var_i=0; var_i<3; var_i++) { for (unsigned int j=0; j<n_var_dofs; j++) u_vec(var_i) += phi[j][qp]*soln(dof_indices_var[var_i][j]); // Row is variable u, v, or w column is x, y, or z for (unsigned int var_j=0; var_j<3; var_j++) for (unsigned int j=0; j<n_var_dofs; j++) grad_u(var_i,var_j) += dphi[j][qp](var_j)*soln(dof_indices_var[var_i][j]); } DenseMatrix<Number> strain_tensor(3, 3); for (unsigned int i=0; i<3; i++) for (unsigned int j=0; j<3; j++) { strain_tensor(i,j) += 0.5 * (grad_u(i,j) + grad_u(j,i)); for (unsigned int k=0; k<3; k++) strain_tensor(i,j) += 0.5 * grad_u(k,i)*grad_u(k,j); } // Define the deformation gradient DenseMatrix<Number> F(3, 3); F = grad_u; for (unsigned int var=0; var<3; var++) F(var, var) += 1.; DenseMatrix<Number> stress_tensor(3, 3); for (unsigned int i=0; i<3; i++) for (unsigned int j=0; j<3; j++) for (unsigned int k=0; k<3; k++) for (unsigned int l=0; l<3; l++) stress_tensor(i,j) += elasticity_tensor(young_modulus, poisson_ratio, i, j, k, l) * strain_tensor(k,l); DenseVector<Number> f_vec(3); f_vec(0) = 0.; f_vec(1) = 0.; f_vec(2) = -forcing_magnitude; for (unsigned int dof_i=0; dof_i<n_var_dofs; dof_i++) for (unsigned int i=0; i<3; i++) { for (unsigned int j=0; j<3; j++) { Number FxStress_ij = 0.; for (unsigned int m=0; m<3; m++) FxStress_ij += F(i,m) * stress_tensor(m,j); Re_var[i](dof_i) += JxW[qp] * (-FxStress_ij * dphi[dof_i][qp](j)); } Re_var[i](dof_i) += JxW[qp] * (f_vec(i) * phi[dof_i][qp]); } } dof_map.constrain_element_vector (Re, dof_indices); residual.add_vector (Re, dof_indices); } }