35 #include "libmesh/nonlinear_solver.h" 36 #include "libmesh/numeric_vector.h" 37 #include "libmesh/sparse_matrix.h" 38 #include "libmesh/dof_map.h" 39 #include "libmesh/petsc_vector.h" 40 #include "libmesh/petsc_matrix.h" 47 _base_sol_sensitivity (nullptr) {
96 const libMesh::NumericVector<Real>&
111 const libMesh::NumericVector<Real>& imag) {
113 START_LOG(
"complex_solve()",
"Residual-L2");
131 std::vector<libMesh::dof_id_type> dof_indices;
132 const libMesh::DofMap& dof_map = nonlin_sys.get_dof_map();
135 std::unique_ptr<libMesh::NumericVector<Real> >
136 residual_re(nonlin_sys.solution->zero_clone().release()),
137 residual_im(nonlin_sys.solution->zero_clone().release()),
138 localized_base_solution,
153 libMesh::MeshBase::const_element_iterator el =
154 nonlin_sys.get_mesh().active_local_elements_begin();
155 const libMesh::MeshBase::const_element_iterator end_el =
156 nonlin_sys.get_mesh().active_local_elements_end();
161 for ( ; el != end_el; ++el) {
163 const libMesh::Elem* elem = *el;
165 dof_map.dof_indices (elem, dof_indices);
174 unsigned int ndofs = (
unsigned int)dof_indices.size();
176 delta_sol.setZero(ndofs);
178 mat.setZero(ndofs, ndofs);
185 for (
unsigned int i=0; i<dof_indices.size(); i++)
186 sol(i) = (*localized_base_solution)(dof_indices[i]);
190 for (
unsigned int i=0; i<dof_indices.size(); i++)
191 delta_sol(i) =
Complex((*localized_real_solution)(dof_indices[i]),
192 (*localized_imag_solution)(dof_indices[i]));
212 dof_map.constrain_element_vector(v, dof_indices);
213 residual_re->add_vector(v, dof_indices);
219 dof_map.constrain_element_vector(v, dof_indices);
220 residual_im->add_vector(v, dof_indices);
229 residual_re->close();
230 residual_im->close();
234 l2_real = residual_re->l2_norm(),
235 l2_imag = residual_im->l2_norm();
237 STOP_LOG(
"complex_solve()",
"Residual-L2");
239 return sqrt(pow(l2_real,2) + pow(l2_imag,2));
247 const libMesh::NumericVector<Real>& X_I,
248 libMesh::NumericVector<Real>& R_R,
249 libMesh::NumericVector<Real>& R_I,
250 libMesh::SparseMatrix<Real>& J_R,
251 libMesh::SparseMatrix<Real>& J_I) {
271 std::vector<libMesh::dof_id_type> dof_indices;
272 const libMesh::DofMap& dof_map =
_system->
system().get_dof_map();
275 std::unique_ptr<libMesh::NumericVector<Real> >
276 localized_base_solution,
277 localized_real_solution,
278 localized_imag_solution;
300 libMesh::MeshBase::const_element_iterator el =
301 nonlin_sys.get_mesh().active_local_elements_begin();
302 const libMesh::MeshBase::const_element_iterator end_el =
303 nonlin_sys.get_mesh().active_local_elements_end();
308 for ( ; el != end_el; ++el) {
310 const libMesh::Elem* elem = *el;
312 dof_map.dof_indices (elem, dof_indices);
321 unsigned int ndofs = (
unsigned int)dof_indices.size();
323 delta_sol.setZero(ndofs);
325 mat.setZero(ndofs, ndofs);
332 for (
unsigned int i=0; i<dof_indices.size(); i++)
333 sol(i) = (*localized_base_solution)(dof_indices[i]);
338 for (
unsigned int i=0; i<dof_indices.size(); i++)
339 delta_sol(i) =
Complex((*localized_real_solution)(dof_indices[i]),
340 (*localized_imag_solution)(dof_indices[i]));
373 dof_map.constrain_element_matrix_and_vector(m, v, dof_indices);
374 R_R.add_vector(v, dof_indices);
375 J_R.add_matrix(m, dof_indices);
384 dof_map.constrain_element_matrix_and_vector(m, v, dof_indices);
385 R_I.add_vector(v, dof_indices);
386 J_I.add_matrix(m, dof_indices);
400 libMesh::out <<
"R_R: " << R_R.l2_norm() <<
" R_I: " << R_I.l2_norm() << std::endl;
412 libMesh::NumericVector<Real>& R,
413 libMesh::SparseMatrix<Real>& J,
420 START_LOG(
"residual_and_jacobian()",
"ComplexSolve");
440 jac_bmat =
dynamic_cast<libMesh::PetscMatrix<Real>&
>(J).mat();
444 std::vector<libMesh::dof_id_type> dof_indices;
445 const libMesh::DofMap& dof_map = nonlin_sys.get_dof_map();
446 const std::vector<libMesh::dof_id_type>&
447 send_list = nonlin_sys.get_dof_map().get_send_list();
451 std::unique_ptr<libMesh::NumericVector<Real> >
452 localized_base_solution,
453 localized_complex_sol(libMesh::NumericVector<Real>::build(nonlin_sys.comm()).release());
456 std::vector<libMesh::dof_id_type>
457 complex_send_list(2*send_list.size());
459 for (
unsigned int i=0; i<send_list.size(); i++) {
460 complex_send_list[2*i ] = 2*send_list[i];
461 complex_send_list[2*i+1] = 2*send_list[i]+1;
464 localized_complex_sol->init(2*nonlin_sys.n_dofs(),
465 2*nonlin_sys.n_local_dofs(),
469 X.localize(*localized_complex_sol, complex_send_list);
483 libMesh::MeshBase::const_element_iterator el =
484 nonlin_sys.get_mesh().active_local_elements_begin();
485 const libMesh::MeshBase::const_element_iterator end_el =
486 nonlin_sys.get_mesh().active_local_elements_end();
491 for ( ; el != end_el; ++el) {
493 const libMesh::Elem* elem = *el;
495 dof_map.dof_indices (elem, dof_indices);
504 unsigned int ndofs = (
unsigned int)dof_indices.size();
506 delta_sol.setZero(ndofs);
508 mat.setZero(ndofs, ndofs);
515 for (
unsigned int i=0; i<dof_indices.size(); i++)
516 sol(i) = (*localized_base_solution)(dof_indices[i]);
521 for (
unsigned int i=0; i<dof_indices.size(); i++) {
524 delta_sol(i) =
Complex((*localized_complex_sol)(2*dof_indices[i]),
525 (*localized_complex_sol)(2*dof_indices[i]+1));
562 std::vector<Real> vals(4);
570 dof_map.constrain_element_matrix(m_R, dof_indices);
571 dof_map.constrain_element_matrix(m_I1, dof_indices);
572 dof_map.constrain_element_matrix(m_I2, dof_indices);
573 dof_map.constrain_element_vector(v_R, dof_indices);
574 dof_map.constrain_element_vector(v_I, dof_indices);
577 for (
unsigned int i=0; i<dof_indices.size(); i++) {
579 R.add(2*dof_indices[i], v_R(i));
580 R.add(2*dof_indices[i]+1, v_I(i));
582 for (
unsigned int j=0; j<dof_indices.size(); j++) {
587 ierr = MatSetValuesBlocked(jac_bmat,
588 1, (PetscInt*)&dof_indices[i],
589 1, (PetscInt*)&dof_indices[j],
604 libMesh::out <<
"R: " << R.l2_norm() << std::endl;
605 STOP_LOG(
"residual_and_jacobian()",
"ComplexSolve");
616 libMesh::NumericVector<Real>& sensitivity_rhs) {
MAST::AssemblyElemOperations * _elem_ops
provides assembly elem operations for use by this class
MAST::NonlinearSystem & system()
libMesh::DenseMatrix< Real > DenseRealMatrix
void residual_and_jacobian_blocked(const libMesh::NumericVector< Real > &X, libMesh::NumericVector< Real > &R, libMesh::SparseMatrix< Real > &J, MAST::Parameter *p=nullptr)
Assembles the residual and Jacobian of the N complex system of equations split into 2N real system of...
This class implements a system for solution of nonlinear systems.
void residual_and_jacobian_field_split(const libMesh::NumericVector< Real > &X_R, const libMesh::NumericVector< Real > &X_I, libMesh::NumericVector< Real > &R_R, libMesh::NumericVector< Real > &R_I, libMesh::SparseMatrix< Real > &J_R, libMesh::SparseMatrix< Real > &J_I)
virtual ~ComplexAssemblyBase()
destructor resets the association of this assembly object with the system
const libMesh::NumericVector< Real > * _base_sol_sensitivity
sensitivity of base solution may be needed for sensitivity analysis.
virtual void set_elem_complex_solution_sensitivity(const ComplexVectorX &sol)
sets the element complex solution
Matrix< Complex, Dynamic, 1 > ComplexVectorX
This is a scalar function whose value can be changed and one that can be used as a design variable in...
Real residual_l2_norm(const libMesh::NumericVector< Real > &real, const libMesh::NumericVector< Real > &imag)
calculates the L2 norm of the residual complex system of equations.
void set_base_solution(const libMesh::NumericVector< Real > &sol, bool if_sens=false)
if the problem is defined about a non-zero base solution, then this method provides the object with t...
virtual void set_elem_solution(const RealVectorX &sol)
sets the element solution
MAST::SystemInitialization * _system
System for which this assembly is performed.
MAST::PhysicsDisciplineBase * _discipline
PhysicsDisciplineBase object for which this class is assembling.
virtual void set_elem_data(unsigned int dim, const libMesh::Elem &ref_elem, MAST::GeomElem &elem) const =0
some analyses may want to set additional element data before initialization of the GeomElem...
virtual void elem_sensitivity_calculations(const MAST::FunctionBase &f, ComplexVectorX &vec)=0
performs the element sensitivity calculations over elem, and returns the element residual sensitivity...
libMesh::DenseVector< Real > DenseRealVector
Matrix< Real, Dynamic, Dynamic > RealMatrixX
Matrix< Complex, Dynamic, Dynamic > ComplexMatrixX
void copy(DenseRealMatrix &m1, const RealMatrixX &m2)
void clear()
clear the solution
Matrix< Real, Dynamic, 1 > RealVectorX
virtual void init(const MAST::GeomElem &elem)=0
initializes the object for calculation of element quantities for the specified elem.
virtual void set_elem_complex_solution(const ComplexVectorX &sol)
sets the element complex solution
std::unique_ptr< libMesh::NumericVector< Real > > build_localized_vector(const libMesh::System &sys, const libMesh::NumericVector< Real > &global) const
localizes the parallel vector so that the local copy stores all values necessary for calculation of t...
This class acts as a wrapper around libMesh::Elem for the purpose of providing a uniform interface fo...
virtual void set_elem_velocity(const RealVectorX &vel)
sets the element velocity
ComplexAssemblyBase()
constructor associates this assembly object with the system
void clear_base_solution(bool if_sens=false)
Clears pointer to the solution vector The flag if_sens tells the method to clear pointer of the solut...
const libMesh::NumericVector< Real > * _base_sol
base solution about which this problem is defined.
virtual void elem_calculations(bool if_jac, ComplexVectorX &vec, ComplexMatrixX &mat)=0
performs the element calculations over elem, and returns the element vector and matrix quantities in ...
virtual void clear_elem()
clears the element initialization
virtual void init(const libMesh::Elem &elem, const MAST::SystemInitialization &sys_init)
initialize the object for the specified reference elem.
virtual bool sensitivity_assemble(const MAST::FunctionBase &f, libMesh::NumericVector< Real > &sensitivity_rhs)
Assembly function.
const libMesh::NumericVector< Real > & base_sol(bool if_sens=false) const
MAST::MeshFieldFunction * _sol_function
system solution that will be initialized before each solution