1#ifndef VIENNASHE_IO_DEVICE_READER_VTK_HPP
2#define VIENNASHE_IO_DEVICE_READER_VTK_HPP
24#include "viennagrid/forwards.hpp"
25#include "viennagrid/topology/simplex.hpp"
26#include "viennagrid/io/vtk_reader.hpp"
45 template <
typename MeshT >
52 template <
typename SegT>
55 this->mesh_reader_(mesh, seg, this->filename_);
62 std::string filename_;
76 template <
typename DeviceType >
79 std::string doping_n_key,
80 std::string doping_p_key,
81 std::string material_key)
83 typedef typename DeviceType::mesh_type MeshType;
84 typedef typename DeviceType::segment_type SegmentType;
86 typedef typename viennagrid::result_of::cell<MeshType>::type CellType;
87 typedef typename viennagrid::result_of::vertex<MeshType>::type VertexType;
92 typedef typename viennagrid::result_of::const_cell_range<SegmentType>::type CellContainer;
93 typedef typename viennagrid::result_of::iterator<CellContainer>::type CellIterator;
95 typedef typename viennagrid::result_of::const_vertex_range<CellType>::type VertexOnCellContainer;
96 typedef typename viennagrid::result_of::iterator<VertexOnCellContainer>::type VertexOnCellIterator;
99 typedef typename viennagrid::result_of::field<const std::deque<double>, CellType >::type scalar_cell_data;
100 typedef typename viennagrid::result_of::field<const std::deque<double>, VertexType >::type scalar_vertex_data;
102 if (doping_n_key.empty() || doping_p_key.empty())
104 viennashe::log::error() <<
"* read_device(): One of the doping keys is empty! Unable to proceed." << std::endl;
116 log::info() <<
"* read_device_vtk(): There are " << num_segments <<
" segments." << std::endl;
119 for (std::size_t j = 0; j < num_segments; ++j)
121 log::debug() <<
"* read_device():" << std::endl;
123 if(mesh_reader.scalar_cell_data_names(j).size() > 0)
124 log::debug() <<
" Segment " << j <<
" has the following scalar CELL quantities: " << std::endl;
125 for(std::size_t i = 0; i < mesh_reader.scalar_cell_data_names(j).size(); ++i)
126 log::debug() <<
"\t" << mesh_reader.scalar_cell_data_names(j)[i] << std::endl;
128 if(mesh_reader.scalar_vertex_data_names(j).size() > 0)
129 log::debug() <<
" Segment " << j <<
" has the following scalar VERTEX quantities: " << std::endl;
130 for(std::size_t i = 0; i < mesh_reader.scalar_vertex_data_names(j).size(); ++i)
131 log::debug() <<
"\t" << mesh_reader.scalar_vertex_data_names(j)[i] << std::endl;
138 if (!material_key.empty())
141 log::debug()<<
"* read_device(): Using '" << material_key <<
"' as material ID per cell." << std::endl;
143 for (std::size_t j = 0; j < num_segments; ++j)
145 bool has_material_information =
false;
146 std::vector<std::string> cell_data_names = mesh_reader.scalar_cell_data_names(j);
147 for (std::size_t n=0; n<cell_data_names.size(); ++n)
149 if (cell_data_names[n] == material_key)
150 has_material_information =
true;
153 if (!has_material_information)
155 viennashe::log::error() <<
"* read_device(): WARNING: Unable to find material for segment " << j <<
"! Skipping..." << std::endl;
159 scalar_cell_data
const & mat_data = mesh_reader.cell_scalar_field(material_key, j);
161 for ( CellIterator cit = cells.begin(); cit != cells.end(); ++cit )
163 const long material_id = mat_data.at(*cit);
170 viennashe::log::warning() <<
"* read_device(): Empty material key found! Assuming the material is being set by the user ... " << std::endl;
176 for (std::size_t j = 0; j < num_segments; ++j)
178 bool doping_on_cells =
false;
180 std::vector<std::string> cell_data_names = mesh_reader.scalar_cell_data_names(j);
181 for (std::size_t n=0; n<cell_data_names.size(); ++n)
183 if (cell_data_names[n] == doping_n_key)
184 doping_on_cells =
true;
187 if (doping_on_cells && mesh_reader.cell_scalar_field(doping_n_key, j).is_valid() )
189 log::debug()<<
"* read_device(): Using '" << doping_n_key <<
"' as donor doping per cell on segment " << j <<
"." << std::endl;
190 log::debug()<<
"* read_device(): Using '" << doping_p_key <<
"' as acceptor doping per cell on segment " << j <<
"." << std::endl;
192 scalar_cell_data
const & data_n = mesh_reader.cell_scalar_field(doping_n_key, j);
193 scalar_cell_data
const & data_p = mesh_reader.cell_scalar_field(doping_p_key, j);
195 if (!data_n.is_valid() || !data_p.is_valid())
197 log::error() <<
"* read_device(): Invalid doping key on cells !" << std::endl;
202 for ( CellIterator cit = cells.begin(); cit != cells.end(); ++cit )
208 else if (!doping_on_cells && mesh_reader.vertex_scalar_field(doping_n_key, j).is_valid() )
210 log::debug()<<
"* read_device(): Using '" << doping_n_key <<
"' as donor doping per vertex." << std::endl;
211 log::debug()<<
"* read_device(): Using '" << doping_p_key <<
"' as acceptor doping per vertex." << std::endl;
213 scalar_vertex_data
const & data_n = mesh_reader.vertex_scalar_field(doping_n_key, j);
214 scalar_vertex_data
const & data_p = mesh_reader.vertex_scalar_field(doping_p_key, j);
216 if (!data_n.is_valid() || !data_p.is_valid())
218 log::error() <<
"* read_device(): Invalid doping key on vertices !" << std::endl;
223 for ( CellIterator cit = cells.begin(); cit != cells.end(); ++cit )
225 double doping_n = 1.0;
226 double doping_p = 1.0;
227 VertexOnCellContainer vertices(*cit);
230 for ( VertexOnCellIterator vocit = vertices.begin(); vocit != vertices.end(); ++vocit )
232 if (data_n.at(*vocit))
234 doping_n *= data_n.at(*vocit);
237 if (data_p.at(*vocit))
239 doping_p *= data_p.at(*vocit);
251 viennashe::log::error() <<
"* read_device(): None or inconsistent (cells,vertices) donor doping found! " << std::endl;
259 template <
typename DeviceT,
typename SimulatorT>
262 std::string filename,
263 std::string quantity_name,
264 std::string vtk_quantity_key)
266 typedef typename DeviceT::mesh_type MeshType;
267 typedef typename DeviceT::segment_type SegmentType;
269 typedef typename viennagrid::result_of::cell<MeshType>::type CellType;
270 typedef typename viennagrid::result_of::vertex<MeshType>::type VertexType;
272 typedef typename viennagrid::result_of::const_cell_range<SegmentType>::type CellContainer;
273 typedef typename viennagrid::result_of::iterator<CellContainer>::type CellIterator;
275 typedef typename viennagrid::result_of::const_vertex_range<CellType>::type VertexOnCellContainer;
276 typedef typename viennagrid::result_of::iterator<VertexOnCellContainer>::type VertexOnCellIterator;
278 typedef typename viennagrid::result_of::field<const std::deque<double>, CellType >::type scalar_cell_data;
279 typedef typename viennagrid::result_of::field<const std::deque<double>, VertexType >::type scalar_vertex_data;
285 DeviceT dummy_device;
286 dummy_device.load_device(my_vtk_reader);
288 const std::size_t num_segments = dummy_device.segmentation().size();
289 log::info() <<
"* read_initial_guess_vtk(): There are " << num_segments <<
" segments." << std::endl;
292 for (std::size_t j = 0; j < num_segments; ++j)
294 log::debug() <<
"* read_initial_guess_vtk():" << std::endl;
296 if(mesh_reader.scalar_cell_data_names(j).size() > 0)
297 log::debug() <<
" Segment " << j <<
" has the following scalar CELL quantities: " << std::endl;
298 for(std::size_t i = 0; i < mesh_reader.scalar_cell_data_names(j).size(); ++i)
299 log::debug() <<
"\t" << mesh_reader.scalar_cell_data_names(j)[i] << std::endl;
301 if(mesh_reader.scalar_vertex_data_names(j).size() > 0)
302 log::debug() <<
" Segment " << j <<
" has the following scalar VERTEX quantities: " << std::endl;
303 for(std::size_t i = 0; i < mesh_reader.scalar_vertex_data_names(j).size(); ++i)
304 log::debug() <<
"\t" << mesh_reader.scalar_vertex_data_names(j)[i] << std::endl;
309 viennashe::log::info() <<
"* read_initial_guess_vtk(): Setting quantity as initial guess ..." << std::endl;
311 std::deque<double> cell_data(viennagrid::cells(
device.
mesh()).size());
312 for (std::size_t j = 0; j < num_segments; ++j)
314 bool quantity_on_cells =
false;
316 std::vector<std::string> cell_data_names = mesh_reader.scalar_cell_data_names(j);
317 for (std::size_t n=0; n<cell_data_names.size(); ++n)
319 if (cell_data_names[n] == vtk_quantity_key)
320 quantity_on_cells =
true;
323 if (quantity_on_cells && mesh_reader.cell_scalar_field(vtk_quantity_key, j).is_valid() )
325 log::debug()<<
"* read_initial_guess_vtk(): Using '" << vtk_quantity_key <<
"' as VTK quantity key for " << quantity_name <<
" per cell on segment " << j <<
"." << std::endl;
327 scalar_cell_data
const & data = mesh_reader.cell_scalar_field(vtk_quantity_key, j);
329 if (!data.is_valid())
331 log::error() <<
"* read_initial_guess_vtk(): Invalid quantity key on cells !" << std::endl;
335 CellContainer cells(dummy_device.segment(j));
336 for ( CellIterator cit = cells.begin(); cit != cells.end(); ++cit )
337 cell_data.at(cit->id().get()) = data.at(*cit);
339 else if (!quantity_on_cells && mesh_reader.vertex_scalar_field(vtk_quantity_key, j).is_valid() )
341 log::debug()<<
"* read_initial_guess_vtk(): Using '" << vtk_quantity_key <<
"' as VTK quantity key for " << quantity_name <<
" per vertex on segment " << j <<
"." << std::endl;
343 scalar_vertex_data
const & data = mesh_reader.vertex_scalar_field(vtk_quantity_key, j);
345 if (!data.is_valid())
347 log::error() <<
"* read_device(): Invalid quantity key on vertices !" << std::endl;
351 CellContainer cells(dummy_device.segment(j));
352 for ( CellIterator cit = cells.begin(); cit != cells.end(); ++cit )
355 VertexOnCellContainer vertices(*cit);
356 const double N =
static_cast<double>( vertices.size() );
357 for ( VertexOnCellIterator vocit = vertices.begin(); vocit != vertices.end(); ++vocit )
358 value += data.at(*vocit);
359 cell_data.at(cit->id().get()) = value / N;
364 viennashe::log::error() <<
"* read_initial_guess_vtk(): None or inconsistent (cells,vertices) quantity data for " << quantity_name <<
" found! " << std::endl;
369 typename viennagrid::result_of::accessor<std::deque<double>, CellType>::type cell_data_wrapper(cell_data);
381 template <
typename DeviceType >
384 read_device(
device, filename,
"doping_n",
"doping_p",
"material");
void set_material(long material_id, cell_type const &elem)
Sets the material ID on a cell.
void load_device(DeviceLoaderType &loader)
segmentation_type const & segmentation() const
segment_type const & segment(segment_id_type id) const
void set_doping_p(double value, cell_type const &c)
Sets the acceptor doping (in m^-3) in the specified cell.
MeshT const & mesh() const
Returns the underlying mesh.
void set_doping_n(double value, cell_type const &c)
Sets the donator doping (in m^-3) in the specified cell.
Defines the physical properties of a device, e.g. doping. This is the implementation for 2d and highe...
Class for self-consistent SHE simulations.
void set_initial_guess(std::string quan_name, QuantityAccessorT const &quan_acc)
Transfers the inital guess for the given quantity.
Contains forward declarations and definition of small classes that must be defined at an early stage.
All the exceptions used within the viennashe::io namespace.
A logging facility providing fine-grained control over logging in ViennaSHE.
A very simple material database. Needs to be replaced by something more versatile soon.
bool read_device_vtk(DeviceType &device, std::string filename, std::string doping_n_key, std::string doping_p_key, std::string material_key)
Reads and initalises a device from a VTK file. If material_key is empty the material IDs will not be ...
bool read_initial_guess_vtk(DeviceT const &device, SimulatorT &simulator, std::string filename, std::string quantity_name, std::string vtk_quantity_key)
logger< true > error()
Used to log errors. The logging level is logERROR.
logger< true > debug()
Used to log debug output. The logging level is logDEBUG.
logger< true > warning()
Used to log warnings. The logging level is logWARNING.
logger< true > info()
Used to log infos. The logging level is logINFO.
The main ViennaSHE namespace. All functionality resides inside this namespace.
Provides a number of fundamental constants. All constants in SI units.
viennagrid::io::vtk_reader< MeshT > vtk_reader_type
mesh_generator_vtk(std::string filename)
void operator()(MeshT &mesh, SegT &seg)
vtk_reader_type & reader()