207{
210
211 G4bool IsParallel = false ;
212 G4bool IsConverged = false ;
213
215
217
219 {
221 {
226 }
228 }
229 else
230 {
232 {
233 distance[j] = kInfinity;
235 isvalid[j] = false;
236 gxx[j].
set(kInfinity, kInfinity, kInfinity);
237 }
238 }
239
242
243#ifdef G4TWISTDEBUG
246#endif
247
249
250
251
255 G4bool tmpisvalid = false ;
256
257 std::vector<Intersection> xbuf ;
259
260
261
263
264 G4double phixz = fPhiTwist * ( p.
x() * v.
z() - p.
z() * v.
x() ) ;
265 G4double phiyz = fPhiTwist * ( p.
y() * v.
z() - p.
z() * v.
y() ) ;
266
267
268
269
271 {
272 if ( std::fabs(p.
z()) <= L )
273 {
274 phi = p.
z() * fPhiTwist / L ;
275 u = (fDy1*(4*(-(fdeltaY*phi*v.
x()) + fPhiTwist*p.
y()*v.
x()
276 + fdeltaX*phi*v.
y() - fPhiTwist*p.
x()*v.
y())
277 + ((fDx3plus1 + fDx4plus2)*fPhiTwist
278 + 2*(fDx3minus1 + fDx4minus2)*phi)
279 *(v.
y()*std::cos(phi) - v.
x()*std::sin(phi))))
280 /(fPhiTwist*(4*fDy1* v.
x() - (fa1md1 + 4*fDy1*fTAlph)*v.
y())
281 *std::cos(phi) + fPhiTwist*(fa1md1*v.
x()
282 + 4*fDy1*(fTAlph*v.
x() + v.
y()))*std::sin(phi));
288
289 xbuf.push_back(xbuftmp) ;
290 }
291 else
292 {
293 distance[0] = kInfinity;
294 gxx[0].
set(kInfinity,kInfinity,kInfinity);
295 isvalid[0] = false ;
298 areacode[0], isvalid[0],
299 0, validate, &gp, &gv);
300 return 0;
301 }
302 }
303 else
304 {
305
307
308 c[7] = 57600*
309 fDy1*(fa1md1*phiyz +
310 fDy1*(-4*phixz + 4*fTAlph*phiyz
311 + (fDx3plus1 + fDx4plus2)*fPhiTwist*v.
z())) ;
312 c[6] = -57600*
313 fDy1*(4*fDy1*(phiyz + 2*fDz*v.
x() + fTAlph*(phixz - 2*fDz*v.
y()))
314 - 2*fDy1*(2*fdeltaX + fDx3minus1 + fDx4minus2
315 - 2*fdeltaY*fTAlph)*v.
z()
316 + fa1md1*(phixz - 2*fDz*v.
y() + fdeltaY*v.
z()));
317 c[5] = 4800*
318 fDy1*(fa1md1*(-5*phiyz - 24*fDz*v.
x() + 12*fdeltaX*v.
z()) +
319 fDy1*(20*phixz - 4*(5*fTAlph*phiyz + 24*fDz*fTAlph*v.
x()
320 + 24*fDz*v.
y()) + (48*fdeltaY + (fDx3plus1 + fDx4plus2)
321 *fPhiTwist + 48*fdeltaX*fTAlph)*v.
z()));
322 c[4] = 4800*
323 fDy1*(fa1md1*(phixz - 10*fDz*v.
y() + 5*fdeltaY*v.
z())
324 + 2*fDy1*(2*phiyz + 20*fDz*v.
x()
325 + (-10*fdeltaX + fDx3minus1 + fDx4minus2)*v.
z()
326 + 2*fTAlph*(phixz - 10*fDz*v.
y() + 5*fdeltaY*v.
z())));
327 c[3] = -96*
328 fDy1*(-(fa1md1*(phiyz + 100*fDz*v.
x() - 50*fdeltaX*v.
z()))
329 + fDy1*(4*phixz - 400*fDz*v.
y()
330 + (200*fdeltaY - (fDx3plus1 + fDx4plus2)*fPhiTwist)*v.
z()
331 - 4*fTAlph*(phiyz + 100*fDz*v.
x() - 50*fdeltaX*v.
z())));
332 c[2] = 32*
333 fDy1*(4*fDy1*(7*fTAlph*phixz + 7*phiyz - 6*fDz*v.
x() + 6*fDz*fTAlph*v.
y())
334 + 6*fDy1*(2*fdeltaX+fDx3minus1+fDx4minus2-2*fdeltaY*fTAlph)*v.
z()
335 + fa1md1*(7*phixz + 6*fDz*v.
y() - 3*fdeltaY*v.
z()));
336 c[1] = -8*
337 fDy1*(fa1md1*(-9*phiyz - 56*fDz*v.
x() + 28*fdeltaX*v.
z())
338 + 4*fDy1*(9*phixz - 9*fTAlph*phiyz - 56*fDz*fTAlph*v.
x()
339 - 56*fDz*v.
y() + 28*(fdeltaY + fdeltaX*fTAlph)*v.
z()));
340 c[0] = 72*
341 fDy1*(fa1md1*(2*fDz*v.
y() - fdeltaY*v.
z())
342 + fDy1*(-8*fDz*v.
x() + 8*fDz*fTAlph*v.
y()
343 + 4*fdeltaX*v.
z() - 4*fdeltaY*fTAlph*v.
z()));
344
345#ifdef G4TWISTDEBUG
346 G4cout <<
"coef = " << c[0] <<
" "
347 << c[1] << " "
348 << c[2] << " "
349 << c[3] << " "
350 << c[4] << " "
351 << c[5] << " "
352 << c[6] << " "
354#endif
355
358
359 for (register int i = 0 ; i<num ; i++ )
360 {
361 if ( si[i]==0.0 )
362 {
363#ifdef G4TWISTDEBUG
364 G4cout <<
"Solution " << i <<
" : " << srd[i] <<
G4endl ;
365#endif
366 phi = std::fmod(srd[i] , pihalf) ;
367 u = (fDy1*(4*(phiyz + 2*fDz*phi*v.
y() - fdeltaY*phi*v.
z())
368 - ((fDx3plus1 + fDx4plus2)*fPhiTwist
369 + 2*(fDx3minus1 + fDx4minus2)*phi)*v.
z()*std::sin(phi)))
370 /(fPhiTwist*v.
z()*(4*fDy1*std::cos(phi)
371 + (fa1md1 + 4*fDy1*fTAlph)*std::sin(phi)));
377
378 xbuf.push_back(xbuftmp) ;
379
380#ifdef G4TWISTDEBUG
381 G4cout <<
"solution " << i <<
" = " << phi <<
" , " << u <<
G4endl ;
382#endif
383 }
384 }
385 }
386
387 nxx = xbuf.size() ;
388
395
396 for ( register size_t k = 0 ; k<xbuf.size() ; k++ )
397 {
398#ifdef G4TWISTDEBUG
399 G4cout <<
"Solution " << k <<
" : "
400 << "reconstructed phiR = " << xbuf[k].phi
401 <<
", uR = " << xbuf[k].u <<
G4endl ;
402#endif
403
404 phi = xbuf[k].phi ;
405 u = xbuf[k].u ;
406
407 IsConverged = false ;
408
409 for ( register int i = 1 ; i<maxint ; i++ )
410 {
411 xxonsurface = SurfacePoint(phi,u) ;
412 surfacenormal = NormAng(phi,u) ;
413
415 deltaX = ( tmpxx - xxonsurface ).mag() ;
416 theta = std::fabs(std::acos(v*surfacenormal) - pihalf) ;
417 if ( theta < 0.001 )
418 {
419 factor = 50 ;
420 IsParallel = true ;
421 }
422 else
423 {
424 factor = 1 ;
425 }
426
427#ifdef G4TWISTDEBUG
428 G4cout <<
"Step i = " << i <<
", distance = " << tmpdist
429 <<
", " << deltaX <<
G4endl ;
431#endif
432
433 GetPhiUAtX(tmpxx, phi, u) ;
434
435
436#ifdef G4TWISTDEBUG
437 G4cout <<
"approximated phi = " << phi <<
", u = " << u <<
G4endl ;
438#endif
439
440 if ( deltaX <= factor*ctol ) { IsConverged = true ; break ; }
441
442 }
443
444 if ( std::fabs(tmpdist)<ctol ) { tmpdist = 0 ; }
445
446#ifdef G4TWISTDEBUG
447 G4cout <<
"refined solution " << phi <<
" , " << u <<
G4endl ;
450#endif
451
452 tmpisvalid = false ;
453
454 if ( IsConverged )
455 {
457 {
458 tmpareacode = GetAreaCode(tmpxx);
460 {
461 if (tmpdist >= 0) tmpisvalid = true;
462 }
463 }
465 {
466 tmpareacode = GetAreaCode(tmpxx, false);
468 {
469 if (tmpdist >= 0) { tmpisvalid = true; }
470 }
471 }
472 else
473 {
474 G4Exception(
"G4TwistTrapAlphaSide::DistanceToSurface()",
476 "Feature NOT implemented !");
477 }
478 }
479 else
480 {
481 tmpdist = kInfinity;
482 tmpisvalid = false ;
483 }
484
485
486
487 xbuf[k].xx = tmpxx ;
488 xbuf[k].distance = tmpdist ;
489 xbuf[k].areacode = tmpareacode ;
490 xbuf[k].isvalid = tmpisvalid ;
491
492 }
493
495
496#ifdef G4TWISTDEBUG
499#endif
500
501
502
504 xbuf.end() );
505
506
507
508
509 G4int nxxtmp = xbuf.size() ;
510
511 if ( nxxtmp<2 || IsParallel )
512 {
513
514#ifdef G4TWISTDEBUG
516#endif
517
518 phi = fPhiTwist/2 ;
519 u = 0 ;
520
526
527 xbuf.push_back(xbuftmp) ;
528
529#ifdef G4TWISTDEBUG
531#endif
532
533 phi = -fPhiTwist/2 ;
534 u = 0 ;
535
541
542 xbuf.push_back(xbuftmp) ;
543
544 for ( register size_t k = nxxtmp ; k<xbuf.size() ; k++ )
545 {
546
547#ifdef G4TWISTDEBUG
548 G4cout <<
"Solution " << k <<
" : "
549 << "reconstructed phiR = " << xbuf[k].phi
550 <<
", uR = " << xbuf[k].u <<
G4endl ;
551#endif
552
553 phi = xbuf[k].phi ;
554 u = xbuf[k].u ;
555
556 IsConverged = false ;
557
558 for ( register int i = 1 ; i<maxint ; i++ )
559 {
560 xxonsurface = SurfacePoint(phi,u) ;
561 surfacenormal = NormAng(phi,u) ;
563 deltaX = ( tmpxx - xxonsurface ).mag();
564 theta = std::fabs(std::acos(v*surfacenormal) - pihalf);
565 if ( theta < 0.001 )
566 {
567 factor = 50 ;
568 }
569 else
570 {
571 factor = 1 ;
572 }
573
574#ifdef G4TWISTDEBUG
575 G4cout <<
"Step i = " << i <<
", distance = " << tmpdist
576 <<
", " << deltaX <<
G4endl
577 <<
"X = " << tmpxx <<
G4endl ;
578#endif
579
580 GetPhiUAtX(tmpxx, phi, u) ;
581
582
583#ifdef G4TWISTDEBUG
584 G4cout <<
"approximated phi = " << phi <<
", u = " << u <<
G4endl ;
585#endif
586
587 if ( deltaX <= factor*ctol ) { IsConverged = true ; break ; }
588
589 }
590
591 if ( std::fabs(tmpdist)<ctol ) { tmpdist = 0; }
592
593#ifdef G4TWISTDEBUG
594 G4cout <<
"refined solution " << phi <<
" , " << u <<
G4endl ;
597#endif
598
599 tmpisvalid = false ;
600
601 if ( IsConverged )
602 {
604 {
605 tmpareacode = GetAreaCode(tmpxx);
607 {
608 if (tmpdist >= 0) { tmpisvalid = true; }
609 }
610 }
612 {
613 tmpareacode = GetAreaCode(tmpxx, false);
615 {
616 if (tmpdist >= 0) { tmpisvalid = true; }
617 }
618 }
619 else
620 {
621 G4Exception(
"G4TwistedBoxSide::DistanceToSurface()",
623 "Feature NOT implemented !");
624 }
625 }
626 else
627 {
628 tmpdist = kInfinity;
629 tmpisvalid = false ;
630 }
631
632
633
634 xbuf[k].xx = tmpxx ;
635 xbuf[k].distance = tmpdist ;
636 xbuf[k].areacode = tmpareacode ;
637 xbuf[k].isvalid = tmpisvalid ;
638
639 }
640 }
641
642
644
645
647 xbuf.end() );
648
649#ifdef G4TWISTDEBUG
652#endif
653
654 nxx = xbuf.size() ;
655
656 for ( register size_t i = 0 ; i<xbuf.size() ; i++ )
657 {
658 distance[i] = xbuf[i].distance;
660 areacode[i] = xbuf[i].areacode ;
661 isvalid[i] = xbuf[i].isvalid ;
662
664 isvalid[i], nxx, validate, &gp, &gv);
665#ifdef G4TWISTDEBUG
666 G4cout <<
"element Nr. " << i
667 << ", local Intersection = " << xbuf[i].xx
668 << ", distance = " << xbuf[i].distance
669 << ", u = " << xbuf[i].u
670 << ", phi = " << xbuf[i].phi
671 << ", isvalid = " << xbuf[i].isvalid
673#endif
674
675 }
676
677#ifdef G4TWISTDEBUG
679 G4cout << nxx <<
" possible physical solutions found" <<
G4endl ;
680 for (
G4int k= 0 ; k< nxx ; k++ )
681 {
682 G4cout <<
"global intersection Point found: " << gxx[k] <<
G4endl ;
685 }
686#endif
687
688 return nxx ;
689}
G4bool DistanceSort(const Intersection &a, const Intersection &b)
G4bool EqualIntersection(const Intersection &a, const Intersection &b)
G4DLLIMPORT std::ostream G4cout
G4int FindRoots(G4double *op, G4int degree, G4double *zeror, G4double *zeroi)
G4int GetAreacode(G4int i) const
G4double GetDistance(G4int i) const
void SetCurrentStatus(G4int i, G4ThreeVector &xx, G4double &dist, G4int &areacode, G4bool &isvalid, G4int nxx, EValidate validate, const G4ThreeVector *p, const G4ThreeVector *v=0)
G4bool IsValid(G4int i) const
G4ThreeVector GetXX(G4int i) const
void ResetfDone(EValidate validate, const G4ThreeVector *p, const G4ThreeVector *v=0)
static const G4int sOutside
G4ThreeVector ComputeLocalDirection(const G4ThreeVector &gp) const
G4bool IsInside(G4int areacode, G4bool testbitmode=false) const
G4ThreeVector ComputeLocalPoint(const G4ThreeVector &gp) const
G4bool IsOutside(G4int areacode) const
CurrentStatus fCurStatWithV
G4double DistanceToPlaneWithV(const G4ThreeVector &p, const G4ThreeVector &v, const G4ThreeVector &x0, const G4ThreeVector &n0, G4ThreeVector &xx)
G4ThreeVector ComputeGlobalPoint(const G4ThreeVector &lp) const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)