geant4 development team - indico

30
Geometry related development Evgueni Tcherniaev Geant4 development team National Research Tomsk State University

Upload: others

Post on 18-Apr-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Geant4 development team - Indico

Geometry related development

Evgueni TcherniaevGeant4 development team

National ResearchTomskStateUniversity

Page 2: Geant4 development team - Indico

Kind of (self-) introduction• The title “Geometry related development” means the development

and implementation of various utilities applicable to different 3D geometrical objects - points, vectors, polygons, surfaces, solids, etc. • Being at CERN I’ve participated in the following projects related to

SFT:- CLHEP- ROOT- GEANT4 (VecGeom)

• In this talk I will make a review of what has been done in the past, recent development and plans regarding to those projects

12 November 2018 Monday’s SFT group meeting 1

Page 3: Geant4 development team - Indico

CLHEP – Class Library for HEP• Past• Vector, till CLHEP 1.7 – ThreeVector, LorentzVector and operations on objects

of those classes • Evaluator – evaluation of algebraic expressions given as a string• Geometry – Point3D, Vector3D, Normal3D and Transform3D

• Recent• Revision of HepRotation::getAxis() – get axis of rotation

• No particular plans

12 November 2018 Monday’s SFT group meeting 2

Page 4: Geant4 development team - Indico

ROOT. Past

12 November 2018 Monday’s SFT group meeting 3

• Several hidden line / hidden surface removal algorithms have been originally developed for PAW/HIGZ and then adopted by ROOT (see class TPainter3dAlgorithms)

• Main feature of the algorithms – the result of a drawing is vector graphics

ROOT. Past

12 November 2018 Monday’s SFT group meeting 3

• Several hidden line / hidden surface removal algorithms have been originally developed for PAW/HIGZ and then adopted by ROOT (see class TPainter3dAlgorithms)

• Main feature of the algorithms – the result of a drawing is vector graphics

Page 5: Geant4 development team - Indico

ROOT – Implicit functions

12 November 2018 Monday’s SFT group meeting 4

• A 3D implicit function f(x,y,z) = 0 can be drawn as an iso-surface (method TF3)• It is based on the algorithm Marching Cubes 33

Page 6: Geant4 development team - Indico

Marching cubes

12 November 2018 Monday’s SFT group meeting 5

CERN-CN/95-17 Marching Cubes 33: Construction of Topologically Correct Isosurfaces

• Since then the preprint became a very popular citation in the articles on reconstruction of an isosurface from a 3D grid of values. A reference to it is present in Wikipedia.

• Marching cubes is a computer graphics algorithm for extracting a polygonal mesh of an isosurface from a three-dimensional grid of scalar values. It was published in the 1987 SIGGRAPH proceedings by William E. Lorensen and Harvey E. Cline. This paper is one of the most cited papers in the computer graphics field.

• In 1995 for I’ve implemented an improved version of the Marching Cubes algorithm for the PAW/HIGZ package. The improved algorithm resolves the ambiguities existing in the original method.

• The algorithm was published in a preprint:

Page 7: Geant4 development team - Indico

ROOT. Recent development and Plans

12 November 2018 Monday’s SFT group meeting 6

• Recent development- Code in TPainter3dAlgorithms has been revised, several problems have been

fixed- New options have been added for 3D histograms

• Plans- To provide a common with Geant4 package for Boolean operations on

polyhedra

Page 8: Geant4 development team - Indico

Geant4. Past• Development in scope of CLHEP• BooleanProcessor – Boolean operations on polyhedra

12 November 2018 Monday’s SFT group meeting 7

Page 9: Geant4 development team - Indico

Geant4 (VecGeom). Recent development• Improved calculation of Extent• G4BoundingEnvelope class and re-implementation of CalculateExtent()• Enhanced wrappers for VecGeom

• Revision of G4Solids• Revision of G4Box, G4Trd, G4Para, G4Trap, G4ExtrudedSolid

• Investigation in ATLAS geometry• Usage of Boolean solids• Improvements in Geant4

• Development in VecGeom

12 November 2018 Monday’s SFT group meeting 8

Page 10: Geant4 development team - Indico

Geant4 10.03. Improved calculation of Extent• Each Geant4 solid should have a member function CalculateExtent() which calculates the minimum and

maximum extent of the solid within the specified limits. This function is used for building the voxel structure - an auxiliary structure for acceleration of navigation

• To facilitate implementation of CalculateExtent() a new class G4BoundingEnvelope has been introduced. Bounding envelope is a sequence of 3D convex polygons that bounds the solid.

• G4BoundingEnvelope has it own method CalculateExtent(), so there is no longer need to have solid depended calculation of extent – a developer has to provide a method for calculation of Bounding envelope

• In Geant4-10.03 the CalculateExtent() method has been re-implemented for all the Geant4 solids and all wrappers for the VecGeom solids: it includes 15 wrappers, 10 CGS solids, 16 specific solids as well as reflected, scaled and Boolean constructs

• It has improved the voxel structure: reduced memory footprint and the optimization timeCMS setup:Total memory consumed for geometry optimization: 16418 kByte (5.8 MByte less – 16% less)Total CPU time elapsed for geometry optimization: 3.5 seconds (1.1 s faster)

ATLAS setup:Total memory consumed for geometry optimization: 272640 kByte (18 MByte less – 6% less)Total CPU time elapsed for geometry optimization: 89 seconds (21 s faster)

• It has also improved navigation performance (~2% measured in pure ray-tracing “geantino” tests)

12 November 2018 Monday’s SFT group meeting 9

Page 11: Geant4 development team - Indico

Impact to VecGeom

• Consistent calculation of extents in the Geant4 solids and corresponding wrappers has allowed construction of identical voxel structures when using Geant4 and Geant4+VecGeom• Having identical voxel structures, we can do more accurate

comparison of Geant4 and VecGeom performances• It also helped to improve calculation of the bounding boxes in the

VecGeom solids and spot & fix several bugs there

12 November 2018 Monday’s SFT group meeting 10

Page 12: Geant4 development team - Indico

Geant4 10.04. Revision of G4Solids• Most of the Geant4 solids were implemented a long time ago. We now have better hardware and

software• G4Box, G4Trd, G4Para, G4Trap, G4Orb have been revised• By request of CMS a specialized implementation for convex right prism have been added into

G4ExtrudedSolid• A collection of general purpose utilities for geometry related calculations has been implemented and

grouped in G4GeomTools class (~25 utilities are available at present time)

• All Geant4 solids have the following six methods which are used during tracking:� Inside(p), detects position of a point, can be “inside”, “on surface”, “outside”� SurfaceNormal(p), computes normal at a point of the surface� DistanceToIn(p), estimates distance to the surface from outside (will be referenced as Safety To In)� DistanceToOut(p), estimates distance to the surface from inside (will be referenced as Safety To Out)� DistanceToIn(p,v), computes exact distance to the surface along “v” from outside� DistanceToOut(p,v), computes exact distance to the surface along “v” from inside

12 November 2018 11Monday’s SFT group meeting

Page 13: Geant4 development team - Indico

Example: “std::max” instead of “if”

EInside G4Box::Inside(const G4ThreeVector& p) const

{

G4double dz = std::abs(p.z()) - fDz;

if (dz > delta) return kOutside;

G4double dx = std::abs(p.x()) - fDx;

if (dx > delta) return kOutside;

G4double dy = std::abs(p.y()) - fDy;

if (dy > delta) return kOutside;

if (dx > -delta || dy > -delta || dz > -delta) return kSurface;

return kInside;

} // possible number of comparisons 1 - 6

EInside G4Box::Inside(const G4ThreeVector& p) const

{

G4double dist = std::max(std::max(

std::abs(p.x()) - fDx,

std::abs(p.y()) - fDy),

std::abs(p.z()) - fDz);

if (dist > delta) return kOutside;

return (dist > -delta) ? kSurface : kInside;

} // possible number of comparisons 3 - 4

Speed-up up to 3 times!

12 November 2018 12Monday’s SFT group meeting

On modern platforms std::min(a,b), std::max(a,b) are not anymore templates but processor instructions

Page 14: Geant4 development team - Indico

Example: usage of temporary variablesG4double tmin;G4double tmax;

G4double invx = (v.x() == 0) ? DBL_MAX : -1./v.x();

G4double tx1 = (p.x() + fDx)*invx;G4double tx2 = (p.x() - fDx)*invx;

tmin = std::min(tx1,tx2);tmax = std::max(tx1,tx2);

G4double invy = (v.y() == 0) ? DBL_MAX : -1./v.y();G4double ty1 = (p.y() + fDy)*invy;G4double ty2 = (p.y() - fDy)*invy;tmin = std::max(tmin, std::min(ty1,ty2));

tmax = std::min(tmax, std::max(ty1,ty2));

G4double invz = (v.z() == 0) ? DBL_MAX : -1./v.z();G4double tz1 = (p.z() + fDz)*invz;G4double tz2 = (p.z() - fDz)*invz;

tmin = std::max(tmin, std::min(tz1,tz2));

tmax = std::min(tmax, std::max(tz1,tz2));

G4double invx = (v.x() == 0) ? DBL_MAX : -1./v.x();

G4double tx1 = (p.x() + fDx)*invx;G4double tx2 = (p.x() - fDx)*invx;

G4double txmin = std::min(tx1,tx2);G4double txmax = std::max(tx1,tx2);

G4double invy = (v.y() == 0) ? DBL_MAX : -1./v.y();G4double ty1 = (p.y() + fDy)*invy;G4double ty2 = (p.y() - fDy)*invy;G4double tymin = std::max(txmin, std::min(ty1,ty2));

G4double tymax = std::min(txmax, std::max(ty1,ty2));

G4double invz = (v.z() == 0) ? DBL_MAX : -1./v.z();G4double tz1 = (p.z() + fDz)*invz;G4double tz2 = (p.z() - fDz)*invz;

G4double tmin = std::max(tymin, std::min(tz1,tz2));

G4double tmax = std::min(tymax, std::max(tz1,tz2));

Speed-up by 2 times!

12 November 2018 13Monday’s SFT group meeting

Page 15: Geant4 development team - Indico

“Trapezoid like” solidsRevision of the code has substantially improved the performance of the “trapezoid like” solidsMeasurement has been done for 100 million calls on MacBook Pro, 2.7 GHz Intel Core i5

Type 1 – rectangular YZ cross-section + symmetrical X-sidesType 2 – just rectangular YZ cross-section

G4Boxold new

G4Trdold new

G4Paraold new

G4Trapold new

InsidekInsidekSurfacekOutside

0.39 s1.09 s0.77 s

0.39 s0.39 s0.32 s

0.58 s1.51 s0.89 s

0.51 s0.51 s0.40 s

0.48 s1.27 s0.78 s

0.59 s0.60 s0.50 s

1.06 s1.37 s0.88 s

Type 1 / Type2 / General0.47 / 0.61 /0.71 s0.47 / 0.61 /0.70 s0.40 / 0.56 /0.60 s

Safety to InSafety to Out

0.35 s0.52 s

0.31 s0.34 s

2.13 s1.16 s

0.42 s0.45 s

0.94 s0.91 s

0.50 s0.52 s

0.81 s0.84 s

0.39 / 0.58 / 0.60 s0.42 / 0.58 / 0.63 s

Distance To InDistance To Out

2.96 s3.61 s

1.15 s1.79 s

3.21 s4.53 s

2.07 s2.59 s

2.26 s4.06 s

2.15 s2.75 s

4.03 s4.49 s

2.48 s3.06 s

Surface Normal 1.62 s 0.60 s 3.22 s 1.59 s 2.27 s 1.71 s 1.98 s 1.79 s

12 November 2018 14Monday’s SFT group meeting

Page 16: Geant4 development team - Indico

G4ExtrudedSolid: convex right prismPractically 100% of G4ExtrudedSolid shapes used by the LHC experiments are right prisms.

By request of CMS a specialized implementation for convex right prism have been added into G4ExtrudedSolid.

Below is the result of the benchmark for a hexagonal prism defined as G4Polyhedra and G4ExtrudedSolid

G4Polyhedra G4ExtrudedSoliddisturbed true

right prism right prism

Inside

kInside

kSurface

kOutside

18.98 s

13.91 s

0.74 -10.32 s

10.32 s

7.82 s

1.90 - 5.08 s

1.17 s

1.16 s

1.08 s

Safety to In

Safety to Out

19.16 s

18.59 s

2.06 s

160.50 s

0.95 s

0.95 s

Distance To In

Distance To Out

11.99 s

14.51 s

24.77 s

82.93 s

3.21 s

3.71 s

Surface Normal 19.77 s 38.64 s 2.63 s

12 November 2018 15Monday’s SFT group meeting

Page 17: Geant4 development team - Indico

Analysis of the ATLAS geometry

• The purpose of the analysis was to gather quantitative information on usage of Geant4 solids in the simulation of very complex detector• Gathered information was used:

• To enhance of Geant4/Geometry code by taking into account the specifics of the shapes used in the detector description

• To spot places, where the ATLAS geometry description can be optimized

• It also has improved the ability to predict the impact on the performance by various modifications in the code and in the geometry

12 November 2018 Monday’s SFT group meeting 16

This year I was engaged in the analysis of the ATLAS geometry description

Page 18: Geant4 development team - Indico

• A detailed quantitative statistics on the use of Geant4 in ATLAS has been gathered. Jira tickets dedicated to specific findings have been joined into an Epic collection ATLASSIM-3678 Improve the description of ATLAS in Geant4:• ATLASSIM-3556 Overlaps detected in MC16 geometry when running in standalone Geant4• ATLASSIM-3557 Optimizing Boolean solid use in ATLAS G4 geometry• ATLASSIM-3680 Revision of TMT• ATLASSIM-3778 Optimizing G4Polycone usage in ATLAS G4 geometry• ATLASSIM-3761 Performance comparison of four geant4 patches

• A number of improvements have been done in Geant4, namely in G4Box, G4DisplacedSolid, G4UnionSolid, G4AffineTransform and navigation code. The improvements will be included in Geant4 10.05

• Robustness of the ATLAS simulation code has been improved, all G4Exception warnings observed previously during simulation of 500 events have disappeared

• Several issues in the ATLAS geometry description have been fixed. An example of one of such issues will be explained on next slides

12 November 2018 Monday’s SFT group meeting 17

Result of the investigation

Page 19: Geant4 development team - Indico

Typical “bad practices” in using G4 Boolean solids• G4 Boolean solids (except G4MultiUnion) do not have internal optimization. The

time required for calculations in Boolean solids scales at best with the number of components (often worse than this) because of rather complicated logic of the calculations.• Below is a list of typical usage of Boolean solids that can degrade the

performance:1) Usage of Boolean subtraction to define holes

A better way to define holes is to define them as daughter volumes2) Describing a mother volume as a union of its daughter volumes

A better way to group volumes is to use G4AssemblyVolume3) Usage of a Boolean solid with complex shape for a lowest level volumes

A better way is to split the solid into simple parts and, if required, group the parts using G4AssemblyVolume

An extreme example of the case 3) was a Thermal Management Tile (TMT) which had been defined as a union of 6 G4Box solids and 36 G4Trap solids. Such a definition provoked a very big amount of calls to Inside()

12 November 2018 Monday’s SFT group meeting 18

Page 20: Geant4 development team - Indico

ATLAS Pixel detector – 112 staves

No. of staves: B-layer – 22, Layer 1 – 38, Layer 2 – 5212 November 2018 Monday’s SFT group meeting 19

Page 21: Geant4 development team - Indico

Pixel Stave Profile. Thermal Management Tile

12 November 2018 Monday’s SFT group meeting 20

Page 22: Geant4 development team - Indico

Redefinition of TMT: old vs new

Insidex 106

Normalx 106

SafetyToInx 106

SafetyToOutx 106

DistToInx 106

DistToOutx 106

G4Box 72.9 | 37.6 1.3 | 1.2 21.2 | 18.6 5.1 | 4.9 9.8 | 8.7 4.6 | 4.2

G4Trap 124.2 | 58.0_ 1.0 | 1.0 110.3 | 107.3 43.4 | 44.2 38.0 | 34.2 23.6 | 23.0

G4Trd 32.8 | 30.9 0.5 | 0.5 18.5 | 17.7 15.4 | 15.0 7.3 | 7.0 10.3 | 10.0

G4Tubs 150.4 | 146.9 9.1 | 8.9 165.9 | 162.4 122.5 | 121.0 65.2 | 64.4 45.3 | 44.7

G4Extruded 3.9 | 3.6 4.0 | 3.5 0.5 | 0.5 0.6 | 0.5 0.1 | 0.1

G4Polycone 317.0 | 314.8 1.6 | 1.6 21.6 | 21.3 115.7 | 114.8 9.0 | 8.9 39.0 | 38.8

G4Union 1168.7 | 33.9__ 3.4 | 1.8 23.0 | 16.6 6.4 | 4.7 13.8 | 8.7_ 22.7 | 3.2_

G4Subtraction 62.9 | 42.4 0.4 | 0.3 17.8 | 15.1 2.8 | 2.2 3.4 | 2.9 1.2 | 0.9

G4Displaced 1171.4 | 73.2__ 1.4 | 1.3 36.4 | 28.5 4.7 | 4.8 19.9 | 14.1 5.3 | 4.0

Total 3104 | 741_ 19 | 17 419 | 391 316 | 312 167 | 149 152 | 1284177 vs 1738 (G4Tubs – 548, G4Polycone – 500)

Profile comparison of two geometries:

aibuild027.cern.ch, SLC6, 1.20 GHzFullG4 simulation, Geant4 10.01.patch03.atlas07, 10 events

Average time per event: 238.0 s vs 232.9 s (~2% less)

12 November 2018 Monday’s SFT group meeting 21

Page 23: Geant4 development team - Indico

Redefinition of TMT• Redefinition of TMT has reduced the number of calls to Inside()by

several times:• more than 4 times in case of Geant4 10.01.patch03.atlas07

(741 x106 instead of 3104 x106)• more than 6 times in comparison with Geant4 10.01.patch03.atlas02

(741 x106 instead of 4700 x106)

• Combination of Geant4 10.01.patch03.atlas07 & new TMT may give 5% improvement in the performance of FullATLASG4 simulation• After redefinition of TMT, 60% of calls are to the methods of G4Tubs

and G4Polycone (31.5% + 28.8%)

12 November 2018 Monday’s SFT group meeting 22

Page 24: Geant4 development team - Indico

Geant4+VecGeom. Estimation of Surface Area by MC• The idea behind calculating the surface area of a 3D solid by Monte Carlo method is to

estimate the volume of a thin shell surrounding the surface and then divide the shell volume by its thickness

• Former algorithm in EstimateSurfaceArea() was usingSafety(p), i.e. DistanceToIn(p) or DistanceToOut(p), todecide whether a random point is inside or outside theshell

• The algorithm worked well for simple solids (Box, Trap,Tube, Cone, …), where calculation of safety returnsa real distance to the solid. However, in a general case,Safety(p) may return an underestimated distance, that, e is a half-thicknessin its order, may lead to substantial overestimation ofthe surface area.

12 November 2018 Monday’s SFT group meeting 23

Page 25: Geant4 development team - Indico

New algorithm, six-point method• To solve the issue, a new algorithm based on calculation of a distance along a predefined

approximative direction has been implemented.

• The approximative direction is defined by the position relative to the surface of six additional points surrounding the point in question. So, there are 26 = 64 predefined directions.

• New algorithm is using only methods whichreturn precise values:

- DistanceToIn(p,v), DistanceToOut(p,v) and- SurfaceNormal(p), where p is on the surface

• That makes new algorithm very robust,it estimates with a good precision the surfacearea of any solids, including complex ones

12 November 2018 Monday’s SFT group meeting 24

Page 26: Geant4 development team - Indico

Scaled TrdScale factors: (3, 2, 0.2)Surface Area: 2.60e+6

Big overestimation of the surface area in the 1st column is caused by the underestimation of safety for Scaled solids.

N Safety algorithm Six-point algorithm105 1.46e+7 (461%) 2.56e+6 (-1.43%)106 1.47e+7 (463%) 2.61e+6 ( 0.43%)107 1.47e+7 (463%) 2.60e+6 (-0.15%)

Time: 0.64 sec Time: 0.77 sec

12 November 2018 Monday’s SFT group meeting 25

Page 27: Geant4 development team - Indico

Intersection SolidSurface Area: 184752

Substantial deviation in the 1st column is caused by the underestimation of safety.

N Safety algorithm Six-point algorithm105 316009 (71.0%) 179511 (-2.83%)106 317748 (71.9%) 184611 (-0.07%)107 318210 (72.2%) 183625 (-0.6%)

Time: 0.72 sec Time: 0.74 sec

12 November 2018 Monday’s SFT group meeting 26

Page 28: Geant4 development team - Indico

Big Box Area: 240000Small Box Area: 2400

Subtraction of Big Box from Small Box

Null Solid

N Safety algorithm Six-point algorithm105 1231 0106 1236 0107 1225 0

Time: 0.68 sec Time: 0.70 sec

12 November 2018 Monday’s SFT group meeting 27

Page 29: Geant4 development team - Indico

Geant4+VecGeom. Plans• Continue support of Geant4 customers, it includes an assistance in

problem resolution, analysis the geometry description in different experiments • Implement missing solids in VecGeom

- Tetrahedron (done), Elliptical tube, Elliptical cone, Ellipsoid

• Revise Boolean processor for polyhedra

12 November 2018 Monday’s SFT group meeting 28

Page 30: Geant4 development team - Indico

Thank you!

12 November 2018 Monday’s SFT group meeting 29