BLCMDfieldexpr Class Reference

Inheritance diagram for BLCMDfieldexpr:

BLElement BLCommand

List of all members.


Detailed Description

class BLCMDfieldexpr implements a fieldexpr command

This command has no volumes, logical or physical. it just implements a field (E and/or B) from expressions on its command line.

Public Member Functions

 BLCMDfieldexpr ()
 Default constructor. Defines the command, args, etc.
virtual ~BLCMDfieldexpr ()
 Destructor.
 BLCMDfieldexpr (const BLCMDfieldexpr &r)
 Copy constructor.
BLElementclone ()
 clone()
G4String commandName ()
 commandName() returns "fieldexpr".
int command (BLArgumentVector &argv, BLArgumentMap &namedArgs)
 command() implements the fieldexpr command.
void defineNamedArgs ()
 defineNamedArgs() defines the named arguments for the command.
void construct (G4RotationMatrix *relativeRotation, G4ThreeVector relativePosition, G4LogicalVolume *parent, G4String parentName, G4RotationMatrix *parentRotation, G4ThreeVector parentPosition)
 construct() - construct the fieldexpr
G4double getLength ()
 getLength() returns 0 (no physical volume here)
G4double getWidth ()
 getWidth() returns 0 (no physical volume nere)
G4double getHeight ()
 getHeight() returns 0 (no physical volume nere)
G4bool isOK ()
 isOK() returns true.
bool isOutside (G4ThreeVector &local, G4double tolerance)
 isOutside() from BLElement. (no volume => every point is "outside")
void generatePoints (int npoints, std::vector< G4ThreeVector > &v)
 generatePoints() from BLElement. (no volume => no generate)
G4double maxError (class FieldComputation *p)
 maxError() returns the maximum relative error (i.e. error / max field).
void checkValidExpr (BLEvaluator &e)
 checkValidExpr() checks that the evaluator had a valid expression. Prints error message and exits the program if not.

Private Member Functions

void handleTimeDependence ()

Private Attributes

G4double factorB
G4double factorE
G4double timeOffset
G4String Bx
G4String By
G4String Bz
G4String Br
G4String Ex
G4String Ey
G4String Ez
G4String Er
G4String time
G4double length
G4double width
G4double height
G4double radius
G4double tmin
G4double tmax
int nX
int nY
int nZ
int nR
int nT
G4double tolerance
BLFieldMapmap

Friends

class FieldComputation


Constructor & Destructor Documentation

BLCMDfieldexpr::BLCMDfieldexpr (  ) 

Default constructor. Defines the command, args, etc.

References BLCMDTYPE_ELEMENT, Br, Bx, By, Bz, Er, Ex, Ey, Ez, factorB, factorE, height, length, map, nR, nT, nX, nY, nZ, radius, BLCommand::registerCommand(), BLCommand::setDescription(), BLCommand::setSynopsis(), time, timeOffset, tmax, tmin, tolerance, and width.

Referenced by clone(), and command().

00157                                : BLElement()
00158 {
00159         // register the commandName(), and its synopsis and description.
00160         registerCommand(BLCMDTYPE_ELEMENT);
00161         setSynopsis("implements a field map, E and/or B, from expressions.");
00162         setDescription("A fieldexpr element can be either a box or a cylinder; "
00163                 "set length and radius for cylinder, set length and width and "
00164                 "height for a box. Units are Tesla, MegaVolts/meter, mm, and "
00165                 "ns. Expressions for the field components can use {x,y,z} for "
00166                 "a box or {z,r} for a cylinder; the time expression can use "
00167                 "{t}. If present, the time expression multiples all "
00168                 "components. The number of points in the map is increased "
00169                 "until the largest map error divided by the maximum field "
00170                 "is smaller than tolerance, or 1M points is exceeded. "
00171                 "Similarly for the time dependence.\n\n"
00172                 "For time dependence: if t-timeOffset<tmin, the value at tmin "
00173                 "is used; if t-timeOffset>tmax, the value at tmax is used.");
00174 
00175         // provide initial values for fields
00176         factorB = 1.0;
00177         factorE = 1.0;
00178         timeOffset = 0.0;
00179         Bx = By = Bz = Br = Ex = Ey = Ez = Er = time = "";
00180         length = width = height = tmin = tmax = 0.0;
00181         radius = -1.0;
00182         nX = nY = nZ = nR = nT = 11;
00183         tolerance = 0.001;
00184         map = 0;
00185 }

virtual BLCMDfieldexpr::~BLCMDfieldexpr (  )  [inline, virtual]

Destructor.

00071 { }

BLCMDfieldexpr::BLCMDfieldexpr ( const BLCMDfieldexpr r  ) 

Copy constructor.

References Br, Bx, By, Bz, BLTune::copyTunableArg(), Er, Ex, Ey, Ez, factorB, factorE, height, length, map, nR, nT, nX, nY, nZ, radius, time, timeOffset, tmax, tmin, tolerance, and width.

00188                                                       : BLElement(r)
00189 {
00190         // copy fields one at a time (transfers default values from the
00191         // default object to this new object).
00192         BLTune::copyTunableArg(&factorB,&r.factorB);
00193         BLTune::copyTunableArg(&factorE,&r.factorE);
00194         timeOffset = r.timeOffset;
00195         Bx = r.Bx;
00196         By = r.By;
00197         Bz = r.Bz;
00198         Br = r.Br;
00199         Ex = r.Ex;
00200         Ey = r.Ey;
00201         Ez = r.Ez;
00202         Er = r.Er;
00203         time = r.time;
00204         length = r.length;
00205         width = r.width;
00206         height = r.height;
00207         radius = r.radius;
00208         tmin = r.tmin;
00209         tmax = r.tmax;
00210         nX = r.nX;
00211         nY = r.nY;
00212         nZ = r.nZ;
00213         nR = r.nR;
00214         nT = r.nT;
00215         tolerance = r.tolerance;
00216         map = r.map;
00217 }


Member Function Documentation

void BLCMDfieldexpr::handleTimeDependence (  )  [private]

References checkValidExpr(), BLFieldMap::createTimeDependence(), BLEvaluator::evaluate(), BLElement::getName(), map, nT, time, tmax, and tmin.

Referenced by command().

00338 {
00339         if(time != "" && nT > 1) {
00340                 BLEvaluator e;
00341                 double *t, *f;
00342                 for(;;) {
00343                         double dt = (tmax - tmin) / (nT-1);
00344                         t = new double[nT];
00345                         f = new double[nT];
00346                         for(int i=0; i<nT; ++i) {
00347                                 t[i] = tmin + dt*i;
00348                                 e.setVariable("t",t[i]);
00349                                 f[i] = e.evaluate(time);
00350                                 checkValidExpr(e);
00351                         }
00352                         // check error, double nT if needed
00353                         double err=0.0;
00354                         double v=tmin+dt/2.0;
00355                         for(int i=0; i<nT-1; ++i) {
00356                                 e.setVariable("t",v);
00357                                 double d = (f[i]+f[i+1])/2.0 - e.evaluate(time);
00358                                 checkValidExpr(e);
00359                                 if(fabs(d) > err) err = fabs(d);
00360                                 v += dt;
00361                         }
00362                         printf("fieldexpr %s: Time %d points  Max Relative Error=%.4f\n",
00363                             getName().c_str(),nT,err);
00364                         if(err <= tolerance || nT > 1000000)
00365                                 break;
00366                         nT = (nT-1)*2 + 1;
00367                         delete[] t;
00368                         delete[] f;
00369                 }
00370                 map->createTimeDependence(nT,t,f,f);
00371                 delete[] t;
00372                 delete[] f;
00373         }
00374 }

BLElement* BLCMDfieldexpr::clone (  )  [inline, virtual]

clone()

Implements BLElement.

References BLCMDfieldexpr().

00077 { return new BLCMDfieldexpr(*this); }

G4String BLCMDfieldexpr::commandName (  )  [inline, virtual]

commandName() returns "fieldexpr".

Implements BLCommand.

00080 { return "fieldexpr"; }

int BLCMDfieldexpr::command ( BLArgumentVector argv,
BLArgumentMap namedArgs 
) [virtual]

command() implements the fieldexpr command.

Implements BLCommand.

References BLCMDfieldexpr(), BLFieldMap::createCylinderMap(), BLFieldMap::createGridMap(), FieldComputation, BLElement::getName(), BLCommand::handleNamedArgs(), handleTimeDependence(), height, length, map, maxError(), nR, nX, nY, nZ, BLCommand::print(), BLCommand::printError(), radius, BLElement::setName(), tolerance, and width.

00220 {
00221         if(argv.size() != 1) {
00222                 BLCommand::printError("fieldexpr: Invalid command, must have name");
00223                 return -1;
00224         }
00225 
00226         if(argv[0] == "default") {
00227                 return defaultBLCMDfieldexpr.handleNamedArgs(namedArgs);
00228         }
00229 
00230         BLCMDfieldexpr *t = new BLCMDfieldexpr(defaultBLCMDfieldexpr);
00231         t->setName(argv[0]);
00232         int retval = t->handleNamedArgs(namedArgs);
00233 
00234         FieldComputation *p = new FieldComputation(t);
00235         t->map = new BLFieldMap();
00236 
00237         // check error; if necessary, double # map points in each dimension.
00238         double err=0.0;
00239         int ntot=0;
00240         for(;;) {
00241             if(t->radius > 0.0) {
00242                 t->map->createCylinderMap(-t->length/2,t->radius/(t->nR-1),
00243                         t->length/(t->nZ-1),t->nR,t->nZ,p);
00244                 ntot = t->nR*t->nZ;
00245             } else {
00246                 t->map->createGridMap(-t->width/2,-t->height/2,-t->length/2,
00247                         t->width/(t->nX-1),t->height/(t->nY-1),
00248                         t->length/(t->nZ-1),t->nX,t->nY,t->nZ,p);
00249                 ntot = t->nX*t->nY*t->nZ;
00250             }
00251             err = t->maxError(p);
00252             printf("fieldexpr %s: Map %d points  Max Relative Error = %.4f\n",
00253                             t->getName().c_str(),ntot,err);
00254             if(err <= t->tolerance || ntot >= 1000000)
00255                     break;
00256             if(t->radius > 0.0) {
00257                 t->nR = (t->nR-1)*2 + 1;
00258                 t->nZ = (t->nZ-1)*2 + 1;
00259             } else {
00260                 t->nX = (t->nX-1)*2 + 1;
00261                 t->nY = (t->nY-1)*2 + 1;
00262                 t->nZ = (t->nZ-1)*2 + 1;
00263             }
00264         }
00265 
00266         delete p;
00267 
00268         t->handleTimeDependence();
00269 
00270         t->print(argv[0]);
00271 
00272         return retval;
00273 }

void BLCMDfieldexpr::defineNamedArgs (  )  [virtual]

defineNamedArgs() defines the named arguments for the command.

Reimplemented from BLCommand.

References BLCommand::argDouble(), BLCommand::argInt(), BLCommand::argString(), BLCommand::argTunable(), Br, Bx, By, Bz, Er, Ex, Ey, Ez, factorB, factorE, height, length, nR, nT, nX, nY, nZ, radius, time, timeOffset, tmax, tmin, tolerance, and width.

00276 {
00277         argTunable(factorB,"factorB","Factor for the B-field (1.0).");
00278         argTunable(factorE,"factorE","Factor for the E-field (1.0).");
00279         argDouble(timeOffset,"timeOffset","Time offset (ns).");
00280         // the rest are not permitted to change
00281         argString(Bx,"Bx","Expression for Bx (Tesla), use {x,y,z}.",false);
00282         argString(By,"By","Expression for By (Tesla), use {x,y,z}.",false);
00283         argString(Bz,"Bz","Expression for Bz (Tesla), use {x,y,z} or {r,z}.",false);
00284         argString(Br,"Br","Expression for Br (Tesla), use {r,z}.",false);
00285         argString(Ex,"Ex","Expression for Ex (MV/m), use {x,y,z}.",false);
00286         argString(Ey,"Ey","Expression for Ey (MV/m), use {x,y,z}.",false);
00287         argString(Ez,"Ez","Expression for Ez (MV/m), use {x,y,z} or {r,z}.",false);
00288         argString(Er,"Er","Expression for Er (MV/m), use {r,z}.",false);
00289         argString(time,"time","Expression for time-dependence factor, use {t}.",false);
00290         argInt(nX,"nX","Number of grid points in x.",false);
00291         argInt(nY,"nY","Number of grid points in y.",false);
00292         argInt(nZ,"nZ","Number of grid points in z.",false);
00293         argInt(nR,"nR","Number of grid points in r.",false);
00294         argInt(nT,"nT","Number of grid points in t.",false);
00295         argDouble(tolerance,"tolerance","Required relative accuracy (0.001).",1,"",false);
00296         argDouble(length,"length","Length of field map (mm).",mm,"",false);
00297         argDouble(width,"width","Width of rectangular field map (mm).",mm,"",false);
00298         argDouble(height,"height","Height of rectangular field map (mm).",mm,"",false);
00299         argDouble(radius,"radius","Radius of cylindrical field map (mm).",mm,"",false);
00300         argDouble(tmin,"tmin","Minimum value of t (ns).",mm,"",false);
00301         argDouble(tmax,"tmax","Maximum value of t (ns).",mm,"",false);
00302 }

void BLCMDfieldexpr::construct ( G4RotationMatrix *  relativeRotation,
G4ThreeVector  relativePosition,
G4LogicalVolume *  parent,
G4String  parentName,
G4RotationMatrix *  parentRotation,
G4ThreeVector  parentPosition 
) [virtual]

construct() - construct the fieldexpr

Implements BLElement.

References BLGlobalField::addElementField(), factorB, factorE, BLElement::getName(), BLGlobalField::getObject(), map, and timeOffset.

00311 {
00312         G4String thisname = parentName+getName();
00313 
00314         // get globalRotation and globalPosition
00315         G4RotationMatrix *globalRotation = 0;
00316         if(relativeRotation && parentRotation) {
00317                 globalRotation = 
00318                     new G4RotationMatrix(*parentRotation * *relativeRotation);
00319         } else if(relativeRotation) {
00320                 globalRotation = relativeRotation;
00321         } else if(parentRotation) {
00322                 globalRotation = parentRotation;
00323         }
00324         G4ThreeVector globalPosition(relativePosition + parentPosition);
00325         if(parentRotation)
00326                 globalPosition = *parentRotation * relativePosition +
00327                                 parentPosition;
00328 
00329         FieldExprPlacement *p = new FieldExprPlacement(map,globalRotation,
00330                                 globalPosition,factorB,factorE,timeOffset);
00331         BLGlobalField::getObject()->addElementField(p);
00332 
00333         printf("BLMappedMagnet::Construct %s parent=%s relZ=%.1f\n",
00334                 thisname.c_str(),parentName.c_str(),relativePosition[2]);
00335 }

G4double BLCMDfieldexpr::getLength (  )  [inline, virtual]

getLength() returns 0 (no physical volume here)

Implements BLElement.

00097 { return 0.0; }

G4double BLCMDfieldexpr::getWidth (  )  [inline, virtual]

getWidth() returns 0 (no physical volume nere)

Implements BLElement.

00100 { return 0.0; }

G4double BLCMDfieldexpr::getHeight (  )  [inline, virtual]

getHeight() returns 0 (no physical volume nere)

Implements BLElement.

00103 { return 0.0; }

G4bool BLCMDfieldexpr::isOK (  )  [inline, virtual]

isOK() returns true.

Implements BLElement.

00106 { return true; }

bool BLCMDfieldexpr::isOutside ( G4ThreeVector &  local,
G4double  tolerance 
) [inline, virtual]

isOutside() from BLElement. (no volume => every point is "outside")

Implements BLElement.

00110                 { return true; }

void BLCMDfieldexpr::generatePoints ( int  npoints,
std::vector< G4ThreeVector > &  v 
) [inline, virtual]

generatePoints() from BLElement. (no volume => no generate)

Implements BLElement.

00114                 { v.clear(); }

G4double BLCMDfieldexpr::maxError ( class FieldComputation p  ) 

maxError() returns the maximum relative error (i.e. error / max field).

References FieldComputation::GetFieldValue(), BLFieldMap::getFieldValue(), height, length, map, nR, nX, nY, nZ, radius, and width.

Referenced by command().

00377 {
00378         G4double err[6],maxField[6];
00379         for(int i=0; i<6; ++i)
00380                 err[i] = maxField[i] = 0.0;
00381 
00382         // find the max error at the center of every grid box
00383         if(radius > 0.0) {
00384                 double dR = radius/(nR-1);
00385                 double dZ = length/(nZ-1);
00386                 for(double z=(-length+dZ)/2.0; z<length/2.0; z+=dZ) {
00387                         for(double r=dR/2.0; r<radius; r+=dR) {
00388                                 G4double point[4],field1[6],field2[6];
00389                                 point[0] = r;
00390                                 point[1] = 0.0;
00391                                 point[2] = z;
00392                                 point[3] = 0.0; // map has no time dependence
00393                                 map->getFieldValue(point,field1,1.0,1.0);
00394                                 p->GetFieldValue(point,field2);
00395                                 for(int i=0; i<6; ++i) {
00396                                         if(fabs(field2[i]) > maxField[i])
00397                                             maxField[i] = fabs(field2[i]);
00398                                         if(fabs(field2[i]-field1[i]) > err[i])
00399                                             err[i] = fabs(field2[i]-field1[i]);
00400                                 }
00401                         }
00402                 }
00403         } else {
00404                 double dX = width/(nX-1);
00405                 double dY = height/(nY-1);
00406                 double dZ = length/(nZ-1);
00407                 for(double z=(-length+dZ)/2.0; z<length/2.0; z+=dZ) {
00408                     for(double y=(-height+dY)/2.0; y<height/2.0; y+= dY) {
00409                         for(double x=(-width+dX)/2.0; x<width/2.0; x+=dX) {
00410                             G4double point[4],field1[6],field2[6];
00411                             point[0] = x;
00412                             point[1] = y;
00413                             point[2] = z;
00414                             point[3] = 0.0; // map has no time dependence, yet
00415                             map->getFieldValue(point,field1,1.0,1.0);
00416                             p->GetFieldValue(point,field2);
00417                             for(int i=0; i<6; ++i) {
00418                                 if(fabs(field2[i]) > maxField[i])
00419                                         maxField[i] = fabs(field2[i]);
00420                                 if(fabs(field2[i]-field1[i]) > err[i])
00421                                         err[i] = fabs(field2[i]-field1[i]);
00422                             }
00423                         }
00424                     }
00425                 }
00426         }
00427 
00428         // denominator is largest component, not largest norm -- good enough.
00429         if(maxField[1] > maxField[0]) maxField[0] = maxField[1];
00430         if(maxField[2] > maxField[0]) maxField[0] = maxField[2];
00431         if(maxField[4] > maxField[3]) maxField[3] = maxField[4];
00432         if(maxField[5] > maxField[3]) maxField[3] = maxField[5];
00433         if(err[1] > err[0]) err[0] = err[1];
00434         if(err[2] > err[0]) err[0] = err[2];
00435         if(err[4] > err[3]) err[3] = err[4];
00436         if(err[5] > err[3]) err[3] = err[5];
00437         G4double errB=0.0, errE=0.0;
00438         if(maxField[0] > 0.000001) errB = err[0]/maxField[0];
00439         if(maxField[3] > 0.000001) errE = err[3]/maxField[3];
00440         return (errB>errE ? errB : errE);
00441 }

void BLCMDfieldexpr::checkValidExpr ( BLEvaluator e  )  [inline]

checkValidExpr() checks that the evaluator had a valid expression. Prints error message and exits the program if not.

References BLElement::getName(), and BLEvaluator::isOK().

Referenced by FieldComputation::GetFieldValue(), and handleTimeDependence().

00123                 { if(!e.isOK()) {
00124                         printf("***fieldexpr '%s': Invalid expression\n",
00125                                 getName().c_str());
00126                         exit(99);
00127                   }
00128                 }


Friends And Related Function Documentation

friend class FieldComputation [friend]

Referenced by command().


Member Data Documentation

G4double BLCMDfieldexpr::factorB [private]

G4double BLCMDfieldexpr::factorE [private]

G4double BLCMDfieldexpr::timeOffset [private]

G4String BLCMDfieldexpr::Bx [private]

G4String BLCMDfieldexpr::By [private]

G4String BLCMDfieldexpr::Bz [private]

G4String BLCMDfieldexpr::Br [private]

G4String BLCMDfieldexpr::Ex [private]

G4String BLCMDfieldexpr::Ey [private]

G4String BLCMDfieldexpr::Ez [private]

G4String BLCMDfieldexpr::Er [private]

G4String BLCMDfieldexpr::time [private]

G4double BLCMDfieldexpr::length [private]

G4double BLCMDfieldexpr::width [private]

G4double BLCMDfieldexpr::height [private]

G4double BLCMDfieldexpr::radius [private]

G4double BLCMDfieldexpr::tmin [private]

G4double BLCMDfieldexpr::tmax [private]

int BLCMDfieldexpr::nX [private]

int BLCMDfieldexpr::nY [private]

int BLCMDfieldexpr::nZ [private]

int BLCMDfieldexpr::nR [private]

int BLCMDfieldexpr::nT [private]

G4double BLCMDfieldexpr::tolerance [private]


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