Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bfield_finemesh.cc
Go to the documentation of this file.
1 // Author: Simon Taylor
2 //
3 //
4 // bfield_finemesh.cc
5 //
6 // Program to create a fine-mesh map of the magnetic field (including
7 // gradients) in the form of an evio file using the coarse-mesh map computed
8 // by TOSCA/Ansys/.. as input.
9 
10 #include "DANA/DApplication.h"
11 using namespace std;
12 
13 #include <vector>
15 #include <evioFileChannel.hxx>
16 #include <evioUtil.hxx>
17 using namespace evio;
18 
19 void Usage(void);
20 
21 //-----------
22 // main
23 //-----------
24 int main(int narg, char *argv[])
25 {
26  if(narg<=1)Usage();
27 
28  double rmax=100.;
29  double rmin=0.;
30  double zmax=600.;
31  double zmin=0.;
32  double dr=0.1;
33  double dz=0.1;
34  string evioFileName="finemesh.evio";
35 
36  // parse command line arguments
37  for(int i=1; i<narg; i++){
38  string arg(argv[i]);
39  string next(i<narg-1 ? argv[i+1]:"");
40  float argf = atof(next.c_str());
41  bool used_next = false;
42  if(arg=="-zmin"){used_next=true; zmin = argf;}
43  if(arg=="-zmax"){used_next=true; zmax = argf;}
44  if(arg=="-rmin"){used_next=true; rmin = argf;}
45  if(arg=="-rmax"){used_next=true; rmax = argf;}
46  if(arg=="-dr"){used_next=true;dr=argf;}
47  if(arg=="-dz"){used_next=true;dz=argf;}
48  if(arg=="-file"){evioFileName=next;};
49  if(used_next){
50  // skip to next argument
51  i++;
52 
53  //If i is now > than narg, then no value was passed to an "-XXX" argument
54  if(i>narg){
55  _DBG_<<"No argument given for \""<<arg<<"\" argument!"<<endl;
56  Usage();
57  exit(-1);
58  }
59  }
60  }
61  cout << "Generation fine-mesh B-field map with the following parameters:"
62  <<endl;
63  cout << " rmin = " << rmin <<endl;
64  cout << " rmax = " << rmax << endl;
65  cout << " dr = " << dr <<endl;
66  cout << " zmin = " << zmin << endl;
67  cout << " zmax = " << zmax << endl;
68  cout << " dz = " << dz <<endl;
69 
70  // Instantiate an event loop object
71  DApplication app(narg, argv);
72 
73  // Initialize
74  app.Init();
75 
76  // Read the Ansys/TOSCA/Poisson map from CalibDB
77  DMagneticFieldMap *bfield = app.GetBfield();
78 
79  // Determine the number of points in x and z from the input parameters
80  unsigned int Nr=(unsigned int)floor((rmax-rmin)/dr+0.5);
81  unsigned int Nz=(unsigned int)floor((zmax-zmin)/dz+0.5);
82 
83  // Create the finer-mesh map
84  vector<float>Br_;
85  vector<float>Bz_;
86  vector<float>dBrdr_;
87  vector<float>dBrdz_;
88  vector<float>dBzdr_;
89  vector<float>dBzdz_;
90  for (unsigned int i=0;i<Nr;i++){
91  double r=rmin+dr*double(i);
92  for (unsigned int j=0;j<Nz;j++){
93  double z=zmin+dz*double(j);
94  double Bx,By,Bz;
95  double dBxdx,dBxdy,dBxdz,dBydx,dBydy,dBydz,dBzdx,dBzdy,dBzdz;
96 
97  bfield->GetFieldAndGradient(r,0.,z,Bx,By,Bz,dBxdx,dBxdy,dBxdz,
98  dBydx,dBydy,dBydz,dBzdx,dBzdy,dBzdz);
99 
100  Br_.push_back(Bx);
101  Bz_.push_back(Bz);
102  dBrdr_.push_back(dBxdx);
103  dBrdz_.push_back(dBxdz);
104  dBzdr_.push_back(dBzdx);
105  dBzdz_.push_back(dBzdz);
106  }
107  }
108 
109  // Open the evio file channel
110  unsigned long bufsize=Nr*Nz*6*sizeof(float)+6;
111  evioFileChannel chan(evioFileName,"w",bufsize);
112  chan.open();
113 
114  // create an event tree, root node has (tag=1,num=0)
115  evioDOMTree tree(1,0);
116 
117  float minmaxdelta[6]={rmin,rmax,dr,zmin,zmax,dz};
118  tree.addBank(2,0,minmaxdelta,6);
119 
120  // Add the banks
121  tree.addBank(3,0,Br_);
122  tree.addBank(3,1,Bz_);
123  tree.addBank(3,2,dBrdr_);
124  tree.addBank(3,3,dBrdz_);
125  tree.addBank(3,4,dBzdr_);
126  tree.addBank(3,5,dBzdz_);
127 
128  chan.write(tree);
129  chan.close();
130 
131  return 0;
132 }
133 
134 //-----------
135 // Usage
136 //-----------
137 void Usage(void)
138 {
139  cout<<endl;
140  cout<<"Usage:"<<endl;
141  cout<<" bfield_finemesh -rmin [rmin] -rmax [rmax] -dr [dr] -zmin [zmin] -zmax [zmax] -dz [dz] [-file filename]"<<endl;
142  cout<<endl;
143 
144  exit(0);
145 }
146 
double rmax
virtual void GetFieldAndGradient(double x, double y, double z, double &Bx, double &By, double &Bz, double &dBxdx, double &dBxdy, double &dBxdz, double &dBydx, double &dBydy, double &dBydz, double &dBzdx, double &dBzdy, double &dBzdz) const =0
DMagneticFieldMap * GetBfield(unsigned int run_number=1)
double zmax
int Nr
Definition: bfield2root.cc:25
double rmin
int Nz
Definition: bfield2root.cc:27
static string evioFileName
static evioFileChannel * chan
#define _DBG_
Definition: HDEVIO.h:12
jerror_t Init(void)
double zmin
void Usage(JApplication &app)
Definition: hd_ana.cc:33
int main(int argc, char *argv[])
Definition: gendoc.cc:6