Commit 53db3b12 authored by TOUZE Francois's avatar TOUZE Francois
Browse files

docker containers creation: step 2

parent 58fd6992
......@@ -2,5 +2,5 @@
how to wrap C++ with Cython.
Assuming you have Python, Cython, Git and a C++ compiler installed
$ python setup.py build_ext --inplace
$ python pspaclass.py build_ext --inplace
#include <iostream>
#include "cppcode.h"
#include "pspaclass.h"
#include "componentFactory.h"
......
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
xtension = Extension(
name="pypspaclass",
sources=["pypspaclass.pyx",
"/home/pspa/backend/pspa/v0/src/Core/dataManager.cpp"],
libraries=["pspa","uap","antlr","xerces-c"],
include_dirs=["/home/pspa/backend/pspa/v0/src/Core",
"/home/pspa/backend/pspa/v0/src/KindOfElements",
"/usr/local/accelerator-ml-code",
"/usr/local/json/include"],
library_dirs=["/home/pspa/backend/pspa/v0/Linux-x86_64",
"/usr/local/accelerator-ml-code/lib",
"/usr/local/lib"]
)
setup(
name="pypspaclass",
ext_modules=cythonize([xtension])
)
#include <iostream>
#include <fstream>
#include "pspaplot.h"
// Default constructor
pspaplot::pspaplot() {}
// Overloaded constructor
pspaplot::pspaplot(string basename)
{
userDir_= basename;
}
// Destructor
pspaplot::~pspaplot() {}
void pspaplot::settings(string str)
{
auto j= json::parse( str );
string item= j.find( "plotItem" ).value();
//cout << "item choisi => " << item << endl;
json obj;
if (item == "ellipse$START") {
obj= phaseSpace( j,"start" );
} else if (item == "ellipse$END") {
obj= phaseSpace( j,"end" );
} else {
obj= plotSelection( j ); //beamParameters( j );
}
string fname= userDir_+"simulator-plot.txt";
ofstream outfile;
outfile.open(fname.c_str(),ios::out);
outfile << std::setw(4) << obj << std::endl;
outfile.close();
}
json pspaplot::phaseSpace(json j, string opt)
{
auto ids= j.find( "plotSimulation" );
//std::cout << std::boolalpha;
//cout << "ids is_array? " << ids->is_array() << "\n";
//cout << "ids size? " << ids->size() << "\n";
json::iterator it= ids->begin();
string fname= *it;
fname= userDir_ + fname + "-plot.txt";
//cout << "open fileName => " << fname << endl;
std::ifstream ifs( fname );
if (!ifs.good()) {
cout << "error during the opening of the file " << fname << endl;
return json({});
}
json obj;
ifs >> obj;
ifs.close();
// check if the horizontal r.m.s table exists
it= obj.find("xsigma");
if (it == obj.end()) {
cout << "select the Beam parameters option in Twiss commands\n";
return json({});
}
// displays the horizontal (xx') ellipse at the beginning/end of the beam line
vector<double> Xaxis, Yaxis;
double zcoor, xrms, alfa, beta;
if (opt == "start") {
zcoor= obj.find( "zcoor" )->front();
xrms= obj.find("xsigma")->front();
alfa= obj.find("xalfa")->front();
beta= obj.find("xbeta")->front();
} else if (opt == "end") {
zcoor= obj.find( "zcoor" )->back();
xrms= obj.find("xsigma")->back();
alfa= obj.find("xalfa")->back();
beta= obj.find("xbeta")->back();
} else {
cout << "option error in the ellipse plot => " << opt << endl;
return json({});
}
xrms *= 0.001;
int n= 50;
double h= 2.0*xrms/n;
int m= 10;
for (int j = 0; j < m; ++j)
{
double xk= -xrms + j*h/m;
Xaxis.push_back( xk );
double aux= xrms*xrms - xk*xk;
double yk= -alfa*xk + sqrt(fabs(aux));
yk /= beta;
Yaxis.push_back( yk );
}
for (int k = 1; k < n-1; ++k)
{
double xk= -xrms + k*h;
Xaxis.push_back( xk );
double aux= xrms*xrms - xk*xk;
double yk= -alfa*xk + sqrt(abs(aux));
yk /= beta;
Yaxis.push_back( yk );
}
for (int j = 0; j < m; ++j)
{
double xk= -xrms + (n-1)*h + j*h/m;
Xaxis.push_back( xk );
double aux= xrms*xrms - xk*xk;
double yk= -alfa*xk + sqrt(fabs(aux));
yk /= beta;
Yaxis.push_back( yk );
}
for (int j = 0; j < m; ++j)
{
double xk= xrms - j*h/m;
Xaxis.push_back( xk );
double aux= xrms*xrms - xk*xk;
double yk= -alfa*xk - sqrt(fabs(aux));
yk /= beta;
Yaxis.push_back( yk );
}
for (int k = 1; k < n-1; ++k)
{
double xk= xrms - k*h;
Xaxis.push_back( xk );
double aux= xrms*xrms - xk*xk;
double yk= -alfa*xk - sqrt(abs(aux));
yk /= beta;
Yaxis.push_back( yk );
}
for (int j = 0; j < m+1; ++j)
{
double xk= xrms - (n-1)*h - j*h/m;
Xaxis.push_back( xk );
double aux= xrms*xrms - xk*xk;
double yk= -alfa*xk - sqrt(fabs(aux));
yk /= beta;
Yaxis.push_back( yk );
}
json jx;
jx["type"]= "line";
json x;
x["label"]= "xx'";
x["backgroundColor"]= "#FFFFFF";
x["borderColor"]= "#74B9FF";
x["fill"]= false;
x["pointRadius"]= 0;
for(unsigned int k = 0; k < Xaxis.size(); ++k)
{
json xy= {{"x",Xaxis.at(k)},{"y",Yaxis.at(k)}};
x["data"].push_back( xy );
}
jx["data"]["datasets"].push_back( x );
std::ostringstream ostr;
ostr << zcoor;
string title= "Phase Space Ellipse (z=" + ostr.str() +"m)";
jx["options"]["title"]["display"]= true;
jx["options"]["title"]["text"]= title;
jx["options"]["title"]["fontSize"]= 24;
jx["options"]["title"]["fontStyle"]= "normal";
x.clear();
x["type"]= "linear";
x["scaleLabel"]["display"]= true;
jx["options"]["scales"]["xAxes"].push_back( x );
x.clear();
x["type"]= "linear";
x["position"]= "left";
x["display"]= true;
x["ticks"]["beginAtZero"]= true;
jx["options"]["scales"]["yAxes"].push_back( x );
x.clear();
return jx;
}
json pspaplot::plotSelection(json j)
{
auto ids= j.find( "plotSimulation" );
string item= j.find( "plotItem" ).value();
std::cout << std::boolalpha;
//cout << "ids is_array? " << ids->is_array() << "\n";
//cout << "ids size? " << ids->size() << "\n";
vector<double> Xaxis;
map<string,vector<double> > Ymaps;
double shift= 0.0;
for (json::iterator it = ids->begin(); it != ids->end(); ++it)
{
string fname= *it;
fname= userDir_ + fname + "-plot.txt";
cout << "plotSelection:: OPEN fileName => " << fname << endl;
std::ifstream ifs( fname );
if (!ifs.good()) {
cout << "plotSelection:: ERROR opening the file " << fname << endl;
continue;
}
json obj;
ifs >> obj;
ifs.close();
vector<double> xaxis = menu1(item,obj);
if (xaxis.empty()) continue;
map<string,vector<double> > ymap = menu2(item,obj);
if (ymap.empty()) continue;
for (auto& y: ymap) {
for(int k = 0; k < y.second.size(); ++k) {
Ymaps[ y.first ].push_back( y.second.at(k) );
}
}
for (auto& x: xaxis) {
Xaxis.push_back( x+shift );
}
// pipeline: available for track and twiss
shift= xaxis.back();
string softw= obj.find( "software" ).value();
cout << "plotSelection:: software => " << softw << endl;
string action= obj.find( "action" ).value();
cout << "plotSelection:: action => " << action << endl;
if (action == "emitt") break;
if (action == "tunes") break;
if (action == "matching") break;
}
json jx;
if (Xaxis.empty() || Ymaps.empty()) {
cout << "pspaplot::bad plot item... check your choice\n";
jx= json({});
} else {
jx= lineplots(Xaxis,Ymaps);
}
return jx;
}
vector<double> pspaplot::menu1(string item,json obj)
{
string key;
if (item == "tunesVSdelta" ||
item == "betaVSdelta" ||
item == "alfaVSdelta") {
key= "deltap";
} else if (item == "tunesVSx") {
key= "Ax";
} else if (item == "tunesVSy") {
key= "Ay";
} else {
key= "zcoor";
}
vector<double> myvector;
json::iterator kt= obj.find(key);
if (kt != obj.end()) {
for (auto& el: *kt) {
myvector.push_back( el );
}
}
return myvector;
}
map<string,vector<double> > pspaplot::menu2(string item,json obj)
{
map<string,vector<double> > mymap;
if (item == "rms-emittance") { // valid only for track
mymap = {
{"xemit",vector<double>()},
{"yemit",vector<double>()} };
} else if (item == "beam-size") {
mymap = {
{"xsigma",vector<double>()},
{"ysigma",vector<double>()} };
} else if (item == "bunch-length") { // valid only for track
mymap = {
{"zsigma",vector<double>()} };
} else if (item == "energy-spread") { // valid only for track
mymap = {
{"spread",vector<double>()} };
} else if (item == "Hx-function") { // valid only for twiss
mymap = {
{"xHinv",vector<double>()} };
} else if (item == "betas") {
mymap = {
{"xbeta",vector<double>()},
{"ybeta",vector<double>()} };
} else if (item == "betas&xeta") {
mymap = {
{"xbeta",vector<double>()},
{"ybeta",vector<double>()},
{"xeta", vector<double>()} };
} else if (item == "alfas") {
mymap = {
{"xalfa",vector<double>()},
{"yalfa",vector<double>()} };
} else if (item == "phases") { // valid only for twiss
mymap = {
{"xmu",vector<double>()},
{"ymu",vector<double>()} };
} else if (item == "chromatics") { // valid only for twiss
mymap = {
{"wx",vector<double>()},
{"wy",vector<double>()} };
} else if (item == "tunesVSdelta") { // valid only for tunes
mymap = {
{"qx",vector<double>()},
{"qy",vector<double>()} };
} else if (item == "betaVSdelta") { // valid only for tunes
mymap = {
{"bx",vector<double>()},
{"by",vector<double>()} };
} else if (item == "alfaVSdelta") { // valid only for tunes
mymap = {
{"ax",vector<double>()},
{"ay",vector<double>()} };
} else if (item == "tunesVSx") { // valid only for tunes
mymap = {
{"q1x",vector<double>()},
{"q2x",vector<double>()} };
} else if (item == "tunesVSy") { // valid only for tunes
mymap = {
{"q1y",vector<double>()},
{"q2y",vector<double>()} };
} else {
return mymap;
}
for (auto it = mymap.begin(); it != mymap.end(); ) {
json::iterator kt= obj.find( it->first );
if (kt == obj.end()) {
it = mymap.erase( it );
} else {
for (auto& el: *kt) {
it->second.push_back( el );
}
++it;
}
}
return mymap;
}
json pspaplot::beamParameters(json j)
{
auto ids= j.find( "plotSimulation" );
string item= j.find( "plotItem" ).value();
std::cout << std::boolalpha;
//cout << "ids is_array? " << ids->is_array() << "\n";
//cout << "ids size? " << ids->size() << "\n";
vector<double> Xaxis;
map<string,vector<double> > Ymaps;
double xshift= 0.0;
for (json::iterator it = ids->begin(); it != ids->end(); ++it)
{
string fname= *it;
fname= userDir_ + fname + "-plot.txt";
cout << "open fileName => " << fname << endl;
std::ifstream ifs( fname );
if (!ifs.good()) {
cout << "error during the opening of the file " << fname << endl;
continue;
}
json obj;
ifs >> obj;
ifs.close();
submenu1(item,obj,xshift,Xaxis);
submenu2(item,obj,Ymaps);
string softw= obj.find( "software" ).value();
//cout << "softw => " << softw << endl;
string action= obj.find( "action" ).value();
cout << "pspaplot::beamParameters: action => " << action << endl;
if (action == "tunes") break;
}
json jx;
if (Ymaps.empty()) {
cout << "pspaplot::bad plot item... check your choice\n";
jx= json({});
} else {
jx= lineplots(Xaxis,Ymaps);
}
return jx;
}
void pspaplot::submenu1(string key,json obj,double& xshift,vector<double>& Xaxis)
{
json::iterator xaxis= obj.end();
if (key == "tunesVSdelta") {
xaxis= obj.find( "deltap" );
} else if (key == "betaVSdelta") {
xaxis= obj.find( "deltap" );
} else if (key == "alfaVSdelta") {
xaxis= obj.find( "deltap" );
} else if (key == "tunesVSx") {
xaxis= obj.find( "Ax" );
} else if (key == "tunesVSy") {
xaxis= obj.find( "Ay" );
} else {
xaxis= obj.find( "zcoor" );
}
if (xaxis == obj.end()) return;
for(unsigned int i = 0; i < xaxis->size(); ++i)
{
double zcoor= xaxis->at( i );
zcoor+= xshift;
Xaxis.push_back( zcoor );
}
xshift= xaxis->back();
}
void pspaplot::submenu2(string key,json obj,map<string,vector<double> >& Ymaps)
{
if (key == "rms-emittance") { // valid only for track
menu("xemit",obj,Ymaps);
menu("yemit",obj,Ymaps);
}
if (key == "beam-size") {
menu("xsigma",obj,Ymaps);
menu("ysigma",obj,Ymaps);
}
if (key == "bunch-length") { // valid only for track
menu("zsigma",obj,Ymaps);
}
if (key == "energy-spread") { // valid only for track
menu("spread",obj,Ymaps);
}
if (key == "Hx-function") { // valid only for twiss
menu("xHinv",obj,Ymaps);
}
if (key == "betas") {
menu("xbeta",obj,Ymaps);
menu("ybeta",obj,Ymaps);
}
if (key == "betas&xeta") {
menu("xbeta",obj,Ymaps);
menu("ybeta",obj,Ymaps);
menu("xeta",obj,Ymaps);
}
if (key == "alfas") {
menu("xalfa",obj,Ymaps);
menu("yalfa",obj,Ymaps);
}
if (key == "phases") { // valid only for twiss
menu("xmu",obj,Ymaps);
menu("ymu",obj,Ymaps);
}
if (key == "chromatics") { // valid only for twiss
menu("wx",obj,Ymaps);
menu("wy",obj,Ymaps);
menu("xetap",obj,Ymaps);
}
if (key == "tunesVSdelta") { // valid only for tunes
menu("qx",obj,Ymaps);
menu("qy",obj,Ymaps);
}
if (key == "betaVSdelta") { // valid only for tunes
menu("bx",obj,Ymaps);
menu("by",obj,Ymaps);
}
if (key == "alfaVSdelta") { // valid only for tunes
menu("ax",obj,Ymaps);
menu("ay",obj,Ymaps);
}
if (key == "tunesVSx") { // valid only for tunes
menu("q1x",obj,Ymaps);
menu("q2x",obj,Ymaps);
}
if (key == "tunesVSy") { // valid only for tunes
menu("q1y",obj,Ymaps);
menu("q2y",obj,Ymaps);
}
}
void pspaplot::menu(string key,json obj,map<string,vector<double> >& Ymaps)
{
auto it= obj.find( key );
if (it == obj.end()) return;
for(unsigned int i = 0; i < it->size(); ++i)
{
Ymaps[ key ].push_back( it->at( i ) );
}
}
json pspaplot::lineplots(vector<double> Xaxis,map<string,vector<double> > Ymaps)
{
json j;
j["type"]= "line";
json x;
string title;
string leftLabel;
string xcoor= "z [m]";
///// First dataset (on left y-axis) /////
map<string,vector<double> >::iterator it;
if (Ymaps.find("xemit") != Ymaps.end()) {
x["label"]= "εx [π.mrad.mm]";
it= Ymaps.find("xemit");
title= "Transverse Emittance";
leftLabel= "εx";
}
if (Ymaps.find("zsigma") != Ymaps.end()) {
x["label"]= "σz [mm]";
it= Ymaps.find("zsigma");
title= "Bunch Length";
leftLabel= "σz";
}
if (Ymaps.find("spread") != Ymaps.end()) {
x["label"]= "ΔE/E [%]";
it= Ymaps.find("spread");
title= "Energy Spread";
leftLabel= "ΔE/E";
}