Correct current logger bin numbers

This commit is contained in:
Jonas Kellerer
2021-05-10 16:06:53 +02:00
parent e22616bd0e
commit 6da1418692
10 changed files with 214 additions and 79 deletions
+4 -4
View File
@@ -14,19 +14,19 @@ CurrentLogger::CurrentLogger(
std::string output_filename): std::string output_filename):
Particle_Logger_Base(this_species), Particle_Logger_Base(this_species),
N_pos_r( N_pos_r(
num_planes_z, z_min, z_max, num_planes_z-1, z_min, z_max, // -1 through |[0,n]| = n+1
num_planes_r, r_max, num_planes_r, r_max,
energy_decade_min, energy_decade_max ), energy_decade_min, energy_decade_max ),
N_neg_r( N_neg_r(
num_planes_z, z_min, z_max, num_planes_z-1, z_min, z_max, // -1 through |[0,n]| = n+1
num_planes_r, r_max, num_planes_r, r_max,
energy_decade_min, energy_decade_max ), energy_decade_min, energy_decade_max ),
N_pos_z( N_pos_z(
num_planes_z, z_min, z_max, num_planes_z-1, z_min, z_max, // -1 through |[0,n]| = n+1
num_planes_r, r_max, num_planes_r, r_max,
energy_decade_min, energy_decade_max ), energy_decade_min, energy_decade_max ),
N_neg_z( N_neg_z(
num_planes_z, z_min, z_max, num_planes_z-1, z_min, z_max, // -1 through |[0,n]| = n+1
num_planes_r, r_max, num_planes_r, r_max,
energy_decade_min, energy_decade_max ), energy_decade_min, energy_decade_max ),
output_filename_(output_filename) output_filename_(output_filename)
@@ -0,0 +1,55 @@
import numpy as np
import h5py as h5py
import os
import sys
import matplotlib
import matplotlib.pyplot as plt
from functions.calculate_currents_from_karl import *
from functions.calculate_density_from_karl import *
import functions.plot_standards as pst
def currentToH5(filename_densities, filename_currents, filename_output):
keys = ["/e-", "/T+", "/T2+", "/T3+"]
print("Get Current: e-")
total_current_e = particleCurrentZ(filename_currents, filename_densities, keys[0])
current_e = np.sum(total_current_e,2)
print("Get Current: T+")
total_current_T = particleCurrentZ(filename_currents, filename_densities, keys[1])
current_T = np.sum(total_current_T,2)
print("Get Current: T2+")
total_current_T2 = particleCurrentZ(filename_currents, filename_densities, keys[2])
current_T2 = np.sum(total_current_T2,2)
print("Get Current: T3+")
total_current_T3 = particleCurrentZ(filename_currents, filename_densities, keys[3])
current_T3 = np.sum(total_current_T3,2)
f = h5py.File(filename_output, 'a')
key_electrons = "Current/electrons"
if key_electrons in f:
del f[key_electrons]
f.create_dataset(key_electrons, data=current_e, dtype=float)
key_T = "Current/T+"
if key_T in f:
del f[key_T]
f.create_dataset(key_T, data=current_T, dtype=float)
key_T2 = "Current/T2+"
if key_T2 in f:
del f[key_T2]
f.create_dataset(key_T2, data=current_T2, dtype=float)
key_T3 = "Current/T3+"
if key_T3 in f:
del f[key_T3]
f.create_dataset(key_T3, data=current_T3, dtype=float)
f.close()
@@ -12,11 +12,6 @@ import functions.plot_standards as pst
def densityToH5(filename, filename_output): def densityToH5(filename, filename_output):
keys = ["/Populations/e-/e-", "/Populations/T+/T+", "/Populations/T2+/T2+", "/Populations/T3+/T3+"] keys = ["/Populations/e-/e-", "/Populations/T+/T+", "/Populations/T2+/T2+", "/Populations/T3+/T3+"]
zmins = getZBinsMin(filename, keys[0])
zmaxs = getZBinsMax(filename, keys[0])
Emins = getEBinsMin(filename, keys[0])
Emaxs = getEBinsMax(filename, keys[0])
print("Get Density: e-") print("Get Density: e-")
total_density_e = getDensities(filename, keys[0]) total_density_e = getDensities(filename, keys[0])
density_e = np.sum(total_density_e,3)[:,:,0] density_e = np.sum(total_density_e,3)[:,:,0]
@@ -35,8 +30,20 @@ def densityToH5(filename, filename_output):
total_density = np.array((density_T + density_T2 + density_T3 -density_e)/1E6) total_density = np.array((density_T + density_T2 + density_T3 -density_e)/1E6)
f = h5py.File(filename_output, 'w') f = h5py.File(filename_output, 'a')
key_density = "Density/total" key_density = "Density/total"
if key_density in f:
del f[key_density]
f.create_dataset(key_density, data=np.transpose(total_density), dtype=float)
key_electrons = "Density/electrons"
if key_electrons in f:
del f[key_electrons]
f.create_dataset(key_electrons, data=np.transpose(density_e/1E6), dtype=float)
key_ions = "Density/ions"
if key_ions in f:
del f[key_ions]
f.create_dataset(key_ions, data=np.transpose((density_T + density_T2 + density_T3)/1E6), dtype=float)
f.create_dataset(key_density, data=total_density, dtype=float)
f.close() f.close()
@@ -2,13 +2,13 @@
<Parameters> <Parameters>
<Geometry> <Geometry>
<Length>18</Length> <Length>1300</Length>
<Radius>4.5</Radius> <Radius>4.5</Radius>
</Geometry> </Geometry>
<DensityGrid> <DensityGrid>
<Bins> <Bins>
<R>5</R> <R>10</R>
<Z>16</Z> <Z>16</Z>
</Bins> </Bins>
</DensityGrid> </DensityGrid>
@@ -24,7 +24,7 @@
<Boundary> <Boundary>
<Rear> <Rear>
<Type>1</Type> <Type>1</Type>
<Potential>0.003333333333333333</Potential> <Potential>0.0</Potential>
</Rear> </Rear>
<Tube> <Tube>
<Type>1</Type> <Type>1</Type>
@@ -35,19 +35,31 @@
</DPS> </DPS>
</Boundary> </Boundary>
<MaxIterations>1000</MaxIterations> <MaxIterations>10000</MaxIterations>
<Precission>0.0001</Precission> <Precission>1E-6</Precission>
</Solver> </Solver>
<Output> <Output>
<Potential> <Potential>
<Filename>./output/potential.dat</Filename> <H5File>
<Filename>./output/output.h5</Filename>
<H5Group>/Potential</H5Group>
<H5DatasetName>potential</H5DatasetName>
</H5File>
</Potential> </Potential>
<Density> <Density>
<Filename>./output/density.dat</Filename> <H5File>
<Filename>./output/output.h5</Filename>
<H5Group>/Density</H5Group>
<H5DatasetName>density</H5DatasetName>
</H5File>
</Density> </Density>
<Efield> <Efield>
<Filename>./output/efield_z.dat</Filename> <H5File>
<Filename>./output/output.h5</Filename>
<H5Group>/Efield</H5Group>
<H5DatasetName>efield</H5DatasetName>
</H5File>
</Efield> </Efield>
</Output> </Output>
@@ -36,29 +36,7 @@ class ElectricField {
blitz::Array<double,2> data_z_; blitz::Array<double,2> data_z_;
}; };
/* ElectricField interpolateOnGrid(ElectricField& electric_field);
Density interpolateOnGrid(Density& denstity);
class Potential {
public:
Potential(blitz::Array<double,2>& data);
Potential(int nr, int nz);
blitz::Array<double,2> data() const;
private:
blitz::Array<double,2> data_;
};
class ElectricField {
};
ElectricField interpolateOnGrid(
ElectricField& electric_field,
double dr_new,
double dz_new);
*/
}; };
@@ -5,7 +5,7 @@
#include "Fields.h" #include "Fields.h"
#include "Boundary.h" #include "Boundary.h"
#include "PoissonSolver.h" #include "PoissonSolver.h"
#include <vector>
namespace IO { namespace IO {
Fields::Density denstiy(const Parameter& parameter); Fields::Density denstiy(const Parameter& parameter);
Boundary rearwallBoundary(const Parameter& parameter); Boundary rearwallBoundary(const Parameter& parameter);
@@ -20,6 +20,14 @@ namespace IO {
void save(const Fields::Potential& potential, const Parameter& parameter); void save(const Fields::Potential& potential, const Parameter& parameter);
void save(const Fields::Density& density, const Parameter& parameter); void save(const Fields::Density& density, const Parameter& parameter);
void save(const Fields::ElectricField& density, const Parameter& parameter); void save(const Fields::ElectricField& density, const Parameter& parameter);
void save(
const std::vector<std::vector<double>>& data,
std::string filename,
std::string group,
std::string path);
std::vector<std::vector<double>> convert(const blitz::Array<double,2>& data);
}; };
#endif /* IO_H */ #endif /* IO_H */
@@ -72,5 +72,27 @@ namespace Fields
{ {
} }
ElectricField interpolateOnGrid(ElectricField& electric_field)
{
const blitz::TinyVector<int,2> extent = electric_field.data_r_.extent();
const blitz::TinyVector<int,2> interpol_extent =
extent - blitz::TinyVector<int,2>(0,1);
blitz::Array<double,2> interpolated_r(interpol_extent);
blitz::Array<double,2> interpolated_z(interpol_extent);
interpolated_r = 0.;
interpolated_z = 0.;
for(int i = 0; i < interpol_extent(0); ++i){
for(int j = 0; j < interpol_extent(1); ++j){
interpolated_r(i,j) =
(electric_field.data_r_(i,j+1) + electric_field.data_r_(i,j))/2;
interpolated_z(i,j) =
(electric_field.data_z_(i+1,j) + electric_field.data_z_(i,j))/2;
}
}
return ElectricField(interpolated_r, interpolated_z);
}
} }
@@ -128,48 +128,99 @@ void save(const Fields::Potential& potential, const Parameter& parameter)
{ {
std::string filename = std::string( std::string filename = std::string(
parameter.document.first_element_by_path( parameter.document.first_element_by_path(
"Parameters/Output/Potential").child_value("Filename")); "Parameters/Output/Potential/H5File").child_value("Filename"));
std::ofstream outdata;
outdata.open(filename); std::string group = std::string(
for(int i = 0; i < potential.data_.extent(0); ++i){ parameter.document.first_element_by_path(
for(int j = 0; j<potential.data_.extent(1)-1; ++j){ "Parameters/Output/Potential/H5File").child_value("H5Group"));
outdata << potential.data_(i,j) << ",";
} std::string path = group + "/" + std::string(
outdata << potential.data_(i,potential.data_.extent(1)-1) << "\n"; parameter.document.first_element_by_path(
} "Parameters/Output/Potential/H5File").child_value("H5DatasetName"));
outdata.close();
save(convert(potential.data_), filename, group, path);
} }
void save(const Fields::Density& density, const Parameter& parameter) void save(const Fields::Density& density, const Parameter& parameter)
{ {
std::string filename = std::string( std::string filename = std::string(
parameter.document.first_element_by_path( parameter.document.first_element_by_path(
"Parameters/Output/Density").child_value("Filename")); "Parameters/Output/Density/H5File").child_value("Filename"));
std::ofstream outdata;
outdata.open(filename); std::string group = std::string(
for(int i = 0; i < density.data_.extent(0); ++i){ parameter.document.first_element_by_path(
for(int j = 0; j<density.data_.extent(1)-1; ++j){ "Parameters/Output/Density/H5File").child_value("H5Group"));
outdata << density.data_(i,j) << ",";
} std::string path = group + "/" + std::string(
outdata << density.data_(i,density.data_.extent(1)-1) << "\n"; parameter.document.first_element_by_path(
} "Parameters/Output/Density/H5File").child_value("H5DatasetName"));
outdata.close();
save(convert(density.data_), filename, group, path);
} }
void save(const Fields::ElectricField& field, const Parameter& parameter) void save(const Fields::ElectricField& field, const Parameter& parameter)
{ {
std::string filename = std::string( std::string filename = std::string(
parameter.document.first_element_by_path( parameter.document.first_element_by_path(
"Parameters/Output/Efield").child_value("Filename")); "Parameters/Output/Efield/H5File").child_value("Filename"));
std::ofstream outdata;
outdata.open(filename); std::string group = std::string(
for(int i = 0; i < field.data_z_.extent(0); ++i){ parameter.document.first_element_by_path(
for(int j = 0; j<field.data_z_.extent(1)-1; ++j){ "Parameters/Output/Efield/H5File").child_value("H5Group"));
outdata << field.data_z_(i,j) << ",";
} std::string path = group + "/" + std::string(
outdata << field.data_z_(i,field.data_z_.extent(1)-1) << "\n"; parameter.document.first_element_by_path(
} "Parameters/Output/Efield/H5File").child_value("H5DatasetName"));
outdata.close();
save(convert(field.data_r_), filename, group, path+"_r");
save(convert(field.data_z_), filename, group, path+"_z");
} }
void save(
const std::vector<std::vector<double>>& data,
std::string filename,
std::string group,
std::string path)
{
HighFive::File file(
filename,
HighFive::File::ReadWrite | HighFive::File::Create);
if(!file.exist(group)){
file.createGroup(group);
}
if(file.exist(path)){
HighFive::DataSet dataset = file.getDataSet(path);
dataset.write(data);
} else{
HighFive::DataSet dataset =
file.createDataSet<double>(path, HighFive::DataSpace::From(data));
dataset.write(data);
}
}
std::vector<std::vector<double>> convert(
const blitz::Array<double,2>& data)
{
const blitz::TinyVector<int,2> start = data.lbound();
const blitz::TinyVector<int,2> end = data.ubound();
std::vector<std::vector<double>> data_v;
for(int i = start(0); i<=end(0); ++i){
std::vector<double> xs;
for(int j = start(1); j<=end(1); ++j){
xs.push_back(data(i,j));
}
data_v.push_back(xs);
}
return data_v;
}
}; };
@@ -75,12 +75,12 @@ void PoissonSolver::iterationPotential(
} }
// apply boundary condition left // apply boundary condition left
if(rearwall.type == BoundaryType::pec){ if(rearwall.type == pec){
for(int i = 0; i<nr; ++i){ for(int i = 0; i<nr; ++i){
potential.data_(i,0) = rearwall.data; potential.data_(i,0) = rearwall.data;
} }
} }
else if (rearwall.type == BoundaryType::pml){ else if (rearwall.type == pml){
for(int i = 0; i<nr; ++i){ for(int i = 0; i<nr; ++i){
potential.data_(i,0) = potential.data_(i,1); potential.data_(i,0) = potential.data_(i,1);
} }
@@ -88,12 +88,12 @@ void PoissonSolver::iterationPotential(
// apply boundary condition right // apply boundary condition right
if(dps.type == BoundaryType::pec){ if(dps.type == pec){
for(int i = 0; i<nr; ++i){ for(int i = 0; i<nr; ++i){
potential.data_(i,nz-1) = 0.; potential.data_(i,nz-1) = 0.;
} }
} }
else if (dps.type == BoundaryType::pml){ else if (dps.type == pml){
for(int i = 0; i<nr; ++i){ for(int i = 0; i<nr; ++i){
potential.data_(i,nz-1) = potential.data_(i,nz-2); potential.data_(i,nz-1) = potential.data_(i,nz-2);
} }
@@ -36,8 +36,10 @@ int main(int argc, char *argv[]){
radius, radius,
length); length);
Fields::ElectricField efield_interpolated = Fields::interpolateOnGrid(efield);
IO::save(potential, parameter); IO::save(potential, parameter);
IO::save(efield, parameter); IO::save(efield_interpolated, parameter);
IO::save(interpolated, parameter); IO::save(interpolated, parameter);
return 0; return 0;