Line data Source code
1 : /*
2 : * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Andreas Vogel
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 : #include "function_group.h"
34 : #include "common/common.h"
35 : #include "common/util/string_util.h"
36 : #include <algorithm>
37 : #include <cstring>
38 :
39 : using namespace std;
40 :
41 : namespace ug{
42 :
43 0 : FunctionGroup::FunctionGroup() : m_spFunctionPattern(NULL) {clear();}
44 :
45 0 : FunctionGroup::FunctionGroup(ConstSmartPtr<FunctionPattern> spFuncPattern)
46 : : m_spFunctionPattern(spFuncPattern)
47 : {
48 : clear();
49 0 : }
50 :
51 0 : FunctionGroup::FunctionGroup(ConstSmartPtr<FunctionPattern> spFuncPattern, const char* name)
52 : : m_spFunctionPattern(spFuncPattern)
53 : {
54 : clear();
55 0 : add(name);
56 0 : }
57 :
58 0 : FunctionGroup::FunctionGroup(ConstSmartPtr<FunctionPattern> spFuncPattern, const std::string& name)
59 : : m_spFunctionPattern(spFuncPattern)
60 : {
61 : clear();
62 0 : add(name);
63 0 : }
64 :
65 0 : FunctionGroup::FunctionGroup(ConstSmartPtr<FunctionPattern> spFuncPattern, const std::vector<std::string>& vName)
66 : : m_spFunctionPattern(spFuncPattern)
67 : {
68 : clear();
69 0 : add(vName);
70 0 : }
71 :
72 0 : void FunctionGroup::set_function_pattern(ConstSmartPtr<FunctionPattern> spFuncPattern)
73 : {
74 0 : m_spFunctionPattern = spFuncPattern;
75 : clear();
76 0 : }
77 :
78 :
79 0 : void FunctionGroup::add(size_t fct)
80 : {
81 0 : if(!is_init())
82 0 : UG_THROW("Cannot use FunctionGroup without FunctionPattern.");
83 :
84 0 : if(fct >= m_spFunctionPattern->num_fct())
85 0 : UG_THROW("Unique function ID " <<fct << " not contained in "
86 : "underlying function pattern (with size=" <<
87 : m_spFunctionPattern->num_fct() << ".");
88 :
89 : std::vector<size_t>::iterator iter;
90 0 : iter = find(m_vFunction.begin(), m_vFunction.end(), fct);
91 0 : if(iter != m_vFunction.end()) return;
92 :
93 0 : m_vFunction.push_back(fct);
94 : }
95 :
96 0 : void FunctionGroup::add(const string& name)
97 : {
98 0 : string tName = TrimString(name);
99 :
100 0 : if(!is_init())
101 0 : UG_THROW("Cannot use FunctionGroup without FunctionPattern.");
102 :
103 : size_t found = 0;
104 :
105 : // Search for name in FunctionPattern
106 0 : for(size_t fct = 0; fct < m_spFunctionPattern->num_fct(); ++fct)
107 : {
108 0 : if(strcmp (tName.c_str(), m_spFunctionPattern->name(fct)) == 0)
109 : {
110 0 : found++;
111 0 : add(fct);
112 : }
113 : }
114 :
115 : // if not found, return false
116 0 : if(found == 0)
117 0 : UG_THROW("FunctionGroup: no function '"<<tName<<"' in Function Pattern.");
118 0 : }
119 :
120 0 : void FunctionGroup::add(const char* name)
121 : {
122 0 : add(TokenizeString(name));
123 0 : }
124 :
125 0 : void FunctionGroup::add(const vector<string>& vName)
126 : {
127 0 : for (size_t i = 0; i < vName.size(); ++i) {
128 0 : add(vName[i]);
129 : }
130 0 : }
131 :
132 0 : void FunctionGroup::add(const FunctionGroup& fctGroup)
133 : {
134 0 : if(!is_init())
135 0 : UG_THROW("Cannot use FunctionGroup without FunctionPattern.");
136 :
137 0 : if(m_spFunctionPattern != fctGroup.function_pattern())
138 0 : UG_THROW("Underlying function pattern does not match. Cannot"
139 : " add function group.");
140 :
141 0 : for(size_t i = 0; i < fctGroup.size(); ++i)
142 0 : add(fctGroup[i]);
143 0 : }
144 :
145 0 : void FunctionGroup::add_all()
146 : {
147 0 : if(!is_init())
148 0 : UG_THROW("Cannot use FunctionGroup without FunctionPattern.");
149 :
150 0 : for(size_t i = 0; i < m_spFunctionPattern->num_fct(); ++i)
151 0 : add(i);
152 0 : }
153 :
154 0 : void FunctionGroup::sort()
155 : {
156 0 : std::sort(m_vFunction.begin(), m_vFunction.end());
157 0 : }
158 :
159 0 : void FunctionGroup::remove(size_t fct)
160 : {
161 : std::vector<size_t>::iterator iter;
162 0 : iter = find(m_vFunction.begin(), m_vFunction.end(), fct);
163 0 : if(iter == m_vFunction.end())
164 0 : UG_THROW("Function "<<fct<<" not contained in FunctionGroup.");
165 :
166 0 : m_vFunction.erase(iter);
167 0 : }
168 :
169 0 : void FunctionGroup::remove(const string& name)
170 : {
171 0 : string tName = TrimString(name);
172 :
173 0 : if(!is_init())
174 0 : UG_THROW("Cannot use FunctionGroup without FunctionPattern.");
175 :
176 : size_t found = 0;
177 :
178 : // Search for name in FunctionPattern
179 0 : for(size_t fct = 0; fct < m_spFunctionPattern->num_fct(); ++fct)
180 : {
181 0 : if(strcmp (tName.c_str(), m_spFunctionPattern->name(fct)) == 0)
182 : {
183 0 : found++;
184 0 : remove(fct);
185 : }
186 : }
187 :
188 : // if not found, return false
189 0 : if(found == 0)
190 0 : UG_THROW("FunctionGroup: no function '"<<tName<<"' in Function Pattern.");
191 0 : }
192 :
193 0 : void FunctionGroup::remove(const char* name)
194 : {
195 0 : remove(TokenizeString(name));
196 0 : }
197 :
198 0 : void FunctionGroup::remove(const vector<string>& vName)
199 : {
200 0 : for (size_t i = 0; i < vName.size(); ++i) {
201 0 : remove(vName[i]);
202 : }
203 0 : }
204 :
205 0 : const char* FunctionGroup::name(size_t i) const
206 : {
207 0 : if(!is_init())
208 0 : UG_THROW("Cannot use FunctionGroup without FunctionPattern.");
209 :
210 : // Check, that subset exist
211 0 : if(i >= size())
212 0 : UG_THROW("Function index "<<i<<" not valid.");
213 :
214 0 : return m_spFunctionPattern->name(m_vFunction[i]);
215 : }
216 :
217 0 : std::string FunctionGroup::names() const
218 : {
219 0 : if(!is_init())
220 0 : UG_THROW("Cannot use FunctionGroup without FunctionPattern.");
221 :
222 : std::string s;
223 0 : for(size_t i = 0; i < size(); ++i){
224 0 : if(i > 0) s.append(", ");
225 0 : s.append(name(i));
226 : }
227 0 : return s;
228 : }
229 :
230 :
231 0 : LFEID FunctionGroup::local_finite_element_id(size_t i) const
232 : {
233 0 : if(!is_init())
234 0 : UG_THROW("Cannot use FunctionGroup without FunctionPattern.");
235 :
236 : // Check, that subset exist
237 0 : if(i >= size())
238 0 : UG_THROW("Function index "<<i<<" not valid.");
239 :
240 0 : return m_spFunctionPattern->local_finite_element_id(m_vFunction[i]);
241 : }
242 :
243 0 : LFEID FunctionGroup::lfeid(size_t i) const{
244 0 : return local_finite_element_id(i);
245 : }
246 :
247 0 : int FunctionGroup::dim(size_t i) const
248 : {
249 0 : if(!is_init())
250 0 : UG_THROW("Cannot use FunctionGroup without FunctionPattern.");
251 :
252 : // Check, that subset exist
253 0 : if(i >= size())
254 0 : UG_THROW("Function index "<<i<<" not valid.");
255 :
256 0 : return m_spFunctionPattern->dim(m_vFunction[i]);
257 : }
258 :
259 0 : int FunctionGroup::dim() const
260 : {
261 0 : if(!is_init())
262 0 : UG_THROW("Cannot use FunctionGroup without FunctionPattern.");
263 :
264 : // without functions no dimension
265 0 : if(size() == 0) return -1;
266 :
267 0 : int d = dim(0);
268 :
269 0 : for(size_t i = 0; i < size(); ++i)
270 : {
271 0 : int test_dim = dim(i);
272 0 : if(d != test_dim)
273 : return -1;
274 : }
275 :
276 : return d;
277 : }
278 :
279 0 : bool FunctionGroup::contains(size_t fct) const
280 : {
281 : std::vector<size_t>::const_iterator iter;
282 0 : iter = find(m_vFunction.begin(), m_vFunction.end(), fct);
283 0 : if(iter == m_vFunction.end()) return false;
284 : return true;
285 : }
286 :
287 0 : bool FunctionGroup::contains(const FunctionGroup& fctGroup) const
288 : {
289 : // loop all functions
290 0 : for(size_t i = 0; i < fctGroup.size(); ++i)
291 : {
292 : // if one function is not contained, return false
293 0 : if(!contains(fctGroup[i]))
294 : return false;
295 : }
296 :
297 : // all functions contained
298 : return true;
299 : }
300 :
301 :
302 0 : size_t FunctionGroup::local_index(size_t fct) const
303 : {
304 0 : for(size_t i = 0; i < m_vFunction.size(); ++i)
305 : {
306 0 : if(fct == m_vFunction[i]) return i;
307 : }
308 :
309 0 : UG_THROW("Function index "<<fct<<" not valid.");
310 : }
311 :
312 : } // end namespace ug
|