ViennaSHE 1.3.0
Free open-source semiconductor device simulator using spherical harmonics expansions techniques.
misc.hpp
Go to the documentation of this file.
1#ifndef VIENNASHE_UTIL_MISC_HPP
2#define VIENNASHE_UTIL_MISC_HPP
3
4/* ============================================================================
5 Copyright (c) 2011-2022, Institute for Microelectronics,
6 Institute for Analysis and Scientific Computing,
7 TU Wien.
8
9 -----------------
10 ViennaSHE - The Vienna Spherical Harmonics Expansion Boltzmann Solver
11 -----------------
12
13 http://viennashe.sourceforge.net/
14
15 License: MIT (X11), see file LICENSE in the base directory
16=============================================================================== */
17
18// std
19#include <iostream>
20#include <iomanip>
21#include <sstream>
22#include <string>
23#include <stdexcept>
24#include <cmath>
25
26// viennagrid
27#include "viennagrid/forwards.hpp"
28#include "viennagrid/algorithm/voronoi.hpp"
29#include "viennagrid/algorithm/interface.hpp"
30#include "viennagrid/algorithm/boundary.hpp"
31
32// viennashe
33#include "viennashe/forwards.h"
36
38
43namespace viennashe
44{
46 namespace util
47 {
48
49 //
50 // Helper
51 //
52
57 template <typename ValueType>
59 {
60 public:
61 typedef ValueType value_type;
62
63 template <typename T>
64 void operator()(T const &, value_type val) const { value_ = val; }
65
66 value_type operator()() const { return value_; }
67
68 private:
69 mutable value_type value_;
70 };
71
72 //
73 // Averaging
74 //
75
81 {
82 template <typename ContainerType>
83 double operator()(ContainerType const & cont) const
84 {
85 double ret = 0;
86 for (typename ContainerType::const_iterator cit = cont.begin();
87 cit != cont.end();
88 ++cit)
89 ret += *cit / cont.size();
90
91 return ret;
92 }
93 };
94
101 {
102 template <typename ContainerType>
103 double operator()(ContainerType const & cont) const
104 {
105 double ret = 1.0;
106 for (typename ContainerType::const_iterator cit = cont.begin();
107 cit != cont.end();
108 ++cit)
109 {
110 ret *= std::pow(*cit, 1.0 / cont.size());
111 }
112
113 return ret;
114 }
115 };
116
122 {
123 template <typename ContainerType>
124 bool operator()(ContainerType const & cont) const
125 {
126 bool ret = false;
127 for (typename ContainerType::const_iterator cit = cont.begin();
128 cit != cont.end();
129 ++cit)
130 {
131 ret |= *cit;
132 }
133
134 return ret;
135 }
136 };
137
138
139 //
140 // Filtered setter
141 //
147 template <typename FilterType, typename FunctorType>
149 {
150 public:
151 filtered_functor(FilterType const & fil, FunctorType & fun) : filter_(fil), fun_(fun) {}
152
153 template <typename T>
154 void operator()(T const & t)
155 {
156 if (filter_(t))
157 fun_(t);
158 }
159
160 template <typename T>
161 void operator()(T & t)
162 {
163 if (filter_(t))
164 fun_(t);
165 }
166
167 private:
168 FilterType const & filter_;
169 FunctorType & fun_;
170 };
171
173 template <typename FilterType, typename FunctorType>
174 filtered_functor<FilterType, FunctorType>
175 make_filtered_functor(FilterType const & filter, FunctorType & fun)
176 {
178 }
179
180
182 namespace detail
183 {
186 {
187 template <typename U>
188 double operator()(U const &) const { return 1.0; }
189 };
190
191 } //namespace detail
192
193
195 template <typename MeshT, typename FacetT, typename CellT>
196 CellT const * get_other_cell_of_facet(MeshT const & mesh, FacetT const & facet, CellT const & cell)
197 {
198 typedef typename viennagrid::result_of::const_coboundary_range<MeshT, FacetT, CellT>::type CellOnFacetContainer;
199 typedef typename viennagrid::result_of::iterator<CellOnFacetContainer>::type CellOnFacetIterator;
200
201 CellOnFacetContainer cells_on_facet(mesh, viennagrid::handle(mesh, facet));
202 CellOnFacetIterator cofit = cells_on_facet.begin();
203
204 if ( &(*cofit) == &(cell)) //one of the two vertices of the edge is different from 'cell'
205 ++cofit;
206
207 if (cofit == cells_on_facet.end()) // Only one cell attached to facet
208 return NULL;
209
210 return &(*cofit);
211 }
212
213
215 template <typename DeviceT, typename CellT>
216 CellT const * get_connected_semiconductor_cell(DeviceT const & device, CellT const & cell)
217 {
218 typedef typename viennagrid::result_of::const_facet_range<CellT>::type FacetOnCellContainer;
219 typedef typename viennagrid::result_of::iterator<FacetOnCellContainer>::type FacetOnCellIterator;
220
221 FacetOnCellContainer facets_on_cell(cell);
222 for (FacetOnCellIterator focit = facets_on_cell.begin();
223 focit != facets_on_cell.end();
224 ++focit)
225 {
226 // check whether cell is adjacent to a semiconductor cell:
227 CellT const * other_cell = viennashe::util::get_other_cell_of_facet(device.mesh(), *focit, cell);
228 if (!other_cell)
229 continue;
230
232 return other_cell;
233 }
234
235 return NULL;
236 }
237
238
240
241 // empty default template
242 template <bool b>
244
245 // template specialized on true
246 template <>
248 {
249 static void check() {}
250 };
251
252 template <typename T, typename U>
253 struct is_equal
254 {
255 static const bool value = false;
256 };
257
258 template <typename T>
259 struct is_equal<T, T>
260 {
261 static const bool value = true;
262 };
263
264
271 template <typename DeviceType, typename ElementTagT, typename ValueType = double>
273 {
275
276 typedef typename DeviceType::mesh_type MeshType;
277
278 public:
279 typedef typename viennagrid::result_of::element<MeshType, ElementTagT>::type element_type;
280 typedef ValueType value_type;
281
283 template <typename VectorType, typename IndexKeyType, typename BoundaryValueAccessor>
285 VectorType const & quantity_values,
286 IndexKeyType const & index_array,
287 BoundaryValueAccessor const & bnd_accessor)
288 : values_(viennagrid::elements<ElementTagT>(device.mesh()).size()), vec_(quantity_values.size())
289 {
291 typename VectorType::value_type>::value
292 >::check();
293
294 typedef typename viennagrid::result_of::const_element_range<MeshType, ElementTagT>::type ElementContainer;
295 typedef typename viennagrid::result_of::iterator<ElementContainer>::type ElementIterator;
296 typedef typename element_type::id_type id_type;
297
298 // Iterate over all elements and copy values over
299 ElementContainer elements(device.mesh());
300 for (ElementIterator it = elements.begin(); it != elements.end(); ++it)
301 {
302 id_type element_id = it->id();
303 long index = index_array.at(element_id);
304
305 if (index >= 0)
306 values_.at(element_id) = quantity_values[index];
307 else
308 values_.at(element_id) = bnd_accessor(*it);
309 }
310
311 for (std::size_t i=0; i<quantity_values.size(); ++i)
312 vec_[i] = quantity_values[i];
313 }
314
317 {
318 return values_.at(t.id());
319 }
320
322 std::vector<double> const & vector() const { return vec_; }
323
324 private:
325 std::vector<value_type> values_;
326 std::vector<double> vec_;
327 };
328
335 template <typename DeviceType, typename ElementTagT, typename ValueType = double>
337 {
339
340 typedef typename DeviceType::mesh_type MeshType;
341
342 public:
343 typedef typename viennagrid::result_of::element<MeshType, ElementTagT>::type element_type;
344 typedef ValueType value_type;
345
347 spatial_quantity(DeviceType const & device, ValueType default_value = ValueType()) : values_(viennagrid::cells(device.mesh()).size(), default_value) {}
348
351 {
352 return values_.at(t.id());
353 }
354
355 void set(element_type const & t, ValueType val)
356 {
357 values_.at(t.id()) = val;
358 }
359
360 private:
361 std::vector<value_type> values_;
362 };
363
364
371 template <typename T>
372 std::string format_number_to_width(T number, int width)
373 {
374 std::stringstream s;
375 s << std::setfill(' ') << std::setw(width) << number;
376 return s.str();
377 }
378
385 template <typename VectorType>
386 typename VectorType::value_type norm_inf(VectorType const & v, std::size_t index_start, std::size_t index_stop)
387 {
388 typename VectorType::value_type ret = 0;
389 for (std::size_t i=index_start; i<index_stop; ++i)
390 ret = std::max(std::abs(v[i]), ret);
391
392 return ret;
393 }
394
395
396 } //namespace util
397} //namespace viennashe
398#endif
long get_material(cell_type const &elem) const
Returns the material id of the provided cell.
Definition: device.hpp:502
MeshT const & mesh() const
Returns the underlying mesh.
Definition: device.hpp:145
Defines the physical properties of a device, e.g. doping. This is the implementation for 2d and highe...
Definition: device.hpp:818
A compound functor which applies the provided functor only to the argument if the supplied filter eva...
Definition: misc.hpp:149
filtered_functor(FilterType const &fil, FunctorType &fun)
Definition: misc.hpp:151
void operator()(T const &t)
Definition: misc.hpp:154
A functor-style wrapper for a spatial quantity (typically potential, electron density or hole density...
Definition: misc.hpp:273
viennagrid::result_of::element< MeshType, ElementTagT >::type element_type
Definition: misc.hpp:279
value_type operator()(element_type const &t) const
The functor interface.
Definition: misc.hpp:316
spatial_quantity_wrapper(DeviceType const &device, VectorType const &quantity_values, IndexKeyType const &index_array, BoundaryValueAccessor const &bnd_accessor)
Copy over all values from the accessors so that this object can be easily passed around.
Definition: misc.hpp:284
std::vector< double > const & vector() const
Returns the internal vector with the quantities. Allows to externally adjust the quantity.
Definition: misc.hpp:322
A functor-style wrapper for a spatial quantity which is externally prescribed by the user.
Definition: misc.hpp:337
value_type operator()(element_type const &t) const
The functor interface.
Definition: misc.hpp:350
spatial_quantity(DeviceType const &device, ValueType default_value=ValueType())
Copy over all values from the accessors so that this object can be easily passed around.
Definition: misc.hpp:347
void set(element_type const &t, ValueType val)
Definition: misc.hpp:355
viennagrid::result_of::element< MeshType, ElementTagT >::type element_type
Definition: misc.hpp:343
A functor which holds a single value. Used in most of the postprocessing routines.
Definition: misc.hpp:59
value_type operator()() const
Definition: misc.hpp:66
void operator()(T const &, value_type val) const
Definition: misc.hpp:64
Provides the exceptions used in the main viennashe namespace.
Contains forward declarations and definition of small classes that must be defined at an early stage.
A very simple material database. Needs to be replaced by something more versatile soon.
bool is_semiconductor(long material_id)
Convenience function for checking whether the supplied material ID refers to a semiconductor.
Definition: all.hpp:156
VectorType::value_type norm_inf(VectorType const &v, std::size_t index_start, std::size_t index_stop)
Computes the infimum norm of a subvector.
Definition: misc.hpp:386
std::string format_number_to_width(T number, int width)
Returns the formatted number as string with the given length, where spaces are used as padding charac...
Definition: misc.hpp:372
filtered_functor< FilterType, FunctorType > make_filtered_functor(FilterType const &filter, FunctorType &fun)
Convenience routine for creating a filtered functor out of the provided filter- and action-functor....
Definition: misc.hpp:175
CellT const * get_connected_semiconductor_cell(DeviceT const &device, CellT const &cell)
Helper function returning a const-pointer to the 'second cell' of a facet, or NULL if there is no sec...
Definition: misc.hpp:216
CellT const * get_other_cell_of_facet(MeshT const &mesh, FacetT const &facet, CellT const &cell)
Helper function returning a const-pointer to the 'second cell' of a facet, or NULL if there is no sec...
Definition: misc.hpp:196
The main ViennaSHE namespace. All functionality resides inside this namespace.
Definition: accessors.hpp:40
A functor which computes the arithmetic average of all entries in a container.
Definition: misc.hpp:81
double operator()(ContainerType const &cont) const
Definition: misc.hpp:83
A functor assigning unity weight to the element provided.
Definition: misc.hpp:186
double operator()(U const &) const
Definition: misc.hpp:188
A functor which computes the geometric average of all entries in a container.
Definition: misc.hpp:101
double operator()(ContainerType const &cont) const
Definition: misc.hpp:103
static const bool value
Definition: misc.hpp:255
A functor which returns the result of a logic-or chain consisting of all elements of the supplied con...
Definition: misc.hpp:122
bool operator()(ContainerType const &cont) const
Definition: misc.hpp:124
Defines all exceptions used/thrown in the viennashe/util/ namespace.