FLAME  devel
 All Classes Functions Variables Typedefs Enumerations Pages
test_jb_2.cpp
1 #include <iostream>
2 #include <iomanip>
3 #include <fstream>
4 #include <list>
5 #include <ctime>
6 
7 #include <boost/numeric/ublas/io.hpp>
8 
9 #include <flame/constants.h>
10 #include <flame/base.h>
11 #include <flame/moment.h>
12 #include <flame/chg_stripper.h>
13 #include <flame/state/vector.h>
14 #include <flame/state/matrix.h>
15 
16 #include <flame/moment_sup.h>
17 
18 
19 typedef MomentState state_t;
20 typedef state_t::vector_t value_vec;
21 typedef state_t::matrix_t value_mat;
22 
23 extern int glps_debug;
24 
25 
26 static
27 void PrtVec(const MomentState::vector_t &a)
28 {
29  for (size_t k = 0; k < a.size(); k++)
30  std::cout << std::scientific << std::setprecision(10)
31  << std::setw(18) << a[k];
32  std::cout << "\n";
33 }
34 
35 static
36 void PrtMat(const value_mat &M)
37 {
38  for (size_t j = 0; j < M.size1(); j++) {
39  for (size_t k = 0; k < M.size2(); k++)
40  std::cout << std::scientific << std::setprecision(10)
41  << std::setw(18) << M(j, k);
42  std::cout << "\n";
43  }
44 }
45 
46 void PrtState(const state_t& ST)
47 {
48  MomentState::vector_t CenofChg, BeamRMS;
49 
50  if (true)
51  for (size_t k = 0; k < ST.size(); k++) {
52  std::cout << "\nState: "<<k<<" s = "<<std::fixed << std::setprecision(5) << ST.pos << "\n"
53  <<"\n Ref: "<<ST.ref
54  <<"\n Real: "<<ST.real[k]
55  <<"\n moment0\n";
56  PrtVec(ST.moment0[k]);
57  std::cout << " moment1\n";
58  PrtMat(ST.moment1[k]);
59  }
60 
61  std::cout<<"\nCenter: "<< std::scientific << std::setprecision(10)
62  << std::setw(18)<<ST.moment0_env<<"\n";
63  std::cout<<"RMS Beam Size: "<< std::scientific << std::setprecision(10)
64  << std::setw(18)<<ST.moment0_rms<<"\n";
65  PrtMat(ST.moment1_env);
66 }
67 
68 static
69 void prt_initial_cond(Machine &sim,
70  state_t &ST)
71 {
72  sim.propagate(&ST, 0, 1);
73  PrtState(ST);
74 }
75 
76 static
77 void PrtOut(std::ofstream &outf1, std::ofstream &outf2, std::ofstream &outf3, const state_t& ST)
78 {
79  outf1 << std::scientific << std::setprecision(14) << std::setw(22) << ST.pos;
80  for (size_t j = 0; j < ST.size(); j++)
81  for (int k = 0; k < 6; k++)
82  outf1 << std::scientific << std::setprecision(14) << std::setw(22) << ST.moment0[j][k];
83  for (int k = 0; k < 6; k++)
84  outf1 << std::scientific << std::setprecision(14) << std::setw(22) << ST.moment0_env[k];
85  outf1 << "\n";
86 
87  outf2 << std::scientific << std::setprecision(14) << std::setw(22) << ST.pos;
88  for (size_t j = 0; j < ST.size(); j++)
89  for (int k = 0; k < 6; k++)
90  outf2 << std::scientific << std::setprecision(14) << std::setw(22) << sqrt(ST.moment1[j](k, k));
91  for (int k = 0; k < 6; k++)
92  outf2 << std::scientific << std::setprecision(14) << std::setw(22) << sqrt(ST.moment1_env(k, k));
93  outf2 << "\n";
94 
95  outf3 << std::scientific << std::setprecision(14)
96  << std::setw(22) << ST.pos << std::setw(22) << ST.ref.phis << std::setw(22) << ST.ref.IonEk << "\n";
97 }
98 
99 static
100 void propagate(const Config &conf)
101 {
102  // Propagate element-by-element for each charge state.
103  Machine sim(conf);
104  std::auto_ptr<StateBase> state(sim.allocState());
105  state_t *StatePtr = dynamic_cast<state_t*>(state.get());
106  std::ofstream outf1, outf2, outf3;
107 
108  if(!StatePtr) throw std::runtime_error("Only sim_type MomentMatrix is supported");
109 
110  outf1.open("moment0.txt", std::ios::out);
111  outf2.open("moment1.txt", std::ios::out);
112  outf3.open("ref_orbit.txt", std::ios::out);
113 
114  prt_initial_cond(sim, *StatePtr);
115 
116  clock_t tStamp[2];
117 
118  tStamp[0] = clock();
119 
120  prt_initial_cond(sim, *StatePtr);
121 
122  Machine::iterator it = sim.begin()+1;
123  while (it != sim.end()) {
124  ElementVoid* elem = *it;
125 
126  elem->advance(*state);
127  ++it;
128 
129 // PrtOut(outf1, outf2, outf3, *StatePtr);
130 
131 // PrtState(*StatePtr);
132  }
133 
134  outf1.close();
135  outf2.close();
136  outf3.close();
137 
138  tStamp[1] = clock();
139 
140  PrtState(*StatePtr);
141 
142  std::cout << std::fixed << std::setprecision(5)
143  << "\npropagate: " << double(tStamp[1]-tStamp[0])/CLOCKS_PER_SEC << " sec" << "\n";
144 }
145 
146 
147 int main(int argc, char *argv[])
148 {
149  try {
150  std::auto_ptr<Config> conf;
151 
152  if(argc>2)
153 
154  glps_debug = 0; // 0 or 1.
155 
156  try {
157  GLPSParser P;
158  conf.reset(P.parse_file(argc>1 ? argv[1] : NULL));
159  fprintf(stderr, "Parsing succeeds\n");
160  } catch(std::exception& e) {
161  fprintf(stderr, "Parse error: %s\n", e.what());
162  return 1;
163  }
164 
165 // std::cout<<"# Reduced lattice\n";
166 // GLPSPrint(std::cout, *conf);
167 // std::cout<<"\n";
168 
169  registerMoment();
170 
171  propagate(*conf);
172 
173  return 0;
174  } catch(std::exception& e) {
175  std::cerr << "Main exception: " << e.what() << "\n";
177  return 1;
178  }
179 }
void propagate(StateBase *S, size_t start=0, size_t max=-1) const
Pass the given bunch State through this Machine.
Definition: base.cpp:167
p_elements_t::iterator iterator
Beamline element iterator.
Definition: base.h:332
Base class for all simulated elements.
Definition: base.h:166
StateBase * allocState(const Config &c) const
Allocate (with "operator new") an appropriate State object.
Definition: base.cpp:186
The core simulate Machine engine.
Definition: base.h:230
Interface to lattice file parser.
Definition: config.h:240
double IonEk
Kinetic energy.
Definition: moment.h:24
virtual void advance(StateBase &s)=0
Propogate the given State through this Element.
double phis
Absolute synchrotron phase [rad].
Definition: moment.h:24
iterator begin()
Points to the first element.
Definition: base.h:337
Associative configuration container.
Definition: config.h:66
static void registeryCleanup()
Discard all registered State and Element type information.
Definition: base.cpp:242
double pos
absolute longitudinal position at end of Element
Definition: base.h:36
Config * parse_file(const char *fname)
Open and parse a file.
Definition: config.cpp:362
iterator end()
Points just after the last element.
Definition: base.h:342
size_t size() const
of charge states
Definition: moment.h:123