ug4
attachment_util_impl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__LIB_GRID__ATTACHMENT_UTIL_IMPL__
34 #define __H__LIB_GRID__ATTACHMENT_UTIL_IMPL__
35 
36 #include "attachment_util.h"
37 
38 namespace ug
39 {
40 
42 // SetAttachmentValues
43 template <class TAttachmentAccessor, class TIter, class TVal>
44 void SetAttachmentValues(TAttachmentAccessor& aaVal,
45  TIter elemsBegin, TIter elemsEnd,
46  const TVal& val)
47 {
48  while(elemsBegin != elemsEnd)
49  {
50  aaVal[*elemsBegin] = val;
51  ++elemsBegin;
52  }
53 }
54 
56 // ConvertMathVectorAttachmentValues
57 template<class TElem, class TSrcAttachment, class TDestAttachment>
59  TSrcAttachment& srcAttachment,
60  TDestAttachment& destAttachment)
61 {
62  typedef TSrcAttachment ASrc;
63  typedef TDestAttachment ADest;
64 
65 // make sure that the attachments are attached correctly
66  if(!grid.has_attachment<TElem>(srcAttachment))
67  return false;
68 
69  if(!grid.has_attachment<TElem>(destAttachment))
70  grid.attach_to<TElem>(destAttachment);
71 
72 // get data containers
73  typename ASrc::ContainerType& cSrc
74  = *grid.get_attachment_data_container<TElem>(srcAttachment);
75  typename ADest::ContainerType& cDest
76  = *grid.get_attachment_data_container<TElem>(destAttachment);
77 
78  int srcDim = ASrc::ValueType::Size;
79  int destDim = ADest::ValueType::Size;
80 
81 // iterate through the elements and copy them
82 // if destDim is smaller than srcDim we only have to copy
83  if(destDim <= srcDim)
84  {
85  for(uint i = 0; i < cSrc.size(); ++i)
86  {
87  typename ASrc::ValueType& vSrc = cSrc[i];
88  typename ADest::ValueType& vDest = cDest[i];
89 
90  for(int j = 0; j < destDim; ++j)
91  vDest[j] = vSrc[j];
92  }
93  }
94  else
95  {
96  // we have to fill the higher dimensions with 0
97  for(uint i = 0; i < cSrc.size(); ++i)
98  {
99  typename ASrc::ValueType& vSrc = cSrc[i];
100  typename ADest::ValueType& vDest = cDest[i];
101 
102  for(int j = 0; j < srcDim; ++j)
103  vDest[j] = vSrc[j];
104 
105  for(int j = srcDim; j < destDim; ++j)
106  vDest[j] = 0;
107  }
108  }
109 
110  return true;
111 }
112 
114 template <class TElem, class TAttachment>
115 bool CopyAttachments(Grid& srcGrid, TAttachment& aSrc,
116  Grid& destGrid, TAttachment& aDest)
117 {
118 // make sure the attachments are properly attached.
119  if(!srcGrid.has_attachment<TElem>(aSrc))
120  return false;
121 
122  if(!destGrid.has_attachment<TElem>(aDest))
123  destGrid.attach_to<TElem>(aDest);
124 
125 // access the attachments
127  Grid::AttachmentAccessor<TElem, TAttachment> aaDest(destGrid, aDest);
128 
129 // iterate over the elements
130  typename geometry_traits<TElem>::iterator iterSrc = srcGrid.begin<TElem>();
131  typename geometry_traits<TElem>::iterator iterDest = destGrid.begin<TElem>();
132 
133  while((iterSrc != srcGrid.end<TElem>())
134  &&(iterDest != destGrid.end<TElem>()))
135  {
136  aaDest[*iterDest] = aaSrc[*iterSrc];
137  ++iterDest;
138  ++iterSrc;
139  }
140 
141 // done
142  return true;
143 }
144 
146 template <class TElemIter, class TAttachment>
147 bool CopyAttachments(Grid& grid, TElemIter elemsBegin, TElemIter elemsEnd,
148  TAttachment& aSrc, TAttachment& aDest)
149 {
150  typedef typename PtrToValueType<
151  typename TElemIter::value_type>::base_type TElem;
152 
153 // make sure the attachments are properly attached.
154  if(!grid.has_attachment<TElem>(aSrc))
155  return false;
156 
157  if(!grid.has_attachment<TElem>(aDest))
158  grid.attach_to<TElem>(aDest);
159 
160 // access the attachments
163 
164 // iterate over the elements
165  TElemIter iter = elemsBegin;
166  while(iter != elemsEnd)
167  {
168  aaDest[*iter] = aaSrc[*iter];
169  ++iter;
170  }
171 
172 // done
173  return true;
174 }
175 
177 template <class TIterator, class TAAInt>
178 void AssignIndices(TIterator begin, TIterator end,
179  TAAInt& aaInt, int baseIndex)
180 {
181  for(TIterator iter = begin; iter != end; ++iter)
182  aaInt[*iter] = baseIndex++;
183 }
184 
186 template <class TIterator, class TAttAcc>
187 TIterator FindElementByValue(TIterator begin, TIterator end,
188  const typename TAttAcc::ValueType& val,
189  TAttAcc& aa)
190 {
191  TIterator iter = begin;
192  while(iter != end){
193  if(aa[*iter] == val)
194  break;
195  ++iter;
196  }
197  return iter;
198 }
199 
200 }// end of namespace
201 
202 #endif
the generic attachment-accessor for access to grids attachment pipes.
Definition: grid.h:182
Manages the elements of a grid and their interconnection.
Definition: grid.h:132
void attach_to(IAttachment &attachment, bool passOnValues)
attach with custom pass-on-behaviour and unspecified default value.
Definition: grid_impl.hpp:296
bool has_attachment(IAttachment &attachment)
Definition: grid.h:796
geometry_traits< TGeomObj >::iterator begin()
Definition: grid_impl.hpp:164
geometry_traits< TGeomObj >::iterator end()
Definition: grid_impl.hpp:175
TAttachment::ContainerType * get_attachment_data_container(TAttachment &attachment)
Definition: grid_impl.hpp:399
Definition: grid_base_object_traits.h:68
bool ConvertMathVectorAttachmentValues(Grid &grid, TSrcAttachment &srcAttachment, TDestAttachment &destAttachment)
Fills the dest-attachment with values from the source-attachment.
Definition: attachment_util_impl.hpp:58
TIterator FindElementByValue(TIterator begin, TIterator end, const typename TAttAcc::ValueType &val, TAttAcc &aa)
returns the iterator whose element has the specified attachment value.
Definition: attachment_util_impl.hpp:187
bool CopyAttachments(Grid &srcGrid, TAttachment &aSrc, Grid &destGrid, TAttachment &aDest)
copies attachments from one grid to the other
Definition: attachment_util_impl.hpp:115
void AssignIndices(TIterator begin, TIterator end, TAAInt &aaInt, int baseIndex=0)
assigns indices to the elements between begin and end.
Definition: attachment_util_impl.hpp:178
void SetAttachmentValues(TAttachmentAccessor &aaVal, TIter elemsBegin, TIter elemsEnd, const TVal &val)
sets attachment-values for elements between elemsBegin and elemsEnd.
Definition: attachment_util_impl.hpp:44
IMaterialLaw< TDomain > base_type
unsigned int uint
Definition: types.h:114
the ug namespace
T value_type
Definition: sparsematrix_interface.h:2
Definition: grid_base_objects.h:1011