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. | |
BLElement * | clone () |
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 |
BLFieldMap * | map |
Friends | |
class | FieldComputation |
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().
00158 : BLElement() 00159 { 00160 // register the commandName(), and its synopsis and description. 00161 registerCommand(BLCMDTYPE_ELEMENT); 00162 setSynopsis("implements a field map, E and/or B, from expressions."); 00163 setDescription("A fieldexpr element can be either a box or a cylinder; " 00164 "set length and radius for cylinder, set length and width and " 00165 "height for a box. Units are Tesla, MegaVolts/meter, mm, and " 00166 "ns. Expressions for the field components can use {x,y,z} for " 00167 "a box or {z,r} for a cylinder; the time expression can use " 00168 "{t}. If present, the time expression multiples all " 00169 "components. The number of points in the map is increased " 00170 "until the largest map error divided by the maximum field " 00171 "is smaller than tolerance, or 1M points is exceeded. " 00172 "Similarly for the time dependence.\n\n" 00173 "For time dependence: if t-timeOffset<tmin, the value at tmin " 00174 "is used; if t-timeOffset>tmax, the value at tmax is used."); 00175 00176 // provide initial values for fields 00177 factorB = 1.0; 00178 factorE = 1.0; 00179 timeOffset = 0.0; 00180 Bx = By = Bz = Br = Ex = Ey = Ez = Er = time = ""; 00181 length = width = height = tmin = tmax = 0.0; 00182 radius = -1.0; 00183 nX = nY = nZ = nR = nT = 11; 00184 tolerance = 0.001; 00185 map = 0; 00186 }
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.
00189 : BLElement(r) 00190 { 00191 // copy fields one at a time (transfers default values from the 00192 // default object to this new object). 00193 BLTune::copyTunableArg(&factorB,&r.factorB); 00194 BLTune::copyTunableArg(&factorE,&r.factorE); 00195 timeOffset = r.timeOffset; 00196 Bx = r.Bx; 00197 By = r.By; 00198 Bz = r.Bz; 00199 Br = r.Br; 00200 Ex = r.Ex; 00201 Ey = r.Ey; 00202 Ez = r.Ez; 00203 Er = r.Er; 00204 time = r.time; 00205 length = r.length; 00206 width = r.width; 00207 height = r.height; 00208 radius = r.radius; 00209 tmin = r.tmin; 00210 tmax = r.tmax; 00211 nX = r.nX; 00212 nY = r.nY; 00213 nZ = r.nZ; 00214 nR = r.nR; 00215 nT = r.nT; 00216 tolerance = r.tolerance; 00217 map = r.map; 00218 }
void BLCMDfieldexpr::handleTimeDependence | ( | ) | [private] |
References checkValidExpr(), BLFieldMap::createTimeDependence(), BLEvaluator::evaluate(), BLElement::getName(), map, nT, time, tmax, and tmin.
Referenced by command().
00339 { 00340 if(time != "" && nT > 1) { 00341 BLEvaluator e; 00342 double *t, *f; 00343 for(;;) { 00344 double dt = (tmax - tmin) / (nT-1); 00345 t = new double[nT]; 00346 f = new double[nT]; 00347 for(int i=0; i<nT; ++i) { 00348 t[i] = tmin + dt*i; 00349 e.setVariable("t",t[i]); 00350 f[i] = e.evaluate(time); 00351 checkValidExpr(e); 00352 } 00353 // check error, double nT if needed 00354 double err=0.0; 00355 double v=tmin+dt/2.0; 00356 for(int i=0; i<nT-1; ++i) { 00357 e.setVariable("t",v); 00358 double d = (f[i]+f[i+1])/2.0 - e.evaluate(time); 00359 checkValidExpr(e); 00360 if(fabs(d) > err) err = fabs(d); 00361 v += dt; 00362 } 00363 printf("fieldexpr %s: Time %d points Max Relative Error=%.4f\n", 00364 getName().c_str(),nT,err); 00365 if(err <= tolerance || nT > 1000000) 00366 break; 00367 nT = (nT-1)*2 + 1; 00368 delete[] t; 00369 delete[] f; 00370 } 00371 map->createTimeDependence(nT,t,f,f); 00372 delete[] t; 00373 delete[] f; 00374 } 00375 }
BLElement* BLCMDfieldexpr::clone | ( | ) | [inline, virtual] |
G4String BLCMDfieldexpr::commandName | ( | ) | [inline, virtual] |
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.
00221 { 00222 if(argv.size() != 1) { 00223 BLCommand::printError("fieldexpr: Invalid command, must have name"); 00224 return -1; 00225 } 00226 00227 if(argv[0] == "default") { 00228 return defaultBLCMDfieldexpr.handleNamedArgs(namedArgs); 00229 } 00230 00231 BLCMDfieldexpr *t = new BLCMDfieldexpr(defaultBLCMDfieldexpr); 00232 t->setName(argv[0]); 00233 int retval = t->handleNamedArgs(namedArgs); 00234 00235 FieldComputation *p = new FieldComputation(t); 00236 t->map = new BLFieldMap(); 00237 00238 // check error; if necessary, double # map points in each dimension. 00239 double err=0.0; 00240 int ntot=0; 00241 for(;;) { 00242 if(t->radius > 0.0) { 00243 t->map->createCylinderMap(-t->length/2,t->radius/(t->nR-1), 00244 t->length/(t->nZ-1),t->nR,t->nZ,p); 00245 ntot = t->nR*t->nZ; 00246 } else { 00247 t->map->createGridMap(-t->width/2,-t->height/2,-t->length/2, 00248 t->width/(t->nX-1),t->height/(t->nY-1), 00249 t->length/(t->nZ-1),t->nX,t->nY,t->nZ,p); 00250 ntot = t->nX*t->nY*t->nZ; 00251 } 00252 err = t->maxError(p); 00253 printf("fieldexpr %s: Map %d points Max Relative Error = %.4f\n", 00254 t->getName().c_str(),ntot,err); 00255 if(err <= t->tolerance || ntot >= 1000000) 00256 break; 00257 if(t->radius > 0.0) { 00258 t->nR = (t->nR-1)*2 + 1; 00259 t->nZ = (t->nZ-1)*2 + 1; 00260 } else { 00261 t->nX = (t->nX-1)*2 + 1; 00262 t->nY = (t->nY-1)*2 + 1; 00263 t->nZ = (t->nZ-1)*2 + 1; 00264 } 00265 } 00266 00267 delete p; 00268 00269 t->handleTimeDependence(); 00270 00271 t->print(argv[0]); 00272 00273 return retval; 00274 }
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.
00277 { 00278 argTunable(factorB,"factorB","Factor for the B-field (1.0)."); 00279 argTunable(factorE,"factorE","Factor for the E-field (1.0)."); 00280 argDouble(timeOffset,"timeOffset","Time offset (ns)."); 00281 // the rest are not permitted to change 00282 argString(Bx,"Bx","Expression for Bx (Tesla), use {x,y,z}.",false); 00283 argString(By,"By","Expression for By (Tesla), use {x,y,z}.",false); 00284 argString(Bz,"Bz","Expression for Bz (Tesla), use {x,y,z} or {r,z}.",false); 00285 argString(Br,"Br","Expression for Br (Tesla), use {r,z}.",false); 00286 argString(Ex,"Ex","Expression for Ex (MV/m), use {x,y,z}.",false); 00287 argString(Ey,"Ey","Expression for Ey (MV/m), use {x,y,z}.",false); 00288 argString(Ez,"Ez","Expression for Ez (MV/m), use {x,y,z} or {r,z}.",false); 00289 argString(Er,"Er","Expression for Er (MV/m), use {r,z}.",false); 00290 argString(time,"time","Expression for time-dependence factor, use {t}.",false); 00291 argInt(nX,"nX","Number of grid points in x.",false); 00292 argInt(nY,"nY","Number of grid points in y.",false); 00293 argInt(nZ,"nZ","Number of grid points in z.",false); 00294 argInt(nR,"nR","Number of grid points in r.",false); 00295 argInt(nT,"nT","Number of grid points in t.",false); 00296 argDouble(tolerance,"tolerance","Required relative accuracy (0.001).",1,"",false); 00297 argDouble(length,"length","Length of field map (mm).",mm,"",false); 00298 argDouble(width,"width","Width of rectangular field map (mm).",mm,"",false); 00299 argDouble(height,"height","Height of rectangular field map (mm).",mm,"",false); 00300 argDouble(radius,"radius","Radius of cylindrical field map (mm).",mm,"",false); 00301 argDouble(tmin,"tmin","Minimum value of t (ns).",mm,"",false); 00302 argDouble(tmax,"tmax","Maximum value of t (ns).",mm,"",false); 00303 }
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.
00312 { 00313 G4String thisname = parentName+getName(); 00314 00315 // get globalRotation and globalPosition 00316 G4RotationMatrix *globalRotation = 0; 00317 if(relativeRotation && parentRotation) { 00318 globalRotation = 00319 new G4RotationMatrix(*parentRotation * *relativeRotation); 00320 } else if(relativeRotation) { 00321 globalRotation = relativeRotation; 00322 } else if(parentRotation) { 00323 globalRotation = parentRotation; 00324 } 00325 G4ThreeVector globalPosition(relativePosition + parentPosition); 00326 if(parentRotation) 00327 globalPosition = *parentRotation * relativePosition + 00328 parentPosition; 00329 00330 FieldExprPlacement *p = new FieldExprPlacement(map,globalRotation, 00331 globalPosition,factorB,factorE,timeOffset); 00332 BLGlobalField::getObject()->addElementField(p); 00333 00334 printf("BLMappedMagnet::Construct %s parent=%s relZ=%.1f\n", 00335 thisname.c_str(),parentName.c_str(),relativePosition[2]); 00336 }
G4double BLCMDfieldexpr::getLength | ( | ) | [inline, virtual] |
G4double BLCMDfieldexpr::getWidth | ( | ) | [inline, virtual] |
G4double BLCMDfieldexpr::getHeight | ( | ) | [inline, virtual] |
G4bool BLCMDfieldexpr::isOK | ( | ) | [inline, virtual] |
bool BLCMDfieldexpr::isOutside | ( | G4ThreeVector & | local, | |
G4double | tolerance | |||
) | [inline, virtual] |
void BLCMDfieldexpr::generatePoints | ( | int | npoints, | |
std::vector< G4ThreeVector > & | v | |||
) | [inline, virtual] |
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().
00378 { 00379 G4double err[6],maxField[6]; 00380 for(int i=0; i<6; ++i) 00381 err[i] = maxField[i] = 0.0; 00382 00383 // find the max error at the center of every grid box 00384 if(radius > 0.0) { 00385 double dR = radius/(nR-1); 00386 double dZ = length/(nZ-1); 00387 for(double z=(-length+dZ)/2.0; z<length/2.0; z+=dZ) { 00388 for(double r=dR/2.0; r<radius; r+=dR) { 00389 G4double point[4],field1[6],field2[6]; 00390 point[0] = r; 00391 point[1] = 0.0; 00392 point[2] = z; 00393 point[3] = 0.0; // map has no time dependence 00394 map->getFieldValue(point,field1,1.0,1.0); 00395 p->GetFieldValue(point,field2); 00396 for(int i=0; i<6; ++i) { 00397 if(fabs(field2[i]) > maxField[i]) 00398 maxField[i] = fabs(field2[i]); 00399 if(fabs(field2[i]-field1[i]) > err[i]) 00400 err[i] = fabs(field2[i]-field1[i]); 00401 } 00402 } 00403 } 00404 } else { 00405 double dX = width/(nX-1); 00406 double dY = height/(nY-1); 00407 double dZ = length/(nZ-1); 00408 for(double z=(-length+dZ)/2.0; z<length/2.0; z+=dZ) { 00409 for(double y=(-height+dY)/2.0; y<height/2.0; y+= dY) { 00410 for(double x=(-width+dX)/2.0; x<width/2.0; x+=dX) { 00411 G4double point[4],field1[6],field2[6]; 00412 point[0] = x; 00413 point[1] = y; 00414 point[2] = z; 00415 point[3] = 0.0; // map has no time dependence, yet 00416 map->getFieldValue(point,field1,1.0,1.0); 00417 p->GetFieldValue(point,field2); 00418 //@ printf("fieldexpr x,y,z=%.3f,%.3f,%.3f B1=%.4f,%.4f,%.4f B2=%.4f,%.4f,%.4f\n",x,y,z,field1[0]/tesla,field1[1]/tesla,field1[2]/tesla,field2[0]/tesla,field2[1]/tesla,field2[2]/tesla); 00419 00420 for(int i=0; i<6; ++i) { 00421 if(fabs(field2[i]) > maxField[i]) 00422 maxField[i] = fabs(field2[i]); 00423 if(fabs(field2[i]-field1[i]) > err[i]) 00424 err[i] = fabs(field2[i]-field1[i]); 00425 } 00426 } 00427 } 00428 } 00429 } 00430 00431 // denominator is largest component, not largest norm -- good enough. 00432 if(maxField[1] > maxField[0]) maxField[0] = maxField[1]; 00433 if(maxField[2] > maxField[0]) maxField[0] = maxField[2]; 00434 if(maxField[4] > maxField[3]) maxField[3] = maxField[4]; 00435 if(maxField[5] > maxField[3]) maxField[3] = maxField[5]; 00436 if(err[1] > err[0]) err[0] = err[1]; 00437 if(err[2] > err[0]) err[0] = err[2]; 00438 if(err[4] > err[3]) err[3] = err[4]; 00439 if(err[5] > err[3]) err[3] = err[5]; 00440 G4double errB=0.0, errE=0.0; 00441 if(maxField[0] > 0.000001) errB = err[0]/maxField[0]; 00442 if(maxField[3] > 0.000001) errE = err[3]/maxField[3]; 00443 return (errB>errE ? errB : errE); 00444 }
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().
00124 { if(!e.isOK()) { 00125 printf("***fieldexpr '%s': Invalid expression\n", 00126 getName().c_str()); 00127 exit(99); 00128 } 00129 }
friend class FieldComputation [friend] |
Referenced by command().
G4double BLCMDfieldexpr::factorB [private] |
Referenced by BLCMDfieldexpr(), construct(), and defineNamedArgs().
G4double BLCMDfieldexpr::factorE [private] |
Referenced by BLCMDfieldexpr(), construct(), and defineNamedArgs().
G4double BLCMDfieldexpr::timeOffset [private] |
Referenced by BLCMDfieldexpr(), construct(), and defineNamedArgs().
G4String BLCMDfieldexpr::Bx [private] |
Referenced by BLCMDfieldexpr(), defineNamedArgs(), and FieldComputation::GetFieldValue().
G4String BLCMDfieldexpr::By [private] |
Referenced by BLCMDfieldexpr(), defineNamedArgs(), and FieldComputation::GetFieldValue().
G4String BLCMDfieldexpr::Bz [private] |
Referenced by BLCMDfieldexpr(), defineNamedArgs(), and FieldComputation::GetFieldValue().
G4String BLCMDfieldexpr::Br [private] |
Referenced by BLCMDfieldexpr(), defineNamedArgs(), and FieldComputation::GetFieldValue().
G4String BLCMDfieldexpr::Ex [private] |
Referenced by BLCMDfieldexpr(), defineNamedArgs(), and FieldComputation::GetFieldValue().
G4String BLCMDfieldexpr::Ey [private] |
Referenced by BLCMDfieldexpr(), defineNamedArgs(), and FieldComputation::GetFieldValue().
G4String BLCMDfieldexpr::Ez [private] |
Referenced by BLCMDfieldexpr(), defineNamedArgs(), and FieldComputation::GetFieldValue().
G4String BLCMDfieldexpr::Er [private] |
Referenced by BLCMDfieldexpr(), defineNamedArgs(), and FieldComputation::GetFieldValue().
G4String BLCMDfieldexpr::time [private] |
Referenced by BLCMDfieldexpr(), defineNamedArgs(), and handleTimeDependence().
G4double BLCMDfieldexpr::length [private] |
Referenced by BLCMDfieldexpr(), command(), defineNamedArgs(), and maxError().
G4double BLCMDfieldexpr::width [private] |
Referenced by BLCMDfieldexpr(), command(), defineNamedArgs(), and maxError().
G4double BLCMDfieldexpr::height [private] |
Referenced by BLCMDfieldexpr(), command(), defineNamedArgs(), and maxError().
G4double BLCMDfieldexpr::radius [private] |
Referenced by BLCMDfieldexpr(), command(), defineNamedArgs(), FieldComputation::GetFieldValue(), and maxError().
G4double BLCMDfieldexpr::tmin [private] |
Referenced by BLCMDfieldexpr(), defineNamedArgs(), and handleTimeDependence().
G4double BLCMDfieldexpr::tmax [private] |
Referenced by BLCMDfieldexpr(), defineNamedArgs(), and handleTimeDependence().
int BLCMDfieldexpr::nX [private] |
Referenced by BLCMDfieldexpr(), command(), defineNamedArgs(), and maxError().
int BLCMDfieldexpr::nY [private] |
Referenced by BLCMDfieldexpr(), command(), defineNamedArgs(), and maxError().
int BLCMDfieldexpr::nZ [private] |
Referenced by BLCMDfieldexpr(), command(), defineNamedArgs(), and maxError().
int BLCMDfieldexpr::nR [private] |
Referenced by BLCMDfieldexpr(), command(), defineNamedArgs(), and maxError().
int BLCMDfieldexpr::nT [private] |
Referenced by BLCMDfieldexpr(), defineNamedArgs(), and handleTimeDependence().
G4double BLCMDfieldexpr::tolerance [private] |
Referenced by BLCMDfieldexpr(), command(), and defineNamedArgs().
BLFieldMap* BLCMDfieldexpr::map [private] |
Referenced by BLCMDfieldexpr(), command(), construct(), handleTimeDependence(), and maxError().