-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileio.cpp
More file actions
145 lines (123 loc) · 3.03 KB
/
Copy pathfileio.cpp
File metadata and controls
145 lines (123 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* @file fileio.cpp
* @ingroup Langevin
* @brief Routines for loading and saving files
* @version $spbd_version$
* @author Kherim Willems
*
* @attention
* @verbatim
*
* SPBD -- Single Protein Brownian Dynamics
*
* Kherim Willems (kherim@kher.im)
*
* @endverbatim
*/
#include "fileio.h"
int readFileToStrings(
std::string const &filename,
std::vector<std::string> &lines
)
{
// Allocate variables
std::ifstream infile;
std::string line;
// Read in file contents
try
{
// Opening the file for reading
infile.open(filename.c_str(), std::ios::in);
// Read all the lines
while (std::getline(infile, line))
{
lines.push_back(line);
}
infile.close();
}
catch (std::exception e)
{
std::cout << "Exception opening file: " << filename << "\n"
<< "Exception: " << e.what() << std::endl;
}
return 0;
}
void writeSimulationResultsToFile(
langevin_simulation simu
)
{
writeTrajectoryToFile(
simu.conf.trajectoryOutputFile,
simu.out.positionVector,
simu.out.timeVector);
}
void writeTrajectoryToFile(
std::string const &filename,
std::vector<float> const &positionVector,
std::vector<unsigned long long> const &timeVector
)
{
// Allocate variables
std::ofstream outfile;
unsigned long long step;
float position;
try
{
// Open file for writing
outfile.open(filename.c_str(), std::ios::out | std::ios::trunc);
// Print out header
outfile << "step" << ", " << "position" << "\n";
// Print out all values to file
for (unsigned long long i = 0; i < timeVector.size(); i++)
{
// Load step number and postion
step = timeVector[i];
position = positionVector[i];
// Save data to file
outfile << step << ", " << position << "\n";
}
outfile.close();
}
catch (std::exception e)
{
std::cout << "Exception writing to file: " << filename << "\n"
<< "Exception: " << e.what() << std::endl;
}
}
template<typename Out>
void split(
const std::string &s,
char delim,
Out result)
{
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
if (!item.empty()) {
*(result++) = item;
}
}
}
std::vector<std::string> split(
const std::string &s,
char delim)
{
std::vector<std::string> elems;
split(s, delim, std::back_inserter(elems));
return elems;
}
std::vector<float> convertStringToFloatVector(
const std::vector<std::string> &v
)
{
// Create new vector
std::vector<float> float_v;
float_v.reserve(v.size());
// Loop over all elements and convert
for (std::vector<std::string>::size_type i = 0; i != v.size(); i++)
{
float_v.push_back(std::stof(v[i]));
}
return float_v;
}