Commit d5bdff2c authored by frey_m's avatar frey_m

Merge AMR fork in OPAL master.

parent ba764f33
......@@ -8,6 +8,7 @@ SET (_EXPR_SRCS
SumErrSqRadialPeak.cpp
Parser/expression.cpp
Parser/evaluator.cpp
SeptumExpr.h
)
add_sources(${_EXPR_SRCS})
......
//
// Struct SeptumExpr
// Objective to obtain a nice septum in cyclotron simulations.
//
// Copyright (c) 2019, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// Implemented as part of the PhD thesis
// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef __SEPTUM_EXPRESSION_H__
#define __SEPTUM_EXPRESSION_H__
#include <string>
#include <iostream>
#include "boost/variant/get.hpp"
#include "boost/variant/variant.hpp"
#include "boost/smart_ptr.hpp"
#include "Util/Types.h"
#include "Util/PeakReader.h"
#include "Expression/Parser/function.hpp"
#include "Util/ProbeHistReader.h"
struct SeptumExpr {
static const std::string name;
Expressions::Result_t operator()(client::function::arguments_t args) {
if (args.size() != 1) {
throw OptPilotException("SeptumExpr::operator()",
"SeptumExpr expects 1 arguments, " + std::to_string(args.size()) + " given");
}
std::string probe = boost::get<std::string>(args[0]);
bool is_valid = true;
double result = 0.0;
try {
boost::scoped_ptr<PeakReader> sim_peaks(new PeakReader(probe + std::string(".peaks")));
sim_peaks->parseFile();
boost::scoped_ptr<ProbeHistReader> sim_hist(new ProbeHistReader(probe + std::string(".hist")));
sim_hist->parseFile();
double upperBound = 0.0;
double lowerBound = 0.0;
size_t nTurns = sim_peaks->getNumberOfPeaks();
sim_peaks->getPeak(nTurns, upperBound);
sim_peaks->getPeak(nTurns - 1, lowerBound);
result = sim_hist->minimum(lowerBound, upperBound);
} catch (OptPilotException &ex) {
std::cout << "Exception while getting septum value "
<< ex.what()
<< std::endl;
is_valid = false;
}
return boost::make_tuple(result, is_valid);
}
};
#endif
......@@ -5,6 +5,7 @@ SET (_UTIL_SRCS
PeakReader.cpp
ProbeReader.cpp
ProbeHistReader.cpp
SDDSParser.cpp
SDDSParser/array.cpp
SDDSParser/associate.cpp
......
//
// Class ProbeHistReader
// Implements a parser and value extractor for hist files (*.hist).
// It is for example used together with the septum objective.
// A histogram file is generated by the OPAL probe element.
//
// Copyright (c) 2019, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// Implemented as part of the PhD thesis
// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#include <iterator>
#include <regex>
#include "ProbeHistReader.h"
#include "Util/OptPilotException.h"
ProbeHistReader::ProbeHistReader(std::string filename)
: filename_m(filename)
, rmin_m(0.0)
, binwidth_m(0.0)
, bincount_m(0)
{ }
void ProbeHistReader::parseFile() {
std::ifstream histfile;
histfile.open(filename_m.c_str(), std::ios::in);
if (!histfile) {
throw OptPilotException("ProbeHistReader::parseFile()",
"Error opening file " + filename_m);
}
parseHeader(histfile);
std::istream_iterator<size_t> it(histfile);
while ( it != std::istream_iterator<size_t>() ) {
bincount_m.push_back(*it);
++it;
}
histfile.close();
if (histfile.is_open()) {
throw OptPilotException("ProbeHistReader::parseFile()",
"Error closing file " + filename_m);
}
}
size_t ProbeHistReader::minimum(double lower, double upper) {
size_t lidx = (lower - rmin_m) / binwidth_m;
size_t uidx = (upper - rmin_m) / binwidth_m;
if (lidx >= uidx) {
throw OptPilotException("ProbeHistReader::minimum()",
"Lower index >= upper index: " + std::to_string(lidx) +
" >= " + std::to_string(uidx));
}
if (uidx >= bincount_m.size()) {
throw OptPilotException("ProbeHistReader::minimum()",
"Index >= number of bins: " + std::to_string(uidx) +
" >= " + std::to_string(bincount_m.size()));
}
container_t::iterator beg = std::begin(bincount_m);
std::advance(beg, lidx);
container_t::iterator end = std::begin(bincount_m);
std::advance(end, uidx);
container_t::iterator result = std::min_element(beg, end);
if (result == bincount_m.end()) {
throw OptPilotException("ProbeHistReader::minimum()",
"No minimum between " + std::to_string(lower) +
" and " + std::to_string(upper) + " found.");
}
return double(*result);
}
void ProbeHistReader::parseHeader(std::ifstream& ifs) {
std::string header;
std::getline(ifs, header);
if (header.find("# Histogram bin counts") == std::string::npos) {
throw OptPilotException("ProbeHistReader::parseHeader()",
"Error reading file " + filename_m);
}
const std::regex re("\\.*\\) (.*) mm (.*) mm (.*) (.*) mm");
std::smatch match;
std::regex_search(header, match, re);
if ( match.size() != 5 ) {
throw OptPilotException("ProbeHistReader::parseHeader()",
"Error parsing header of file " + filename_m);
}
rmin_m = getValue<double>(match[1].str());
double rmax = getValue<double>(match[2].str());
size_t nbins = getValue<size_t>(match[3].str());
binwidth_m = getValue<double>(match[4].str());
if ( rmin_m >= rmax ) {
throw OptPilotException("ProbeHistReader::parseHeader()",
"Not a valid histogram file " + filename_m);
}
bincount_m.reserve(nbins);
}
//
// Class ProbeHistReader
// Implements a parser and value extractor for hist files (*.hist).
// It is for example used together with the septum objective.
// A histogram file is generated by the OPAL probe element.
//
// Copyright (c) 2019, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// Implemented as part of the PhD thesis
// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef __PROBE_HIST_READER_H__
#define __PROBE_HIST_READER_H__
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
class ProbeHistReader {
public:
typedef std::vector<size_t> container_t;
ProbeHistReader(std::string filename);
void parseFile();
/**
* @param lower radius [mm]
* @param upper radius [mm]
*/
size_t minimum(double lower, double upper);
private:
void parseHeader(std::ifstream& ifs);
template <typename T>
T getValue(const std::string& s);
/// Histogram file
std::string filename_m;
double rmin_m; // start radius of probe in mm
double binwidth_m; // size of each bin in mm
container_t bincount_m;
};
template <typename T>
T ProbeHistReader::getValue(const std::string& s) {
std::istringstream ss(s);
T res;
ss >> res;
return res;
}
#endif
//
// Class AbstractAmrWriter
// Abstract base class for writing AMR data to output files.
//
// Copyright (c) 2016 - 2020, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// Implemented as part of the PhD thesis
// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef ABSTRACT_AMR_WRITER_H
#define ABSTRACT_AMR_WRITER_H
......@@ -5,10 +25,6 @@
#include "Amr/AmrDefs.h"
#include "Algorithms/AmrPartBunch.h"
/*!
* Abstract base class for writing AMR data to files in
* order to plot them.
*/
class AbstractAmrWriter {
public:
......
This diff is collapsed.
//
// Class AmrBoxLib
// Concrete AMR object. It is based on the AMReX library
// (cf. https://amrex-codes.github.io/ or https://ccse.lbl.gov/AMReX/).
// AMReX is the successor of BoxLib. This class represents the interface
// to AMReX and the AMR framework in OPAL. It implements the functions of
// the AmrObject class.
//
// Copyright (c) 2016 - 2020, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// Implemented as part of the PhD thesis
// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef AMR_BOXLIB_H
#define AMR_BOXLIB_H
......@@ -9,12 +33,6 @@ class AmrPartBunch;
#include <AMReX_AmrMesh.H>
#include <AMReX.H>
/*!
* Concrete AMR object. It is based on the
* <a href="https://ccse.lbl.gov/AMReX/">AMReX</a> library
* developed at LBNL. This library is the successor of
* <a href="https://ccse.lbl.gov/BoxLib/">BoxLib</a>.
*/
class AmrBoxLib : public AmrObject,
public amrex::AmrMesh
{
......@@ -200,6 +218,14 @@ private:
*/
void doRegrid_m(int lbase, double time);
/*!
* Called within doRegrid_m(). Especially used
* for potential and e-field tagging in combination
* with binning.
*/
void preRegrid_m();
/*!
* Called within doRegrid_m(). Redistribute
* particles and delete levels on particle side.
......@@ -207,6 +233,9 @@ private:
*/
void postRegrid_m(int old_finest);
double solvePoisson_m();
/* ATTENTION
* The tagging routines assume the particles to be in the
......
//
// File AmrDefs
// AMR namespace with typedefs of AMReX classes.
//
// Copyright (c) 2016 - 2020, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// Implemented as part of the PhD thesis
// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef AMR_DEFS_H
#define AMR_DEFS_H
......
//
// Class AmrObject
// The AMR interface to OPAL. A new AMR library needs
// to inherit from this class in order to work properly
// with OPAL. Among other things it specifies the refinement
// strategies.
//
// Copyright (c) 2016 - 2020, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// Implemented as part of the PhD thesis
// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#include "AmrObject.h"
AmrObject::AmrObject()
......
//
// Class AmrObject
// The AMR interface to OPAL. A new AMR library needs
// to inherit from this class in order to work properly
// with OPAL. Among other things it specifies the refinement
// strategies.
//
// Copyright (c) 2016 - 2020, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// Implemented as part of the PhD thesis
// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef AMR_OBECT_H
#define AMR_OBECT_H
......@@ -5,16 +28,6 @@
#include "Algorithms/PBunchDefs.h"
// #include "Algorithms/AmrPartBunch.h"
// class AmrPartBunch;
/**
* The AMR interface to OPAL. A new AMR library needs
* to inherit from this class in order to work properly
* with OPAL. Among other things it specifies the refinement
* strategies.
*/
class AmrObject {
public:
......@@ -147,7 +160,7 @@ public:
* Rebalance the grids among the
* cores
*/
virtual void redistributeGrids(int how) { }
virtual void redistributeGrids(int /*how*/) { }
/*!
* Used in AmrPartBunch to check if we need to refine
......
//
// Class AmrYtWriter
// This concrete class writes output files that are readable by yt
// (cf. http://yt-project.org/). We have a fork of yt in
// the repository at https://gitlab.psi.ch/frey_m/yt.
// The functions of this class are copied from AMReX and modified to fit
// our needs.
//
// Copyright (c) 2016 - 2020, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// Implemented as part of the PhD thesis
// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#include "AmrYtWriter.h"
#include <AMReX_Utility.H>
......@@ -271,7 +295,7 @@ void AmrYtWriter::writeFields(const amr::AmrScalarFieldContainer_t& rho,
void AmrYtWriter::writeBunch(const AmrPartBunch* bunch_p,
const double& time,
const double& /*time*/,
const double& gamma)
{
/* According to
......
//
// Class AmrYtWriter
// This concrete class writes output files that are readable by yt
// (cf. http://yt-project.org/). We have a fork of yt in
// the repository at https://gitlab.psi.ch/frey_m/yt.
// The functions of this class are copied from AMReX and modified to fit
// our needs.
//
// Copyright (c) 2016 - 2020, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// Implemented as part of the PhD thesis
// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef AMR_YT_WRITER_H
#define AMR_YT_WRITER_H
......@@ -7,16 +31,6 @@
#include <vector>
/*!
* This concrete class writes output files
* that are readable by
* <a href="http://yt-project.org/">yt</a>.
* The format can be read by the fork
* <a href="https://Yavin_4@bitbucket.org/Yavin_4/opal-yt">opal-yt</a>
* that uses accelerator units instead of astrophysics units.
* The functions are copied from AMReX and modified to fit
* our needs.
*/
class AmrYtWriter : public AbstractAmrWriter {
public:
......
//
// Class BoxLibLayout
// In contrast to AMReX, OPAL is optimized for the
// distribution of particles to cores. In AMReX the ParGDB object
// is responsible for the particle to core distribution. This
// layout is derived from this object and does all important
// bunch updates. It is the interface for AMReX and Ippl.
//
// In AMReX, the geometry, i.e. physical domain, is fixed
// during the whole computation. Particles leaving the domain
// would be deleted. In order to prevent this we map the particles
// onto the domain [-1, 1]^3. Furthermore, it makes sure
// that we have enougth grid points to represent the bunch
// when its charges are scattered on the grid for the self-field
// computation.
//
// The self-field computation and the particle-to-core update
// are performed in the particle mapped domain.
//
// Copyright (c) 2016 - 2020, Matthias Frey, Uldis Locans, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// Implemented as part of the PhD thesis
// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef BOXLIB_LAYOUT_H
#define BOXLIB_LAYOUT_H
......@@ -8,24 +43,6 @@
#include <AMReX_ParGDB.H>
/*!
* In contrast to AMReX, OPAL is optimized for
* distribution particles to cores. In AMReX the ParGDB object
* is responsible for the particle to core distribution. This
* layout is derived from this object and does all important
* bunch updates. It is the interface for AMReX and Ippl.
*
* In AMReX the geometry, i.e. physical domain, is fixed
* during the whole computation. Particles leaving the domain
* would be deleted. In order to prevent this we map the particles
* onto the domain \f$[-1, 1]^3\f$. Furthermore, it makes sure
* that we have enougth grid points to represent the bunch
* when its charges are scattered on the grid for the self-field
* computation.
*
* The self-field computation and the particle-to-core update
* are performed in the particle mapped domain.
*/
template<class T, unsigned Dim>
class BoxLibLayout : public ParticleAmrLayout<T, Dim>,
public amrex::ParGDB
......
//
// Class BoxLibLayout
// In contrast to AMReX, OPAL is optimized for the
// distribution of particles to cores. In AMReX the ParGDB object
// is responsible for the particle to core distribution. This
// layout is derived from this object and does all important
// bunch updates. It is the interface for AMReX and Ippl.
//
// In AMReX, the geometry, i.e. physical domain, is fixed
// during the whole computation. Particles leaving the domain
// would be deleted. In order to prevent this we map the particles
// onto the domain [-1, 1]^3. Furthermore, it makes sure
// that we have enougth grid points to represent the bunch
// when its charges are scattered on the grid for the self-field
// computation.
//
// The self-field computation and the particle-to-core update
// are performed in the particle mapped domain.
//
// Copyright (c) 2016 - 2020, Matthias Frey, Uldis Locans, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// Implemented as part of the PhD thesis
// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef BoxLibLayout_HPP
#define BoxLibLayout_HPP
......@@ -128,8 +163,8 @@ void BoxLibLayout<T, Dim>::setDomainRatio(const std::vector<double>& ratio) {
template<class T, unsigned Dim>
void BoxLibLayout<T, Dim>::update(IpplParticleBase< BoxLibLayout<T,Dim> >& PData,
const ParticleAttrib<char>* canSwap)
void BoxLibLayout<T, Dim>::update(IpplParticleBase< BoxLibLayout<T,Dim> >& /*PData*/,
const ParticleAttrib<char>* /*canSwap*/)
{
/* Exit since we need AmrParticleBase with grids and levels for particles for this layout