GridImpl Class Reference

Inheritance diagram for GridImpl:

FieldMapImpl

List of all members.


Detailed Description

class GridImpl -- class for a Grid FieldMap implementation

Public Member Functions

 GridImpl (BLArgumentVector &argv, BLArgumentMap &namedArgs)
 ~GridImpl ()
void getFieldValue (const G4double local[4], G4double field[6]) const
bool handleCommand (InputFile &in, BLArgumentVector &argv, BLArgumentMap &namedArgs)
virtual void getBoundingPoint (int i, G4double point[4])
virtual bool hasB ()
virtual bool hasE ()
int bits (G4String s)
const char * bits2str (int v)
bool setField (G4double X, G4double Y, G4double Z, G4double Bx, G4double By, G4double Bz, G4double Ex, G4double Ey, G4double Ez, int linenumber)
virtual bool writeFile (FILE *f)

Private Attributes

G4int nX
G4int nY
G4int nZ
G4double dX
G4double dY
G4double dZ
G4double X0
G4double Y0
G4double Z0
G4double tolerance
float * mapBx
float * mapBy
float * mapBz
float * mapEx
float * mapEy
float * mapEz
bool extendX
bool extendY
bool extendZ
int extendXbits
int extendYbits
int extendZbits

Constructor & Destructor Documentation

GridImpl::GridImpl ( BLArgumentVector argv,
BLArgumentMap namedArgs 
)

References argDouble(), argInt(), dX, dY, dZ, extendX, extendXbits, extendY, extendYbits, extendZ, extendZbits, mapBx, mapBy, mapBz, mapEx, mapEy, mapEz, nX, nY, nZ, tolerance, X0, Y0, and Z0.

00567                                                         : FieldMapImpl()
00568 {
00569         nX = 2;
00570         nY = 2;
00571         nZ = 2;
00572         dX = 10.0*mm;
00573         dY = 10.0*mm;
00574         dZ = 10.0*mm;
00575         X0 = 0.0;
00576         Y0 = 0.0;
00577         Z0 = 0.0;
00578         tolerance = 0.01*mm;
00579         mapBx = 0;
00580         mapBy = 0;
00581         mapBz = 0;
00582         mapEx = 0;
00583         mapEy = 0;
00584         mapEz = 0;
00585         extendX = false;
00586         extendY = false;
00587         extendZ = false;
00588         extendXbits = 0;
00589         extendYbits = 0;
00590         extendZbits = 0;
00591         argInt(nX,"nX",namedArgs);
00592         argInt(nY,"nY",namedArgs);
00593         argInt(nZ,"nZ",namedArgs);
00594         argDouble(dX,"dX",namedArgs);
00595         argDouble(dY,"dY",namedArgs);
00596         argDouble(dZ,"dZ",namedArgs);
00597         argDouble(X0,"X0",namedArgs);
00598         argDouble(Y0,"Y0",namedArgs);
00599         argDouble(Z0,"Z0",namedArgs);
00600         argDouble(tolerance,"tolerance",namedArgs);
00601 }

GridImpl::~GridImpl (  ) 

References mapBx, mapBy, mapBz, mapEx, mapEy, and mapEz.

00604 {
00605         if(mapBx) delete mapBx;
00606         if(mapBy) delete mapBy;
00607         if(mapBz) delete mapBz;
00608         if(mapEx) delete mapEx;
00609         if(mapEy) delete mapEy;
00610         if(mapEz) delete mapEz;
00611 }


Member Function Documentation

void GridImpl::getFieldValue ( const G4double  local[4],
G4double  field[6] 
) const [virtual]

Implements FieldMapImpl.

References COMPONENT, dX, dY, dZ, extendX, extendXbits, extendY, extendYbits, extendZ, extendZbits, mapBx, mapBy, mapBz, mapEx, mapEy, mapEz, nX, nY, nZ, X0, Y0, and Z0.

00615 {
00616         G4double x = local[0];
00617         G4double y = local[1];
00618         G4double z = local[2];
00619 
00620         x -= X0;
00621         y -= Y0;
00622         z -= Z0;
00623 
00624         G4double factor[6];
00625         factor[0]=factor[1]=factor[2]=factor[3]=factor[4]=factor[5]=1.0;
00626         if(extendX && x < 0.0) {
00627                 x = -x;
00628                 for(int i=0; i<6; ++i) {
00629                         if(extendXbits & (1<<i)) factor[i] = -factor[i];
00630                 }
00631         }
00632         if(extendY && y < 0.0) {
00633                 y = -y;
00634                 for(int i=0; i<6; ++i) {
00635                         if(extendYbits & (1<<i)) factor[i] = -factor[i];
00636                 }
00637         }
00638         if(extendZ && z < 0.0) {
00639                 z = -z;
00640                 for(int i=0; i<6; ++i) {
00641                         if(extendZbits & (1<<i)) factor[i] = -factor[i];
00642                 }
00643         }
00644 
00645         // We compute a 3D linear average of the 8 surrounding points in the map
00646         // First, get the X,Y,Z indices into i,j,k
00647         int i = (int)floor(x/dX);
00648         int j = (int)floor(y/dY);
00649         int k = (int)floor(z/dZ);
00650         if(i < 0 || i >= nX-1 || j < 0 || j >= nY-1 || k < 0 || k >= nZ-1) {
00651                 field[0] = field[1] = field[2] = field[3] = field[4] = 
00652                                                                 field[5] = 0.0;
00653                 return;
00654         }
00655         // m is the initial index (corner of the cube with minimum X, Y, and Z)
00656         int m = k*nY*nX + j*nX + i;
00657         assert(m+nY*nX+nX+1 < nX*nY*nZ);
00658 
00659         // now compute the fractional weighting factors for X, Y, and Z
00660         float fx = 1.0 - (x - i*dX) / dX;
00661         assert(fx >= 0.0 && fx <= 1.0);
00662         float fy = 1.0 - (y - j*dY) / dY;
00663         assert(fy >= 0.0 && fy <= 1.0);
00664         float fz = 1.0 - (z - k*dZ) / dZ;
00665         assert(fz >= 0.0 && fz <= 1.0);
00666 
00667         // now compute the fractional weighting factors for the 8 corners
00668         float f0 = fx*fy*fz;
00669         float f1 = (1.0-fx)*fy*fz;
00670         float f2 = fx*(1.0-fy)*fz;
00671         float f3 = (1.0-fx)*(1.0-fy)*fz;
00672         float f4 = fx*fy*(1.0-fz);
00673         float f5 = (1.0-fx)*fy*(1.0-fz);
00674         float f6 = fx*(1.0-fy)*(1.0-fz);
00675         float f7 = (1.0-fx)*(1.0-fy)*(1.0-fz);
00676 
00677         // Finally, compute the components of the field
00678 #define COMPONENT(C)                                            \
00679         G4double C = 0.0;                                       \
00680         if(map##C) C =  map##C[m]*f0 + map##C[m+1]*f1 +         \
00681                 map##C[m+nX]*f2 + map##C[m+nX+1]*f3 +           \
00682                 map##C[m+nY*nX]*f4 + map##C[m+nY*nX+1]*f5 +     \
00683                 map##C[m+nY*nX+nX]*f6 + map##C[m+nY*nX+nX+1]*f7;
00684         COMPONENT(Bx);
00685         COMPONENT(By);
00686         COMPONENT(Bz);
00687         COMPONENT(Ex);
00688         COMPONENT(Ey);
00689         COMPONENT(Ez);
00690 
00691 #ifdef STUB
00692         G4double Bx = 0.0;
00693         if(mapBx) Bx =
00694                 mapBx[m]*fx*fy*fz +
00695                 mapBx[m+1]*(1.0-fx)*fy*fz +
00696                 mapBx[m+nX]*fx*(1.0-fy)*fz +
00697                 mapBx[m+nX+1]*(1.0-fx)*(1.0-fy)*fz +
00698                 mapBx[m+nY*nX]*fx*fy*(1.0-fz) +
00699                 mapBx[m+nY*nX+1]*(1.0-fx)*fy*(1.0-fz) +
00700                 mapBx[m+nY*nX+nX]*fx*(1.0-fy)*(1.0-fz) +
00701                 mapBx[m+nY*nX+nX+1]*(1.0-fx)*(1.0-fy)*(1.0-fz);
00702         G4double By = 0.0;
00703         if(mapBy) By =
00704                 mapBy[m]*fx*fy*fz +
00705                 mapBy[m+1]*(1.0-fx)*fy*fz +
00706                 mapBy[m+nX]*fx*(1.0-fy)*fz +
00707                 mapBy[m+nX+1]*(1.0-fx)*(1.0-fy)*fz +
00708                 mapBy[m+nY*nX]*fx*fy*(1.0-fz) +
00709                 mapBy[m+nY*nX+1]*(1.0-fx)*fy*(1.0-fz) +
00710                 mapBy[m+nY*nX+nX]*fx*(1.0-fy)*(1.0-fz) +
00711                 mapBy[m+nY*nX+nX+1]*(1.0-fx)*(1.0-fy)*(1.0-fz);
00712         G4double Bz = 0.0;
00713         if(mapBz) Bz =
00714                 mapBz[m]*fx*fy*fz +
00715                 mapBz[m+1]*(1.0-fx)*fy*fz +
00716                 mapBz[m+nX]*fx*(1.0-fy)*fz +
00717                 mapBz[m+nX+1]*(1.0-fx)*(1.0-fy)*fz +
00718                 mapBz[m+nY*nX]*fx*fy*(1.0-fz) +
00719                 mapBz[m+nY*nX+1]*(1.0-fx)*fy*(1.0-fz) +
00720                 mapBz[m+nY*nX+nX]*fx*(1.0-fy)*(1.0-fz) +
00721                 mapBz[m+nY*nX+nX+1]*(1.0-fx)*(1.0-fy)*(1.0-fz);
00722 
00723         G4double Ex = 0.0;
00724         if(mapEx) Ex =
00725                 mapEx[m]*fx*fy*fz +
00726                 mapEx[m+1]*(1.0-fx)*fy*fz +
00727                 mapEx[m+nX]*fx*(1.0-fy)*fz +
00728                 mapEx[m+nX+1]*(1.0-fx)*(1.0-fy)*fz +
00729                 mapEx[m+nY*nX]*fx*fy*(1.0-fz) +
00730                 mapEx[m+nY*nX+1]*(1.0-fx)*fy*(1.0-fz) +
00731                 mapEx[m+nY*nX+nX]*fx*(1.0-fy)*(1.0-fz) +
00732                 mapEx[m+nY*nX+nX+1]*(1.0-fx)*(1.0-fy)*(1.0-fz);
00733         G4double Ey = 0.0;
00734         if(mapEy) Ey =
00735                 mapEy[m]*fx*fy*fz +
00736                 mapEy[m+1]*(1.0-fx)*fy*fz +
00737                 mapEy[m+nX]*fx*(1.0-fy)*fz +
00738                 mapEy[m+nX+1]*(1.0-fx)*(1.0-fy)*fz +
00739                 mapEy[m+nY*nX]*fx*fy*(1.0-fz) +
00740                 mapEy[m+nY*nX+1]*(1.0-fx)*fy*(1.0-fz) +
00741                 mapEy[m+nY*nX+nX]*fx*(1.0-fy)*(1.0-fz) +
00742                 mapEy[m+nY*nX+nX+1]*(1.0-fx)*(1.0-fy)*(1.0-fz);
00743         G4double Ez = 0.0;
00744         if(mapEz) Ez =
00745                 mapEz[m]*fx*fy*fz +
00746                 mapEz[m+1]*(1.0-fx)*fy*fz +
00747                 mapEz[m+nX]*fx*(1.0-fy)*fz +
00748                 mapEz[m+nX+1]*(1.0-fx)*(1.0-fy)*fz +
00749                 mapEz[m+nY*nX]*fx*fy*(1.0-fz) +
00750                 mapEz[m+nY*nX+1]*(1.0-fx)*fy*(1.0-fz) +
00751                 mapEz[m+nY*nX+nX]*fx*(1.0-fy)*(1.0-fz) +
00752                 mapEz[m+nY*nX+nX+1]*(1.0-fx)*(1.0-fy)*(1.0-fz);
00753 #endif //STUB
00754 
00755         field[0] = Bx * factor[0];
00756         field[1] = By * factor[1];
00757         field[2] = Bz * factor[2];
00758         field[3] = Ex * factor[3];
00759         field[4] = Ey * factor[4];
00760         field[5] = Ez * factor[5];
00761 }

bool GridImpl::handleCommand ( InputFile in,
BLArgumentVector argv,
BLArgumentMap namedArgs 
) [virtual]

Implements FieldMapImpl.

References bits(), extendX, extendXbits, extendY, extendYbits, extendZ, extendZbits, InputFile::getline(), InputFile::linenumber(), BLCommand::printError(), InputFile::repeatLine(), and setField().

00765 {
00766         if(argv[0] == "extendX") {
00767                 extendX = true;
00768                 extendXbits = bits(namedArgs["flip"]);
00769                 return true;
00770         } else if(argv[0] == "extendY") {
00771                 extendY = true;
00772                 extendYbits = bits(namedArgs["flip"]);
00773                 return true;
00774         } else if(argv[0] == "extendZ") {
00775                 extendZ = true;
00776                 extendZbits = bits(namedArgs["flip"]);
00777                 return true;
00778         } else if(argv[0] == "Bx") {
00779                 BLCommand::printError("BLFieldMap: Bx not implemented");
00780                 return true;
00781         } else if(argv[0] == "By") {
00782                 BLCommand::printError("BLFieldMap: By not implemented");
00783                 return true;
00784         } else if(argv[0] == "Bz") {
00785                 BLCommand::printError("BLFieldMap: Bz not implemented");
00786                 return true;
00787         } else if(argv[0] == "Ex") {
00788                 BLCommand::printError("BLFieldMap: Ex not implemented");
00789                 return true;
00790         } else if(argv[0] == "Ey") {
00791                 BLCommand::printError("BLFieldMap: Ey not implemented");
00792                 return true;
00793         } else if(argv[0] == "Ez") {
00794                 BLCommand::printError("BLFieldMap: Ez not implemented");
00795                 return true;
00796         } else if(argv[0] == "data") {
00797                 for(char *line=0; (line=in.getline())!=0; ) {
00798                         if(isalpha(line[0])) {
00799                                 in.repeatLine();
00800                                 break;
00801                         }
00802                         int n;
00803                         float X=0.0,Y=0.0,Z=0.0,Bx=0.0,By=0.0,Bz=0.0,
00804                                                         Ex=0.0,Ey=0.0,Ez=0.0;
00805                         for(char *p=line; (p=strchr(p,','))!=0;) *p = ' ';
00806                         n = sscanf(line,"%f%f%f%f%f%f%f%f%f",&X,&Y,&Z,
00807                                                 &Bx,&By,&Bz,&Ex,&Ey,&Ez);
00808                         if(n <= 3) {
00809                                 BLCommand::printError("BLFieldMap: invalid line %d\n",in.linenumber());
00810                                 continue;
00811                         }
00812                         setField(X,Y,Z,Bx*tesla,By*tesla,Bz*tesla,
00813                                 Ex*megavolt/meter,Ey*megavolt/meter,
00814                                 Ez*megavolt/meter,in.linenumber());
00815                 }
00816                 return true;
00817         } else {
00818                 return false;
00819         }
00820 }

void GridImpl::getBoundingPoint ( int  i,
G4double  point[4] 
) [virtual]

Implements FieldMapImpl.

References dX, dY, dZ, extendX, extendY, extendZ, nX, nY, nZ, X0, Y0, and Z0.

00823 {
00824         if(extendX)
00825                 point[0] = (i&1 ? -(nX-1)*dX : (nX-1)*dX) + X0;
00826         else
00827                 point[0] = (i&1 ? 0.0 : (nX-1)*dX) + X0;
00828         if(extendY)
00829                 point[1] = (i&2 ? -(nY-1)*dY : (nY-1)*dY) + Y0;
00830         else
00831                 point[1] = (i&2 ? 0.0 : (nY-1)*dY) + Y0;
00832         if(extendZ)
00833                 point[2] = (i&4 ? -(nZ-1)*dZ : (nZ-1)*dZ) + Z0;
00834         else
00835                 point[2] = (i&4 ? 0.0 : (nZ-1)*dZ) + Z0;
00836 }

virtual bool GridImpl::hasB (  )  [inline, virtual]

Implements FieldMapImpl.

References mapBx, mapBy, and mapBz.

00131 { return mapBx!=0 || mapBy!=0 || mapBz!=0; }

virtual bool GridImpl::hasE (  )  [inline, virtual]

Implements FieldMapImpl.

References mapEx, mapEy, and mapEz.

Referenced by writeFile().

00132 { return mapEx!=0 || mapEy!=0 || mapEz!=0; }

int GridImpl::bits ( G4String  s  ) 

Referenced by handleCommand().

00839 {
00840         int v=0;
00841         if(s.find("Bx") < s.size()) v |= 1;
00842         if(s.find("By") < s.size()) v |= 2;
00843         if(s.find("Bz") < s.size()) v |= 4;
00844         if(s.find("Ex") < s.size()) v |= 8;
00845         if(s.find("Ey") < s.size()) v |= 16;
00846         if(s.find("Ez") < s.size()) v |= 32;
00847         return v;
00848 }

const char * GridImpl::bits2str ( int  v  ) 

Referenced by writeFile().

00851 {
00852         static char retval[32];
00853         retval[0] = '\0';
00854         if(v & 1) strcat(retval,"Bx,");
00855         if(v & 2) strcat(retval,"By,");
00856         if(v & 4) strcat(retval,"Bz,");
00857         if(v & 8) strcat(retval,"Ex,");
00858         if(v & 16) strcat(retval,"Ey,");
00859         if(v & 32) strcat(retval,"Ez,");
00860         return retval;
00861 }

bool GridImpl::setField ( G4double  X,
G4double  Y,
G4double  Z,
G4double  Bx,
G4double  By,
G4double  Bz,
G4double  Ex,
G4double  Ey,
G4double  Ez,
int  linenumber 
)

References dX, dY, dZ, mapBx, mapBy, mapBz, mapEx, mapEy, mapEz, nX, nY, nZ, BLCommand::printError(), tolerance, X0, Y0, and Z0.

Referenced by BLFieldMap::createGridMap(), and handleCommand().

00866 {
00867         if(!mapBx && Bx != 0.0) {
00868                 mapBx = new float[nX*nY*nZ];
00869                 assert(mapBx != 0);
00870                 for(int i=0; i<nX*nY*nZ; ++i) 
00871                         mapBx[i] = 0.0;
00872         }
00873         if(!mapBy && By != 0.0) {
00874                 mapBy = new float[nX*nY*nZ];
00875                 assert(mapBy != 0);
00876                 for(int i=0; i<nX*nY*nZ; ++i) 
00877                         mapBy[i] = 0.0;
00878         }
00879         if(!mapBz && Bz != 0.0) {
00880                 mapBz = new float[nX*nY*nZ];
00881                 assert(mapBz != 0);
00882                 for(int i=0; i<nX*nY*nZ; ++i) 
00883                         mapBz[i] = 0.0;
00884         }
00885         if(!mapEx && Ex != 0.0) {
00886                 mapEx = new float[nX*nY*nZ];
00887                 assert(mapEx != 0);
00888                 for(int i=0; i<nX*nY*nZ; ++i) 
00889                         mapEx[i] = 0.0;
00890         }
00891         if(!mapEy && Ey != 0.0) {
00892                 mapEy = new float[nX*nY*nZ];
00893                 assert(mapEy != 0);
00894                 for(int i=0; i<nX*nY*nZ; ++i) 
00895                         mapEy[i] = 0.0;
00896         }
00897         if(!mapEz && Ez != 0.0) {
00898                 mapEz = new float[nX*nY*nZ];
00899                 assert(mapEz != 0);
00900                 for(int i=0; i<nX*nY*nZ; ++i) 
00901                         mapEz[i] = 0.0;
00902         }
00903         X -= X0;
00904         Y -= Y0;
00905         Z -= Z0;
00906         int i = (int)floor((X/dX) + 0.5);
00907         if(i<0 || fabs(i*dX-X)>tolerance || i >= nX) {
00908                 BLCommand::printError("BLFieldMap: ERROR point off"
00909                         " grid  X=%.2f line=%d\n",X, linenumber);
00910                 return false;
00911         }
00912         int j = (int)floor((Y/dY) + 0.5);
00913         if(j<0 || fabs(j*dY-Y)>tolerance || j >= nY) {
00914                 BLCommand::printError("BLFieldMap: ERROR point off"
00915                         " grid Y=%.2f line=%d\n",Y, linenumber);
00916                 return false;
00917         }
00918         int k = (int)floor((Z/dZ) + 0.5);
00919         if(k<0 || fabs(k*dZ-Z)>tolerance || k >= nZ) {
00920                 BLCommand::printError("BLFieldMap: ERROR point off"
00921                         " grid X=%.2f line=%d\n",Z, linenumber);
00922                 return false;
00923         }
00924         int m = k*nY*nX + j*nX + i;
00925         assert(m >= 0 && m < nX*nY*nZ);
00926         if(mapBx) mapBx[m] = Bx;
00927         if(mapBy) mapBy[m] = By;
00928         if(mapBz) mapBz[m] = Bz;
00929         if(mapEx) mapEx[m] = Ex;
00930         if(mapEy) mapEy[m] = Ey;
00931         if(mapEz) mapEz[m] = Ez;
00932 
00933         return true;
00934 }

bool GridImpl::writeFile ( FILE *  f  )  [virtual]

Implements FieldMapImpl.

References bits2str(), dX, dY, dZ, extendX, extendXbits, extendY, extendYbits, extendZ, extendZbits, hasE(), mapBx, mapBy, mapBz, mapEx, mapEy, mapEz, nX, nY, nZ, X0, Y0, and Z0.

00937 {
00938         fprintf(f,"grid X0=%g Y0=%g Z0=%g nX=%d nY=%d nZ=%d dX=%g dY=%g dZ=%g\n",
00939                 X0,Y0,Z0,nX,nY,nZ,dX,dY,dZ);
00940         if(extendX) fprintf(f,"extendX flip=%s\n",bits2str(extendXbits));
00941         if(extendY) fprintf(f,"extendY flip=%s\n",bits2str(extendYbits));
00942         if(extendZ) fprintf(f,"extendZ flip=%s\n",bits2str(extendZbits));
00943         fprintf(f,"data\n");
00944 
00945         for(int i=0; i<nX; ++i) {
00946                 G4double X = X0 + i*dX;
00947                 for(int j=0; j<nY; ++j) {
00948                         G4double Y = Y0 + j*dY;
00949                         for(int k=0; k<nZ; ++k) {
00950                                 G4double Z = Z0 + k*dZ;
00951                                 int m = k*nY*nX + j*nX + i;
00952                                 assert(m >= 0 && m < nX*nY*nZ);
00953                                 G4double Bx = (mapBx ? mapBx[m] : 0.0);
00954                                 G4double By = (mapBy ? mapBy[m] : 0.0);
00955                                 G4double Bz = (mapBz ? mapBz[m] : 0.0);
00956                                 fprintf(f,"%.1f %.1f %.1f %g %g %g",
00957                                         X,Y,Z,Bx/tesla,By/tesla,Bz/tesla);
00958                                 if(hasE()) {
00959                                         G4double Ex = (mapEx ? mapEx[m] : 0.0);
00960                                         G4double Ey = (mapEy ? mapEy[m] : 0.0);
00961                                         G4double Ez = (mapEz ? mapEz[m] : 0.0);
00962                                         fprintf(f," %g %g %g",
00963                                                 Ex/(megavolt/meter),
00964                                                 Ey/(megavolt/meter),
00965                                                 Ez/(megavolt/meter));
00966                                 }
00967                                 fprintf(f,"\n");
00968                         }
00969                 }
00970         }
00971         return true;
00972 }


Member Data Documentation

G4int GridImpl::nX [private]

G4int GridImpl::nY [private]

G4int GridImpl::nZ [private]

G4double GridImpl::dX [private]

G4double GridImpl::dY [private]

G4double GridImpl::dZ [private]

G4double GridImpl::X0 [private]

G4double GridImpl::Y0 [private]

G4double GridImpl::Z0 [private]

G4double GridImpl::tolerance [private]

Referenced by GridImpl(), and setField().

float* GridImpl::mapBx [private]

float* GridImpl::mapBy [private]

float* GridImpl::mapBz [private]

float* GridImpl::mapEx [private]

float* GridImpl::mapEy [private]

float* GridImpl::mapEz [private]

bool GridImpl::extendX [private]

bool GridImpl::extendY [private]

bool GridImpl::extendZ [private]

int GridImpl::extendXbits [private]

int GridImpl::extendYbits [private]

int GridImpl::extendZbits [private]


The documentation for this class was generated from the following file:
g4beamline