diff_context.C
Go to the documentation of this file.00001 // The libMesh Finite Element Library. 00002 // Copyright (C) 2002-2012 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner 00003 00004 // This library is free software; you can redistribute it and/or 00005 // modify it under the terms of the GNU Lesser General Public 00006 // License as published by the Free Software Foundation; either 00007 // version 2.1 of the License, or (at your option) any later version. 00008 00009 // This library is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // Lesser General Public License for more details. 00013 00014 // You should have received a copy of the GNU Lesser General Public 00015 // License along with this library; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 00019 #include "libmesh/diff_context.h" 00020 #include "libmesh/diff_system.h" 00021 00022 namespace libMesh 00023 { 00024 00025 00026 00027 DiffContext::DiffContext (const System& sys) : 00028 time(sys.time), 00029 system_time(sys.time), 00030 elem_solution_derivative(1.), 00031 fixed_solution_derivative(0.), 00032 dof_indices_var(sys.n_vars()), 00033 _deltat(NULL), 00034 _system(sys), 00035 _is_adjoint(false) 00036 { 00037 // Finally initialize solution/residual/jacobian data structures 00038 unsigned int nv = sys.n_vars(); 00039 00040 elem_subsolutions.reserve(nv); 00041 elem_subresiduals.reserve(nv); 00042 elem_subjacobians.resize(nv); 00043 if (sys.use_fixed_solution) 00044 elem_fixed_subsolutions.reserve(nv); 00045 00046 // If the user resizes sys.qoi, it will invalidate us 00047 std::size_t n_qoi = sys.qoi.size(); 00048 elem_qoi.resize(n_qoi); 00049 elem_qoi_derivative.resize(n_qoi); 00050 elem_qoi_subderivatives.resize(n_qoi); 00051 for (std::size_t q=0; q != n_qoi; ++q) 00052 elem_qoi_subderivatives[q].reserve(nv); 00053 00054 for (unsigned int i=0; i != nv; ++i) 00055 { 00056 elem_subsolutions.push_back(new DenseSubVector<Number>(elem_solution)); 00057 elem_subresiduals.push_back(new DenseSubVector<Number>(elem_residual)); 00058 for (std::size_t q=0; q != n_qoi; ++q) 00059 elem_qoi_subderivatives[q].push_back(new DenseSubVector<Number>(elem_qoi_derivative[q])); 00060 elem_subjacobians[i].reserve(nv); 00061 00062 if (sys.use_fixed_solution) 00063 elem_fixed_subsolutions.push_back 00064 (new DenseSubVector<Number>(elem_fixed_solution)); 00065 00066 for (unsigned int j=0; j != nv; ++j) 00067 { 00068 elem_subjacobians[i].push_back 00069 (new DenseSubMatrix<Number>(elem_jacobian)); 00070 } 00071 } 00072 } 00073 00074 00075 00076 DiffContext::~DiffContext () 00077 { 00078 for (std::size_t i=0; i != elem_subsolutions.size(); ++i) 00079 { 00080 delete elem_subsolutions[i]; 00081 delete elem_subresiduals[i]; 00082 for (std::size_t q=0; q != elem_qoi_subderivatives.size(); ++q) 00083 delete elem_qoi_subderivatives[q][i]; 00084 if (!elem_fixed_subsolutions.empty()) 00085 delete elem_fixed_subsolutions[i]; 00086 00087 for (std::size_t j=0; j != elem_subjacobians[i].size(); ++j) 00088 delete elem_subjacobians[i][j]; 00089 } 00090 00091 // We also need to delete all the DenseSubVectors from the localized_vectors map 00092 // localized_vectors iterators 00093 std::map<const NumericVector<Number>*, std::pair<DenseVector<Number>, std::vector<DenseSubVector<Number>*> > >::iterator localized_vectors_it = localized_vectors.begin(); 00094 std::map<const NumericVector<Number>*, std::pair<DenseVector<Number>, std::vector<DenseSubVector<Number>*> > >::iterator localized_vectors_end = localized_vectors.end(); 00095 00096 // Loop over every localized_vector 00097 for(; localized_vectors_it != localized_vectors_end; ++localized_vectors_it) 00098 { 00099 // Grab the DenseSubVector to be deleted 00100 std::vector<DenseSubVector<Number>* >& localized_vector_dsv = localized_vectors_it->second.second; 00101 00102 // Loop over that vector and delete each entry 00103 for(std::size_t i=0; i != localized_vector_dsv.size(); ++i) 00104 delete localized_vector_dsv[i]; 00105 } 00106 } 00107 00108 00109 void DiffContext::set_deltat_pointer(Real* dt) 00110 { 00111 // We may actually want to be able to set this pointer to NULL, so 00112 // don't report an error for that. 00113 _deltat = dt; 00114 } 00115 00116 00117 Real DiffContext::get_deltat_value() 00118 { 00119 libmesh_assert(_deltat); 00120 00121 return *_deltat; 00122 } 00123 00124 00125 void DiffContext::add_localized_vector (NumericVector<Number> & _localized_vector, const System & _sys) 00126 { 00127 // Make an empty pair keyed with a reference to this _localized_vector 00128 localized_vectors[&_localized_vector] = std::make_pair(DenseVector<Number>(), std::vector<DenseSubVector<Number>*>()); 00129 00130 unsigned int nv = _sys.n_vars(); 00131 00132 localized_vectors[&_localized_vector].second.reserve(nv); 00133 00134 // Fill the DenseSubVector with nv copies of DenseVector 00135 for(unsigned int i=0; i != nv; ++i) 00136 localized_vectors[&_localized_vector].second.push_back(new DenseSubVector<Number>(localized_vectors[&_localized_vector].first)); 00137 } 00138 00139 00140 DenseVector<Number>& DiffContext::get_localized_vector (const NumericVector<Number> & _localized_vector) 00141 { 00142 return localized_vectors[&_localized_vector].first; 00143 } 00144 00145 00146 const DenseVector<Number>& DiffContext::get_localized_vector (const NumericVector<Number> & _localized_vector) const 00147 { 00148 std::map<const NumericVector<Number>*, std::pair<DenseVector<Number>, std::vector<DenseSubVector<Number>*> > >::const_iterator 00149 localized_vectors_it = localized_vectors.find(&_localized_vector); 00150 libmesh_assert(localized_vectors_it != localized_vectors.end()); 00151 return localized_vectors_it->second.first; 00152 } 00153 00154 00155 DenseSubVector<Number>& DiffContext::get_localized_subvector (const NumericVector<Number> & _localized_vector, unsigned int _var) 00156 { 00157 return *localized_vectors[&_localized_vector].second[_var]; 00158 } 00159 00160 00161 const DenseSubVector<Number>& DiffContext::get_localized_subvector (const NumericVector<Number> & _localized_vector, unsigned int _var) const 00162 { 00163 std::map<const NumericVector<Number>*, std::pair<DenseVector<Number>, std::vector<DenseSubVector<Number>*> > >::const_iterator 00164 localized_vectors_it = localized_vectors.find(&_localized_vector); 00165 libmesh_assert(localized_vectors_it != localized_vectors.end()); 00166 return *localized_vectors_it->second.second[_var]; 00167 } 00168 00169 } // namespace libMesh 00170 00171 00172
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:46 UTC
Hosted By: