Files
katrin-plasma/src/mc_electron/vec3_container.cpp
T
Christian 1978039630 Added buffer for HDF5 output.
Added E-Field container.
2019-09-03 16:35:55 +02:00

90 lines
2.6 KiB
C++

//
// vec3_container.cpp
// mc_electron
//
// Created by Christian Reiling on 02.09.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#include "vec3_container.hpp"
#include "read_system.h"
Vec3_Container::Vec3_Container(int num_z_bins, int num_r_bins, int num_phi_bins, std::string load_path) {
z_scale = Lin_Scale(z_WGTS_start_m, z_WGTS_end_m, num_z_bins);
r_scale = Lin_Scale(0, r_WGTS_m, num_r_bins);
phi_scale = Lin_Scale(0, 180, num_phi_bins);
int num_tot_bins = num_z_bins * num_r_bins * num_phi_bins;
data = new vec3[num_tot_bins];
load_data(load_path);
}
Vec3_Container::~Vec3_Container() {
delete[] data;
}
void Vec3_Container::load_data(std::string load_path) {
int num_tot_bins = z_scale.get_number_bins() * r_scale.get_number_bins() * phi_scale.get_number_bins();
bool *check_array = new bool[num_tot_bins];
for (int i = 0; i< num_tot_bins; i++) {
check_array[i] = false;
}
std::ifstream importStream(load_path);
if (!importStream.good()) {
std::stringstream ss;
ss<<"Vec3_Container::load_data: Could not open file. \n loadFilePath= "<<load_path<<"\n";
throw MyException(ss.str());
}
std::string csv_line;
getline(importStream, csv_line); // ignore first line = header.
while (getline(importStream, csv_line)) {
remove_leading_whites(csv_line);
replace_delimiter(csv_line);
if ( check_ignore_line(csv_line) ) {
continue;
}
read_line(csv_line, check_array);
}
importStream.close();
for (int i = 0; i< num_tot_bins; i++) {
if (!check_array[i]) {
std::stringstream ss;
ss<<"Vec3_Container::load_data: Not all bins loaded.";
throw MyException(ss.str());
}
}
delete[] check_array;
}
void Vec3_Container::read_line(std::string csv_line, bool *check_array) {
std::stringstream ss;
ss.str(csv_line);
int temp_bin;
double temp_x, temp_y, temp_z;
if ((ss>>temp_bin && ss>>temp_x) && (ss>>temp_y && ss>>temp_z)) {
data[temp_bin] = vec3(temp_x, temp_y, temp_z);
check_array[temp_bin] = true;
} else {
std::cout<<"Vec3_Container::load_data: Warning could not read line in inputFile. \nloadFilePath\t"<<csv_line<<"\n";
}
}
vec3 Vec3_Container::get_data (const vec3 &loc_m) {
int bin = calc_data_bin(loc_m);
return data[bin];
}
int Vec3_Container::calc_data_bin(const vec3 &loc_m) {
double z_m = calc_z_m(loc_m);
double r_m = calc_r_m(loc_m);
double phi = calc_phi(loc_m);
int z_bin = z_scale.calc_index(z_m);
int r_bin = r_scale.calc_index(r_m);
int phi_bin = phi_scale.calc_index(phi);
return z_bin * r_scale.get_number_bins() * phi_scale.get_number_bins() + r_bin * phi_scale.get_number_bins() + phi_bin;
};