external poission solver for analysis
This commit is contained in:
@@ -0,0 +1,42 @@
|
|||||||
|
import numpy as np
|
||||||
|
import h5py as h5py
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import matplotlib
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
from functions.calculate_density_from_karl import *
|
||||||
|
import functions.plot_standards as pst
|
||||||
|
|
||||||
|
|
||||||
|
def densityToH5(filename, filename_output):
|
||||||
|
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-")
|
||||||
|
total_density_e = getDensities(filename, keys[0])
|
||||||
|
density_e = np.sum(total_density_e,3)[:,:,0]
|
||||||
|
|
||||||
|
print("Get Density: T+")
|
||||||
|
total_density_T = getDensities(filename, keys[1])
|
||||||
|
density_T = np.sum(total_density_T,3)[:,:,0]
|
||||||
|
|
||||||
|
print("Get Density: T2+")
|
||||||
|
total_density_T2 = getDensities(filename, keys[2])
|
||||||
|
density_T2 = np.sum(total_density_T2,3)[:,:,0]
|
||||||
|
|
||||||
|
print("Get Density: T3+")
|
||||||
|
total_density_T3 = getDensities(filename, keys[3])
|
||||||
|
density_T3 = np.sum(total_density_T3,3)[:,:,0]
|
||||||
|
|
||||||
|
total_density = np.array((density_T + density_T2 + density_T3 -density_e)/1E6)
|
||||||
|
|
||||||
|
f = h5py.File(filename_output, 'w')
|
||||||
|
key_density = "Density/total"
|
||||||
|
|
||||||
|
f.create_dataset(key_density, data=total_density, dtype=float)
|
||||||
|
f.close()
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
|
||||||
|
<Parameters>
|
||||||
|
<Geometry>
|
||||||
|
<Length>18</Length>
|
||||||
|
<Radius>4.5</Radius>
|
||||||
|
</Geometry>
|
||||||
|
|
||||||
|
<DensityGrid>
|
||||||
|
<Bins>
|
||||||
|
<R>5</R>
|
||||||
|
<Z>16</Z>
|
||||||
|
</Bins>
|
||||||
|
</DensityGrid>
|
||||||
|
<Input>
|
||||||
|
<Density>
|
||||||
|
<Filename>./config/density.h5</Filename>
|
||||||
|
<H5Path>/Density/total</H5Path>
|
||||||
|
</Density>
|
||||||
|
</Input>
|
||||||
|
|
||||||
|
|
||||||
|
<Solver>
|
||||||
|
<Boundary>
|
||||||
|
<Rear>
|
||||||
|
<Type>1</Type>
|
||||||
|
<Potential>0.003333333333333333</Potential>
|
||||||
|
</Rear>
|
||||||
|
<Tube>
|
||||||
|
<Type>1</Type>
|
||||||
|
<Potential>0.</Potential>
|
||||||
|
</Tube>
|
||||||
|
<DPS>
|
||||||
|
<Type>0</Type>
|
||||||
|
</DPS>
|
||||||
|
</Boundary>
|
||||||
|
|
||||||
|
<MaxIterations>1000</MaxIterations>
|
||||||
|
<Precission>0.0001</Precission>
|
||||||
|
</Solver>
|
||||||
|
|
||||||
|
<Output>
|
||||||
|
<Potential>
|
||||||
|
<Filename>./output/potential.dat</Filename>
|
||||||
|
</Potential>
|
||||||
|
<Density>
|
||||||
|
<Filename>./output/density.dat</Filename>
|
||||||
|
</Density>
|
||||||
|
<Efield>
|
||||||
|
<Filename>./output/efield_z.dat</Filename>
|
||||||
|
</Efield>
|
||||||
|
</Output>
|
||||||
|
|
||||||
|
</Parameters>
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef BOUNDARY_H
|
||||||
|
#define BOUNDARY_H
|
||||||
|
|
||||||
|
enum BoundaryType {
|
||||||
|
pml = 0,
|
||||||
|
pec = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
class Boundary {
|
||||||
|
public:
|
||||||
|
Boundary(BoundaryType type, double data);
|
||||||
|
BoundaryType type;
|
||||||
|
const double data;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* BOUNDARY_H */
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
#ifndef FIELDS_H
|
||||||
|
#define FIELDS_H
|
||||||
|
|
||||||
|
#include "blitz/array.h"
|
||||||
|
|
||||||
|
namespace Fields {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Density {
|
||||||
|
public:
|
||||||
|
Density(blitz::Array<double,2>& data);
|
||||||
|
Density(const Density& density);
|
||||||
|
|
||||||
|
blitz::Array<double,2> data() const;
|
||||||
|
blitz::Array<double,2> data_;
|
||||||
|
};
|
||||||
|
|
||||||
|
Density interpolateOnGrid(Density& denstity);
|
||||||
|
|
||||||
|
class Potential {
|
||||||
|
public:
|
||||||
|
Potential(blitz::Array<double,2>& data);
|
||||||
|
blitz::Array<double,2> data() const;
|
||||||
|
blitz::Array<double,2> data_;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
class ElectricField {
|
||||||
|
public:
|
||||||
|
ElectricField(
|
||||||
|
blitz::Array<double,2>& data_r,
|
||||||
|
blitz::Array<double,2>& data_z);
|
||||||
|
|
||||||
|
blitz::Array<double,2> data_r_;
|
||||||
|
blitz::Array<double,2> data_z_;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
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);
|
||||||
|
*/
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* FIELDS_H */
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef IO_H
|
||||||
|
#define IO_H
|
||||||
|
|
||||||
|
#include "Parameter.h"
|
||||||
|
#include "Fields.h"
|
||||||
|
#include "Boundary.h"
|
||||||
|
#include "PoissonSolver.h"
|
||||||
|
|
||||||
|
namespace IO {
|
||||||
|
Fields::Density denstiy(const Parameter& parameter);
|
||||||
|
Boundary rearwallBoundary(const Parameter& parameter);
|
||||||
|
Boundary tubewallBoundary(const Parameter& parameter);
|
||||||
|
Boundary dpsBoundary(const Parameter& parameter);
|
||||||
|
|
||||||
|
double length(const Parameter& parameter);
|
||||||
|
double radius(const Parameter& parameter);
|
||||||
|
|
||||||
|
PoissonSolver solver(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::ElectricField& density, const Parameter& parameter);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* IO_H */
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef PARAMETER_H
|
||||||
|
#define PARAMETER_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include "pugixml/pugixml.hpp"
|
||||||
|
|
||||||
|
class Parameter {
|
||||||
|
public:
|
||||||
|
Parameter(std::string filename);
|
||||||
|
const pugi::xml_document document;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* PARAMETER_H */
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
#ifndef POISSONSOLVER_H
|
||||||
|
#define POISSONSOLVER_H
|
||||||
|
|
||||||
|
#include "Fields.h"
|
||||||
|
#include "Boundary.h"
|
||||||
|
|
||||||
|
class PoissonSolver {
|
||||||
|
public:
|
||||||
|
PoissonSolver(double target_accuracy, int num_cycles);
|
||||||
|
|
||||||
|
Fields::Potential solve(
|
||||||
|
Fields::Density& density,
|
||||||
|
Boundary& rearwall,
|
||||||
|
Boundary& tubewall,
|
||||||
|
Boundary& dps,
|
||||||
|
double radius,
|
||||||
|
double length);
|
||||||
|
|
||||||
|
Fields::ElectricField solve(
|
||||||
|
Fields::Potential& potential,
|
||||||
|
double radius,
|
||||||
|
double length);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void iterationPotential(
|
||||||
|
Fields::Potential& potential,
|
||||||
|
Fields::Density& density,
|
||||||
|
Boundary& rearwall,
|
||||||
|
Boundary& tubewall,
|
||||||
|
Boundary& dps,
|
||||||
|
double dr,
|
||||||
|
double dz) const;
|
||||||
|
|
||||||
|
bool converged(
|
||||||
|
Fields::Potential& potential,
|
||||||
|
Fields::Density& density,
|
||||||
|
double dr,
|
||||||
|
double dz) const;
|
||||||
|
|
||||||
|
const double target_accuracy_;
|
||||||
|
const int num_cycles_;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* POISSONSOLVER_H */
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
dummy: all
|
||||||
|
|
||||||
|
DO_DEBUGGING = true
|
||||||
|
|
||||||
|
# Directories
|
||||||
|
PROJECT_DIR = $(HOMEBASE)/src/tools/analysis_jkellerer/poissonSolver
|
||||||
|
|
||||||
|
SRC_DIR = $(PROJECT_DIR)/src
|
||||||
|
TEST_DIR = $(PROJECT_DIR)/test
|
||||||
|
BIN_DIR = $(PROJECT_DIR)/bin
|
||||||
|
BUILD_DIR = $(PROJECT_DIR)/build
|
||||||
|
INCLUDES_DIR = $(PROJECT_DIR)/include
|
||||||
|
|
||||||
|
# Includes
|
||||||
|
include $(HOMEBASE)/src/makeinclude/ostype.mak
|
||||||
|
include $(HOMEBASE)/src/makeinclude/$(X_OSTYPE)
|
||||||
|
|
||||||
|
CODEBAUM_INC = -I$(HOMEBASE)/include
|
||||||
|
HDF5_INC = -I/usr/include/hdf5/serial
|
||||||
|
HDF5_LIB = -L/usr/lib/x86_64-linux-gnu/hdf5/serial/ -lhdf5
|
||||||
|
LIBS = -L$(HOMEBASE)/lib/$(X_OSTYPE)/ -lblitz -lm $(HDF5_LIB)
|
||||||
|
MAIN_INC = -I$(INCLUDES_DIR)
|
||||||
|
|
||||||
|
# Compiler instructions
|
||||||
|
CXXFLAGS=$(X_OFLAGS) $(BLITZ_INC) $(CODEBAUM_INC) $(MAIN_INC) $(HDF5_INC) -fno-finite-math-only
|
||||||
|
CXXFLAGS_TEST = $(CXXFLAGS)
|
||||||
|
|
||||||
|
ifeq ($(DO_DEBUGGING),true)
|
||||||
|
CXXFLAGS += -g -DBZ_DEBUG
|
||||||
|
endif
|
||||||
|
|
||||||
|
# Sources
|
||||||
|
SOURCES = $(wildcard $(SRC_DIR)/*.cpp)
|
||||||
|
#SOURCES_TEST = $(wildcard $(SRC_DIR)/*.cpp)
|
||||||
|
SOURCES_TEST = $(filter-out $(SRC_DIR)/main.cpp, $(wildcard $(SRC_DIR)/*.cpp))
|
||||||
|
SOURCES_TEST += $(wildcard $(TEST_DIR)/*.cpp)
|
||||||
|
|
||||||
|
# Objects
|
||||||
|
OBJECTS=$(addprefix $(BUILD_DIR)/, $(notdir $(SOURCES:.cpp=.o)) )
|
||||||
|
OBJECTS_TEST=$(addprefix $(BUILD_DIR)/, $(notdir $(SOURCES_TEST:.cpp=.o)) )
|
||||||
|
|
||||||
|
TARGET=$(BIN_DIR)/poisson_solver
|
||||||
|
TARGET_TEST=$(BIN_DIR)/poisson_solver_test
|
||||||
|
|
||||||
|
|
||||||
|
all: info $(TARGET)
|
||||||
|
|
||||||
|
testing: $(TARGET_TEST)
|
||||||
|
|
||||||
|
info:
|
||||||
|
@echo
|
||||||
|
@echo "-----> CONFIG = "$(CONFIG)
|
||||||
|
@echo "-----> X_OSTYPE = "$(X_OSTYPE)
|
||||||
|
@echo
|
||||||
|
|
||||||
|
$(TARGET): $(BUILD_DIR) $(BIN_DIR) $(OBJECTS)
|
||||||
|
$(CXX) $(OBJECTS) $(LIBS) -o $(TARGET)
|
||||||
|
@echo
|
||||||
|
@echo "Linking done"
|
||||||
|
@echo
|
||||||
|
|
||||||
|
$(TARGET_TEST): $(BUILD_DIR) $(BIN_DIR) $(OBJECTS_TEST)
|
||||||
|
$(CXX) $(OBJECTS_TEST) $(LIBS) -o $(TARGET_TEST)
|
||||||
|
@echo
|
||||||
|
@echo "Linking done"
|
||||||
|
@echo
|
||||||
|
|
||||||
|
$(BIN_DIR):
|
||||||
|
@mkdir -p $(BIN_DIR)
|
||||||
|
|
||||||
|
$(BUILD_DIR):
|
||||||
|
@mkdir -p $(BUILD_DIR)
|
||||||
|
|
||||||
|
$(BUILD_DIR)/%.o : $(SRC_DIR)/%.cpp
|
||||||
|
@echo "Create $(notdir $@) from $(notdir $<)"
|
||||||
|
@echo
|
||||||
|
$(CXX) $(CXXFLAGS) $< -c -o $@
|
||||||
|
|
||||||
|
$(BUILD_DIR)/%.o : $(TEST_DIR)/%.cpp
|
||||||
|
@echo "Create $@ from $<"
|
||||||
|
$(CXX) $(CXXFLAGS_TEST) $< -c -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-rm -rf $(BUILD_DIR)
|
||||||
|
-rm -rf $(BIN_DIR)
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#include "Boundary.h"
|
||||||
|
|
||||||
|
|
||||||
|
Boundary::Boundary(BoundaryType type, double data):
|
||||||
|
type(type),
|
||||||
|
data(data)
|
||||||
|
{}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
#include "Fields.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace Fields
|
||||||
|
{
|
||||||
|
Density::Density(blitz::Array<double,2>& data):
|
||||||
|
data_(data.copy())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Density::Density(const Density& density):
|
||||||
|
data_(density.data())
|
||||||
|
{}
|
||||||
|
|
||||||
|
blitz::Array<double,2> Density::data() const
|
||||||
|
{
|
||||||
|
return data_.copy();
|
||||||
|
}
|
||||||
|
|
||||||
|
Density interpolateOnGrid(Density& denstity)
|
||||||
|
{
|
||||||
|
const blitz::TinyVector<int,2> extent = denstity.data().extent();
|
||||||
|
const blitz::TinyVector<int,2> interpol_extent =
|
||||||
|
extent + blitz::TinyVector<int,2>(1,1);
|
||||||
|
|
||||||
|
blitz::Array<double,2> interpolated(interpol_extent);
|
||||||
|
interpolated = 0.;
|
||||||
|
|
||||||
|
// center values
|
||||||
|
for(int i = 1; i < interpol_extent(0)-1; ++i){
|
||||||
|
for(int j = 1; j < interpol_extent(1)-1; ++j){
|
||||||
|
interpolated(i,j) = (
|
||||||
|
denstity.data_(i-1, j-1)
|
||||||
|
+ denstity.data_(i-1, j)
|
||||||
|
+ denstity.data_(i, j-1)
|
||||||
|
+ denstity.data_(i,j))/4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// values for r=0
|
||||||
|
for(int j = 1; j < interpol_extent(1)-1; ++j){
|
||||||
|
interpolated(0,j) =
|
||||||
|
(denstity.data_(0,j-1) + denstity.data_(0,j))/2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// values for j = j_max
|
||||||
|
int j_max = interpol_extent(1)-1;
|
||||||
|
for(int i = 1; i < interpol_extent(0)-1; ++i){
|
||||||
|
interpolated(i,j_max) =
|
||||||
|
(denstity.data_(i-1, j_max-1) + denstity.data_(i, j_max-1))/2;
|
||||||
|
}
|
||||||
|
interpolated(0, j_max) = denstity.data_(0, j_max-1);
|
||||||
|
|
||||||
|
return Density(interpolated);
|
||||||
|
}
|
||||||
|
|
||||||
|
Potential::Potential(blitz::Array<double,2>& data):
|
||||||
|
data_(data.copy())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
blitz::Array<double,2> Potential::data() const
|
||||||
|
{
|
||||||
|
return data_.copy();
|
||||||
|
}
|
||||||
|
|
||||||
|
ElectricField::ElectricField(
|
||||||
|
blitz::Array<double,2>& data_r,
|
||||||
|
blitz::Array<double,2>& data_z):
|
||||||
|
data_r_(data_r.copy()),
|
||||||
|
data_z_(data_z.copy())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,175 @@
|
|||||||
|
#include "IO.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#include "highfive/H5File.hpp"
|
||||||
|
#include "highfive/H5DataSet.hpp"
|
||||||
|
#include "highfive/H5DataSpace.hpp"
|
||||||
|
|
||||||
|
#include <hdf5_hl.h>
|
||||||
|
|
||||||
|
namespace IO {
|
||||||
|
|
||||||
|
|
||||||
|
Fields::Density denstiy(const Parameter& parameter)
|
||||||
|
{
|
||||||
|
//blitz::Array<double,2> data(40,180);
|
||||||
|
//data = 0.;
|
||||||
|
//data(blitz::Range(0,1),blitz::Range(2,7)) = 1.;
|
||||||
|
//return Fields::Density(data);
|
||||||
|
|
||||||
|
std::string filename = std::string(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Input/Density").child_value("Filename"));
|
||||||
|
std::cout << filename << std::endl;
|
||||||
|
std::string path = std::string(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Input/Density").child_value("H5Path"));
|
||||||
|
std::cout << path << std::endl;
|
||||||
|
HighFive::File f(filename, HighFive::File::ReadOnly);
|
||||||
|
HighFive::DataSet dataset = f.getDataSet(path);
|
||||||
|
|
||||||
|
std::vector<std::vector<double>> data;
|
||||||
|
dataset.read(data);
|
||||||
|
|
||||||
|
const int nz = data[0].size();
|
||||||
|
const int nr = data.size();
|
||||||
|
blitz::Array<double,2> data2d(nr, nz);
|
||||||
|
|
||||||
|
for(int ri = 0; ri<data2d.shape()(0); ++ri){
|
||||||
|
for(int zi = 0; zi< data2d.shape()(1); ++zi){
|
||||||
|
data2d(ri, zi) = data[ri][zi];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Fields::Density(data2d);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Boundary rearwallBoundary(const Parameter& parameter)
|
||||||
|
{
|
||||||
|
const int type_i = std::atoi(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Solver/Boundary/Rear").child_value("Type"));
|
||||||
|
const BoundaryType type = static_cast<BoundaryType>(type_i);
|
||||||
|
if(type == BoundaryType::pec){
|
||||||
|
const double data = std::atof(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Solver/Boundary/Rear").child_value("Potential"));
|
||||||
|
return Boundary(type, data);
|
||||||
|
}
|
||||||
|
else if(type == BoundaryType::pml){
|
||||||
|
return Boundary(type, 0.);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Boundary tubewallBoundary(const Parameter& parameter)
|
||||||
|
{
|
||||||
|
const int type_i = std::atoi(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Solver/Boundary/Tube").child_value("Type"));
|
||||||
|
const BoundaryType type = static_cast<BoundaryType>(type_i);
|
||||||
|
if(type == BoundaryType::pec){
|
||||||
|
const double data = std::atof(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Solver/Boundary/Tube").child_value("Potential"));
|
||||||
|
return Boundary(type, data);
|
||||||
|
}
|
||||||
|
else if(type == BoundaryType::pml){
|
||||||
|
return Boundary(type, 0.);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Boundary dpsBoundary(const Parameter& parameter)
|
||||||
|
{
|
||||||
|
const int type_i = std::atoi(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Solver/Boundary/DPS").child_value("Type"));
|
||||||
|
const BoundaryType type = static_cast<BoundaryType>(type_i);
|
||||||
|
if(type == BoundaryType::pec){
|
||||||
|
const double data = std::atof(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Solver/Boundary/DPS").child_value("Potential"));
|
||||||
|
return Boundary(type, data);
|
||||||
|
}
|
||||||
|
else if(type == BoundaryType::pml){
|
||||||
|
return Boundary(type, 0.);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
double radius(const Parameter& parameter)
|
||||||
|
{
|
||||||
|
const double radius = std::atof(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Geometry").child_value("Radius"));
|
||||||
|
return radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
double length(const Parameter& parameter)
|
||||||
|
{
|
||||||
|
const double length = std::atof(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Geometry").child_value("Length"));
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
PoissonSolver solver(const Parameter& parameter)
|
||||||
|
{
|
||||||
|
|
||||||
|
double target_accuracy = std::atof(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Solver").child_value("Precission"));
|
||||||
|
int num_cycles = std::atoi(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Solver").child_value("MaxIterations"));
|
||||||
|
return PoissonSolver(target_accuracy, num_cycles);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void save(const Fields::Potential& potential, const Parameter& parameter)
|
||||||
|
{
|
||||||
|
std::string filename = std::string(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Output/Potential").child_value("Filename"));
|
||||||
|
std::ofstream outdata;
|
||||||
|
outdata.open(filename);
|
||||||
|
for(int i = 0; i < potential.data_.extent(0); ++i){
|
||||||
|
for(int j = 0; j<potential.data_.extent(1)-1; ++j){
|
||||||
|
outdata << potential.data_(i,j) << ",";
|
||||||
|
}
|
||||||
|
outdata << potential.data_(i,potential.data_.extent(1)-1) << "\n";
|
||||||
|
}
|
||||||
|
outdata.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void save(const Fields::Density& density, const Parameter& parameter)
|
||||||
|
{
|
||||||
|
std::string filename = std::string(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Output/Density").child_value("Filename"));
|
||||||
|
std::ofstream outdata;
|
||||||
|
outdata.open(filename);
|
||||||
|
for(int i = 0; i < density.data_.extent(0); ++i){
|
||||||
|
for(int j = 0; j<density.data_.extent(1)-1; ++j){
|
||||||
|
outdata << density.data_(i,j) << ",";
|
||||||
|
}
|
||||||
|
outdata << density.data_(i,density.data_.extent(1)-1) << "\n";
|
||||||
|
}
|
||||||
|
outdata.close();
|
||||||
|
}
|
||||||
|
void save(const Fields::ElectricField& field, const Parameter& parameter)
|
||||||
|
{
|
||||||
|
std::string filename = std::string(
|
||||||
|
parameter.document.first_element_by_path(
|
||||||
|
"Parameters/Output/Efield").child_value("Filename"));
|
||||||
|
std::ofstream outdata;
|
||||||
|
outdata.open(filename);
|
||||||
|
for(int i = 0; i < field.data_z_.extent(0); ++i){
|
||||||
|
for(int j = 0; j<field.data_z_.extent(1)-1; ++j){
|
||||||
|
outdata << field.data_z_(i,j) << ",";
|
||||||
|
}
|
||||||
|
outdata << field.data_z_(i,field.data_z_.extent(1)-1) << "\n";
|
||||||
|
}
|
||||||
|
outdata.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#include "Parameter.h"
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
pugi::xml_document openFile(std::string filename)
|
||||||
|
{
|
||||||
|
pugi::xml_document parameter_file;
|
||||||
|
pugi::xml_parse_result result = parameter_file.load_file(filename.c_str());
|
||||||
|
|
||||||
|
if(result.status == pugi::xml_parse_status::status_ok){
|
||||||
|
std::cout << "Loaded paramters from params.xml.\n";
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
std::cout << result.description() << std::endl;
|
||||||
|
throw std::runtime_error("Can not load params.xml.\n");
|
||||||
|
}
|
||||||
|
return parameter_file;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Parameter::Parameter(std::string filename):
|
||||||
|
document(openFile(filename))
|
||||||
|
{}
|
||||||
@@ -0,0 +1,167 @@
|
|||||||
|
#include "PoissonSolver.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
PoissonSolver::PoissonSolver(
|
||||||
|
double target_accuracy, int num_cycles):
|
||||||
|
target_accuracy_(target_accuracy),
|
||||||
|
num_cycles_(num_cycles)
|
||||||
|
{}
|
||||||
|
|
||||||
|
Fields::Potential PoissonSolver::solve(
|
||||||
|
Fields::Density& density,
|
||||||
|
Boundary& rearwall,
|
||||||
|
Boundary& tubewall,
|
||||||
|
Boundary& dps,
|
||||||
|
double radius,
|
||||||
|
double length)
|
||||||
|
{
|
||||||
|
const int nr = density.data_.extent(0);
|
||||||
|
const int nz = density.data_.extent(1);
|
||||||
|
|
||||||
|
const double dz = length/nz;
|
||||||
|
const double dr = radius/nr;
|
||||||
|
|
||||||
|
blitz::Array<double,2> potential_data(nr, nz);
|
||||||
|
potential_data = 0.;
|
||||||
|
|
||||||
|
Fields::Potential potential(potential_data);
|
||||||
|
|
||||||
|
int it = 1;
|
||||||
|
do{
|
||||||
|
iterationPotential(
|
||||||
|
potential, density, rearwall, tubewall, dps, dr, dz);
|
||||||
|
if(it%1000 == 0){
|
||||||
|
if(converged(potential, density, dr, dz)){
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
while(it < num_cycles_);
|
||||||
|
return potential;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PoissonSolver::iterationPotential(
|
||||||
|
Fields::Potential& potential,
|
||||||
|
Fields::Density& density,
|
||||||
|
Boundary& rearwall,
|
||||||
|
Boundary& tubewall,
|
||||||
|
Boundary& dps,
|
||||||
|
double dr,
|
||||||
|
double dz) const
|
||||||
|
{
|
||||||
|
const int nr = density.data_.extent(0);
|
||||||
|
const int nz = density.data_.extent(1);
|
||||||
|
|
||||||
|
// finite diff calculation
|
||||||
|
for(int i = 1; i < nr-1; ++i){
|
||||||
|
for(int k = 1; k<nz-1; ++k){
|
||||||
|
potential.data_(i,k) =
|
||||||
|
(4.*M_PI*density.data_(i,k)
|
||||||
|
+(1.-1./(2*i))*potential.data_(i-1,k)/(dr*dr)
|
||||||
|
+(1.+1./(2*i))*potential.data_(i+1,k)/(dr*dr)
|
||||||
|
+potential.data_(i,k-1)/(dz*dz)
|
||||||
|
+potential.data_(i,k+1)/(dz*dz))/(2/(dr*dr) + 2/(dz*dz));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// r=0
|
||||||
|
for(int k = 1; k<nz-1; ++k){
|
||||||
|
potential.data_(0,k) =
|
||||||
|
(4*M_PI*density.data_(0,k)
|
||||||
|
+4*potential.data_(1,k)/(dr*dr)
|
||||||
|
+potential.data_(0,k-1)/(dz*dz)
|
||||||
|
+potential.data_(0,k+1)/(dz*dz))/(2/(dz*dz) + 4/(dr*dr));
|
||||||
|
}
|
||||||
|
|
||||||
|
// apply boundary condition left
|
||||||
|
if(rearwall.type == BoundaryType::pec){
|
||||||
|
for(int i = 0; i<nr; ++i){
|
||||||
|
potential.data_(i,0) = rearwall.data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (rearwall.type == BoundaryType::pml){
|
||||||
|
for(int i = 0; i<nr; ++i){
|
||||||
|
potential.data_(i,0) = potential.data_(i,1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// apply boundary condition right
|
||||||
|
if(dps.type == BoundaryType::pec){
|
||||||
|
for(int i = 0; i<nr; ++i){
|
||||||
|
potential.data_(i,nz-1) = 0.;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (dps.type == BoundaryType::pml){
|
||||||
|
for(int i = 0; i<nr; ++i){
|
||||||
|
potential.data_(i,nz-1) = potential.data_(i,nz-2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PoissonSolver::converged(
|
||||||
|
Fields::Potential& potential,
|
||||||
|
Fields::Density& density,
|
||||||
|
double dr,
|
||||||
|
double dz) const
|
||||||
|
{
|
||||||
|
const int nr = density.data_.extent(0);
|
||||||
|
const int nz = density.data_.extent(1);
|
||||||
|
|
||||||
|
double norm = 0.;
|
||||||
|
double R = 0.;
|
||||||
|
|
||||||
|
for(int i = 1; i < nr-1; ++i){
|
||||||
|
for(int k = 1; k<nz-1; ++k){
|
||||||
|
double phi =
|
||||||
|
(4.*M_PI*density.data_(i,k)
|
||||||
|
+(1.-1./(2*i))*potential.data_(i-1,k)/(dr*dr)
|
||||||
|
+(1.+1./(2*i))*potential.data_(i+1,k)/(dr*dr)
|
||||||
|
+potential.data_(i,k-1)/(dz*dz)
|
||||||
|
+potential.data_(i,k+1)/(dz*dz))/(2/(dr*dr) + 2/(dz*dz));
|
||||||
|
|
||||||
|
R = -4.*M_PI*density.data_(i,k) - phi;
|
||||||
|
norm += R*R;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
norm = std::sqrt(norm/(nr*nz));
|
||||||
|
std::cout << "Convergence: " << norm << std::endl;
|
||||||
|
return norm < target_accuracy_;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Fields::ElectricField PoissonSolver::solve(
|
||||||
|
Fields::Potential& potential,
|
||||||
|
double radius,
|
||||||
|
double length)
|
||||||
|
{
|
||||||
|
const int nr = potential.data_.extent(0);
|
||||||
|
const int nz = potential.data_.extent(1);
|
||||||
|
|
||||||
|
const double dz = length/nz;
|
||||||
|
const double dr = radius/nr;
|
||||||
|
|
||||||
|
blitz::Array<double,2> efield_z(nr,nz-1);
|
||||||
|
efield_z = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i < nr-1; ++i){
|
||||||
|
for(int j = 0; j < nz-1; ++j){
|
||||||
|
efield_z(i,j) =
|
||||||
|
(potential.data_(i,j) - potential.data_(i,j+1))/dz;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
blitz::Array<double,2> efield_r(nr-1,nz);
|
||||||
|
efield_r = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i < nr-1; ++i){
|
||||||
|
for(int j = 0; j < nz; ++j){
|
||||||
|
efield_r(i,j) =
|
||||||
|
(potential.data_(i,j) - potential.data_(i+1,j))/dr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Fields::ElectricField(efield_r, efield_z);
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include "blitz/array.h"
|
||||||
|
#include "catch/catch.hpp"
|
||||||
|
#include <string>
|
||||||
|
#include "Parameter.h"
|
||||||
|
#include "IO.h"
|
||||||
|
#include "Boundary.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]){
|
||||||
|
|
||||||
|
const std::string parameter_filename = std::string(argv[1]);
|
||||||
|
Parameter parameter(parameter_filename);
|
||||||
|
|
||||||
|
const double radius = IO::radius(parameter);
|
||||||
|
const double length = IO::length(parameter);
|
||||||
|
|
||||||
|
Boundary rearwall = IO::rearwallBoundary(parameter);
|
||||||
|
Boundary tubewall = IO::tubewallBoundary(parameter);
|
||||||
|
Boundary dps = IO::dpsBoundary(parameter);
|
||||||
|
|
||||||
|
Fields::Density density = IO::denstiy(parameter);
|
||||||
|
|
||||||
|
Fields::Density interpolated(Fields::interpolateOnGrid(density));
|
||||||
|
|
||||||
|
PoissonSolver solver = IO::solver(parameter);
|
||||||
|
Fields::Potential potential = solver.solve(
|
||||||
|
interpolated,
|
||||||
|
rearwall,
|
||||||
|
tubewall,
|
||||||
|
dps,
|
||||||
|
radius,
|
||||||
|
length);
|
||||||
|
|
||||||
|
Fields::ElectricField efield = solver.solve(
|
||||||
|
potential,
|
||||||
|
radius,
|
||||||
|
length);
|
||||||
|
|
||||||
|
IO::save(potential, parameter);
|
||||||
|
IO::save(efield, parameter);
|
||||||
|
IO::save(interpolated, parameter);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
#include "catch/catch.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
#include "Fields.h"
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE("Density", "[Density]"){
|
||||||
|
SECTION("Interpolation"){
|
||||||
|
blitz::Array<double,2> data(3,4);
|
||||||
|
data = 0.;
|
||||||
|
data(0,blitz::Range::all()) = 1;
|
||||||
|
data(1,blitz::Range::all()) = 2;
|
||||||
|
data(2,blitz::Range::all()) = 3;
|
||||||
|
|
||||||
|
Fields::Density density_old(data);
|
||||||
|
Fields::Density density_interpol =
|
||||||
|
Fields::interpolateOnGrid(density_old);
|
||||||
|
SECTION("First z value is zero"){
|
||||||
|
REQUIRE(density_interpol.data_(0,0) == 0.);
|
||||||
|
REQUIRE(density_interpol.data_(1,0) == 0.);
|
||||||
|
REQUIRE(density_interpol.data_(2,0) == 0.);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Intermediate values are mean of values"){
|
||||||
|
REQUIRE(density_interpol.data_(1,1) == (1+1+2+2)/4.);
|
||||||
|
REQUIRE(density_interpol.data_(2,1) == (2+2+3+3)/4.);
|
||||||
|
REQUIRE(density_interpol.data_(3,1) == 0.);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Outer values are zero"){
|
||||||
|
REQUIRE(density_interpol.data_(3,0) == 0.);
|
||||||
|
REQUIRE(density_interpol.data_(3,1) == 0.);
|
||||||
|
REQUIRE(density_interpol.data_(3,2) == 0.);
|
||||||
|
REQUIRE(density_interpol.data_(3,3) == 0.);
|
||||||
|
REQUIRE(density_interpol.data_(3,4) == 0.);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Last z values are mean of last density"){
|
||||||
|
REQUIRE(density_interpol.data_(0,4) == 1.);
|
||||||
|
REQUIRE(density_interpol.data_(1,4) == (1+2)/2.);
|
||||||
|
REQUIRE(density_interpol.data_(2,4) == (2+3)/2.);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#define CATCH_CONFIG_MAIN
|
||||||
|
#include "catch/catch.hpp"
|
||||||
|
|
||||||
|
TEST_CASE( "1: All tests cases reside in other .cpp files (empty)", "[multi-file:1]" ){
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user