Now integrate() can return the full trajectory and plotted in the Python; improved readablity
This commit is contained in:
parent
a2219af116
commit
a624814a0c
5 changed files with 144 additions and 106 deletions
112
loadtxt.cpp
112
loadtxt.cpp
|
|
@ -1,73 +1,69 @@
|
|||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include "loadtxt.h"
|
||||
|
||||
//TODO if cols is an empty vector, get all columns from the file
|
||||
//TODO error checking
|
||||
|
||||
class Loadtxt {
|
||||
public:
|
||||
Loadtxt(std::string file_name, std::vector<int> cols)
|
||||
{
|
||||
std::sort(cols.begin(), cols.end());
|
||||
n_cols = cols.size();
|
||||
const int tmp_number_of_rows = 16384;
|
||||
int n_rows_alloc = tmp_number_of_rows;
|
||||
buffer = (double*)malloc(n_cols * sizeof(double) * n_rows_alloc);
|
||||
std::ifstream file(file_name);
|
||||
std::string line;
|
||||
int row = -1;
|
||||
while (getline(file, line)) {
|
||||
if (line[line.find_first_not_of(whitespaces)]=='#') continue;
|
||||
if (++row >= n_rows_alloc) {
|
||||
n_rows_alloc += tmp_number_of_rows;
|
||||
buffer = (double*)realloc(buffer, n_cols * sizeof(double) * n_rows_alloc);
|
||||
}
|
||||
line_to_buf(cols, line, buffer + row*n_cols);
|
||||
Loadtxt::Loadtxt(std::string file_name, std::vector<int> cols)
|
||||
{
|
||||
std::sort(cols.begin(), cols.end());
|
||||
n_cols = cols.size();
|
||||
const int tmp_number_of_rows = 16384;
|
||||
int n_rows_alloc = tmp_number_of_rows;
|
||||
buffer = (double*)malloc(n_cols * sizeof(double) * n_rows_alloc);
|
||||
std::ifstream file(file_name);
|
||||
std::string line;
|
||||
int row = -1;
|
||||
while (getline(file, line)) {
|
||||
if (line[line.find_first_not_of(whitespaces)]=='#') continue;
|
||||
if (++row >= n_rows_alloc) {
|
||||
n_rows_alloc += tmp_number_of_rows;
|
||||
buffer = (double*)realloc(buffer, n_cols * sizeof(double) * n_rows_alloc);
|
||||
}
|
||||
file.close();
|
||||
buffer = (double*)realloc(buffer, n_cols * sizeof(double) * (++row));
|
||||
n_rows = row;
|
||||
line_to_buf(cols, line, buffer + row*n_cols);
|
||||
}
|
||||
~Loadtxt()
|
||||
{
|
||||
free(buffer);
|
||||
}
|
||||
std::vector<std::vector<double>> get_cols()
|
||||
{
|
||||
std::vector<std::vector<double>> data(n_cols);
|
||||
for (int col=0; col<n_cols; col++) data[col] = std::vector<double>(n_rows);
|
||||
for (int row=0; row<n_rows; row++) {
|
||||
for (int col=0; col<n_cols; col++) {
|
||||
data[col][row] = buffer[row*n_cols + col];
|
||||
}
|
||||
file.close();
|
||||
buffer = (double*)realloc(buffer, n_cols * sizeof(double) * (++row));
|
||||
n_rows = row;
|
||||
}
|
||||
|
||||
Loadtxt::~Loadtxt()
|
||||
{
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
std::vector<std::vector<double>> Loadtxt::get_cols()
|
||||
{
|
||||
std::vector<std::vector<double>> data(n_cols);
|
||||
for (int col=0; col<n_cols; col++) data[col] = std::vector<double>(n_rows);
|
||||
for (int row=0; row<n_rows; row++) {
|
||||
for (int col=0; col<n_cols; col++) {
|
||||
data[col][row] = buffer[row*n_cols + col];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
private:
|
||||
const char *whitespaces = " \t";
|
||||
void line_to_buf(std::vector<int> cols, std::string line, double *buffer)
|
||||
{
|
||||
int n_cols = cols.size();
|
||||
line = line.substr(line.find_first_not_of(whitespaces));
|
||||
auto pos = line.find_first_of(whitespaces, 1);
|
||||
int col=0, idx=0;
|
||||
std::vector<double> data;
|
||||
while (pos != std::string::npos) {
|
||||
std::string num_as_string;
|
||||
num_as_string = line.substr(0, pos);
|
||||
pos = line.find_first_not_of(whitespaces, pos);
|
||||
line = line.substr(pos, std::string::npos);
|
||||
pos = line.find_first_of(whitespaces, 1);
|
||||
if (col++ == cols[idx]) buffer[idx++] = std::stod(num_as_string);
|
||||
}
|
||||
if (col++ == cols[idx]) buffer[idx++] = std::stod(line);
|
||||
if (idx < n_cols) throw;
|
||||
return data;
|
||||
}
|
||||
|
||||
void Loadtxt::line_to_buf(std::vector<int> cols, std::string line, double *buffer)
|
||||
{
|
||||
int n_cols = cols.size();
|
||||
line = line.substr(line.find_first_not_of(whitespaces));
|
||||
auto pos = line.find_first_of(whitespaces, 1);
|
||||
int col=0, idx=0;
|
||||
std::vector<double> data;
|
||||
while (pos != std::string::npos) {
|
||||
std::string num_as_string;
|
||||
num_as_string = line.substr(0, pos);
|
||||
pos = line.find_first_not_of(whitespaces, pos);
|
||||
line = line.substr(pos, std::string::npos);
|
||||
pos = line.find_first_of(whitespaces, 1);
|
||||
if (col++ == cols[idx]) buffer[idx++] = std::stod(num_as_string);
|
||||
}
|
||||
double *buffer;
|
||||
int n_rows, n_cols;
|
||||
};
|
||||
if (col++ == cols[idx]) buffer[idx++] = std::stod(line);
|
||||
if (idx < n_cols) throw;
|
||||
}
|
||||
|
||||
// Below is a deomonstration. The file has multiple columns but we are only
|
||||
// interested in the second and fourth. We pass the file name and the column
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue