ViennaSHE 1.3.0
Free open-source semiconductor device simulator using spherical harmonics expansions techniques.
vector.hpp
Go to the documentation of this file.
1#ifndef VIENNASHE_IO_VECTOR_HPP_
2#define VIENNASHE_IO_VECTOR_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
19#include <string>
20#include <iostream>
21#include <fstream>
22
24
29namespace viennashe
30{
31 namespace io
32 {
33
39 template <typename VectorType>
40 void read_vector_from_file(VectorType & vec,
41 const std::string & filename)
42 {
43 std::ifstream file(filename.c_str());
44
45 if (!file)
46 throw cannot_open_file_exception(filename);
47
48 std::size_t size;
49 file >> size;
50
51 //if(size == 0) throw std::range_error(std::string("There are zero vector-components in File: '") + filename + std::string("'"))
52
53 vec.resize(size, false);
54
55 for (std::size_t i = 0; i < size; ++i)
56 {
57 double element;
58 if (file.eof())
59 throw premature_end_of_file_exception(filename);
60 file >> element;
61 vec[i] = element;
62 }
63 }
64
70 template <typename VectorType>
71 void write_vector_to_file(VectorType & vec,
72 const std::string & filename)
73 {
74 std::ofstream writer(filename.c_str());
75
76 if (!writer)
77 throw cannot_open_file_exception(filename);
78
79 writer << vec.size() << std::endl;
80 for (std::size_t i = 0; i < vec.size(); ++i)
81 {
82 writer << vec[i] << std::endl;
83 }
84 }
85
86
87 } //namespace io
88}//namespace viennashe
89
90
91
92#endif
Exception which is thrown if a file cannot be opened.
Definition: exception.hpp:38
Exception which is thrown if a End-of-File is encountered even though further data is expected.
Definition: exception.hpp:44
All the exceptions used within the viennashe::io namespace.
void read_vector_from_file(VectorType &vec, const std::string &filename)
Reads a vector from a file.
Definition: vector.hpp:40
void write_vector_to_file(VectorType &vec, const std::string &filename)
Writes a vector to a file.
Definition: vector.hpp:71
The main ViennaSHE namespace. All functionality resides inside this namespace.
Definition: accessors.hpp:40