ViennaSHE 1.3.0
Free open-source semiconductor device simulator using spherical harmonics expansions techniques.
quantity_wrappers.hpp
Go to the documentation of this file.
1#ifndef LIBVIENNASHE_QUANTITY_WRAPPERS_HPP
2#define LIBVIENNASHE_QUANTITY_WRAPPERS_HPP
3/* ============================================================================
4 Copyright (c) 2011-2022, Institute for Microelectronics,
5 Institute for Analysis and Scientific Computing,
6 TU Wien.
7
8 -----------------
9 ViennaSHE - The Vienna Spherical Harmonics Expansion Boltzmann Solver
10 -----------------
11
12 http://viennashe.sourceforge.net/
13
14 License: MIT (X11), see file LICENSE in the base directory
15=============================================================================== */
16
17
22// ++++++++++++++++++++++++++++++++++++
23//
24// +++++++++++ C++ ONLY +++++++++++
25//
26// ++++++++++++++++++++++++++++++++++++
27
28#include <iostream>
29#include <cstdlib>
30#include <vector>
31
32#include "viennashe/forwards.h"
34
36
37
38namespace libviennashe
39{
41 namespace quantity
42 {
45 {
46 public:
47 quantity_wrapper(std::string name) : name_(name) {}
48
50 quantity_wrapper() : name_("") {}
51
52 quantity_wrapper(quantity_wrapper const & v) : name_(v.name_) {}
53 virtual ~quantity_wrapper() {}
54
55 void operator=(quantity_wrapper const & o) { this->name_ = o.name_; }
56
63 virtual void fill(double ** values, viennashe_index_type * len) const = 0;
64
70 virtual void fill_single (std::size_t idx, std::vector<double> & values) const = 0;
71
73 virtual quantity_wrapper * copy() const = 0;
74
76 std::string name() const { return this->name_; }
77
78 protected:
80 void set_name(std::string const & n) { this->name_ = n; }
81
82 private:
83 std::string name_;
84 };
85
88 template <typename DeviceT, typename AccessorT, typename ElementTagT >
90 {
91 public:
92
94
101 accessor_based_quantity_wrapper(AccessorT const & acc,
102 DeviceT const & dev,
103 std::string name)
104 : quantity_wrapper(name), dev_(dev), acc_(acc)
105 { }
106
108 : quantity_wrapper(o), dev_(o.dev_), acc_(o.acc_)
109 { }
110
112 {
113 this->set_name(o.name());
114 dev_(o.dev_); acc_(o.acc_);
115 }
116
120 double get(std::size_t id) const
121 {
122 return acc_(viennagrid::elements<ElementTagT>(dev_.mesh())[id]);
123 }
124
128 virtual void fill(double ** values, viennashe_index_type * len) const
129 {
130 const std::size_t num = viennagrid::elements<ElementTagT>(dev_.mesh()).size();
131
132 for (std::size_t i = 0; i < num; ++i)
133 {
134 values[i] = new double(); // The user has to clear things up
135 values[i][0] = this->get(i);
136 len[i] = 1;
137 }
138 }
139
143 virtual void fill_single(std::size_t idx, std::vector<double> & values) const
144 {
145 values.resize(1);
146 values[0] = this->get(idx);
147 }
148
149 virtual quantity_wrapper * copy() const { return new self_type(*this); }
150
151 private:
152 DeviceT const & dev_;
153 AccessorT acc_;
154 };
155
159 template <typename DeviceT, typename AccessorT, typename ElementTagT >
161 {
162 public:
163
165
173 DeviceT const & dev,
174 std::string name)
175 : quantity_wrapper(name), dev_(dev), acc_(acc)
176 { }
177
179 : quantity_wrapper(o), dev_(o.dev_), acc_(o.acc_)
180 { }
181
183 {
184 this->set_name(o.name());
185 dev_(o.dev_); acc_(o.acc_);
186 }
187
193 std::vector<double> get(std::size_t id) const
194 {
195 return acc_(viennagrid::elements<ElementTagT>(dev_.mesh())[id]);
196 }
197
199 virtual void fill_single(std::size_t idx, std::vector<double> & values) const { values = this->get(idx); }
200
202 virtual void fill(double ** values, viennashe_index_type * len) const
203 {
204 const std::size_t num = viennagrid::elements<ElementTagT>(dev_.mesh()).size();
205
206 for (std::size_t i = 0; i < num; ++i)
207 {
208 const std::vector<double> valuevector = this->get(i);
209 const std::size_t current_length = valuevector.size();
210
211 values[i] = new double[current_length]; // The user has to clear things up
212
213 for (std::size_t j = 0; j < current_length; ++j) values[i][j] = valuevector.at(j);
214
215 len[i] = static_cast<viennashe_index_type>(current_length);
216 }
217 }
218
220 virtual quantity_wrapper * copy() const { return new self_type(*this); }
221
222 private:
223 DeviceT const & dev_;
224 AccessorT acc_;
225 };
226
229 {
230 public:
231 typedef std::map<std::string, quantity_wrapper * > quan_map;
232
234 {
235 for (quan_map::iterator it = quans_.begin(); it != quans_.end(); ++it)
236 {
237 if (it->second != NULL)
238 delete it->second;
239 it->second = NULL;
240 }
241 }
242
244 std::size_t size() const { return this->quans_.size(); }
245
247 std::vector<std::string> get_names() const
248 {
249 std::vector<std::string> v;
250 for (quan_map::const_iterator it = quans_.begin(); it != quans_.end(); ++it)
251 {
252 v.push_back(it->first);
253 }
254 return v;
255 }
256
260 quantity_wrapper const & get(std::string const & name) const { return *(quans_.at(name)); }
261
267 bool has_quan(std::string const & name) const { return (this->quans_.find(name) != this->quans_.end()); }
268
273 {
274 std::string name = quan.name();
275 if (this->has_quan(name))
276 {
277 viennashe::log::error() << "ERROR: ViennaSHE C/C++ Wrappers failed! Where? Quantitiy Management at register_quan for '"
278 << name << "', which is already registered!" << std::endl;
279 throw std::runtime_error("Quantitiy already exists");
280 }
281 else
282 {
283 this->quans_.insert(std::make_pair(name, quan.copy()));
284 }
285 }
286
287 private:
288 quan_map quans_;
289 }; // quantity_register
290
291
292 } // namespace quantity
293
296 {
297
299
300 std::size_t count() const
301 {
302 return cell_based.size() ;
303 }
304
306
308 };
309
310} // namespace libviennashe
311
312#endif /* LIBVIENNASHE_QUANTITY_WRAPPERS_HPP */
313
Implements quantity_wrapper. Wraps vector valued quantities, which are accessible via a device based ...
accessor_based_array_quantity_wrapper< DeviceT, AccessorT, ElementTagT > self_type
accessor_based_array_quantity_wrapper(AccessorT const &acc, DeviceT const &dev, std::string name)
CTOR.
virtual void fill_single(std::size_t idx, std::vector< double > &values) const
Implements fill_single.
accessor_based_array_quantity_wrapper(accessor_based_array_quantity_wrapper const &o)
void operator=(accessor_based_array_quantity_wrapper const &o)
virtual quantity_wrapper * copy() const
Implements copy.
std::vector< double > get(std::size_t id) const
Simple forward to the accessor.
virtual void fill(double **values, viennashe_index_type *len) const
Implements fill.
Implements quantity_wrapper. Wraps scalar quantities, which are accessible via a device based accesso...
accessor_based_quantity_wrapper< DeviceT, AccessorT, ElementTagT > self_type
void operator=(accessor_based_quantity_wrapper const &o)
accessor_based_quantity_wrapper(AccessorT const &acc, DeviceT const &dev, std::string name)
CTOR.
virtual quantity_wrapper * copy() const
A simple copy factory for storage.
virtual void fill(double **values, viennashe_index_type *len) const
Implementation of fill.
double get(std::size_t id) const
Simple forward to the accessor.
accessor_based_quantity_wrapper(accessor_based_quantity_wrapper const &o)
virtual void fill_single(std::size_t idx, std::vector< double > &values) const
Implemenation of fill_single. Uses get()
The main quantity register. Not deep copy-able!
quantity_wrapper const & get(std::string const &name) const
Returns a single const reference to a quantitiy wrapper.
std::map< std::string, quantity_wrapper * > quan_map
bool has_quan(std::string const &name) const
Returns true if a quantity is registered.
void register_quan(quantity_wrapper const &quan)
Registers a quantity.
std::size_t size() const
Returns the number of registered quantities.
std::vector< std::string > get_names() const
Returns all names of the registered quantities.
The main quantity wrapper interface
quantity_wrapper()
Default CTOR. Creates an empty wrapper.
void operator=(quantity_wrapper const &o)
void set_name(std::string const &n)
Name setter for implementing classes.
virtual void fill_single(std::size_t idx, std::vector< double > &values) const =0
Interface. Fills values with a single value (scalar or vector) at the given element index.
quantity_wrapper(quantity_wrapper const &v)
virtual quantity_wrapper * copy() const =0
A simple copy factory for storage.
std::string name() const
Returns the unique name of the quantity.
virtual void fill(double **values, viennashe_index_type *len) const =0
Interface. Fills values with all quantity values, where the length of values[i] is to be found in len...
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.
The internal C++ namespace of the library.
logger< true > error()
Used to log errors. The logging level is logERROR.
Definition: log.hpp:301
C++ to C wrapper of the quantity registry.
libviennashe::quantity::quantitiy_register cell_based
viennashe_simulator_impl * int_sim
Register for vertex based quantities.
Internal C++ to C wrapper for the simulator. Has typedefs and destructor.
unsigned long viennashe_index_type
Definition: sys.h:42
Contains all viennashe includes and most of the C++/C-wrappers. Contains macros.