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5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 //
  2 // ********************************************************************
  3 // * License and Disclaimer                                           *
  4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.                             *
 10 // *                                                                  *
 11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                                                  *
 18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // ********************************************************************
 25 
 26 //------------------------------------------------
 27 // The Geant4 Virtual Monte Carlo package
 28 // Copyright (C) 2007 - 2015 Ivana Hrivnacova
 29 // All rights reserved.
 30 //
 31 // For the licensing terms see geant4_vmc/LICENSE.
 32 // Contact: root-vmc@cern.ch
 33 //-------------------------------------------------
 34 
 35 /// \file G4FieldBuilder.h
 36 /// \brief Definition of the G4FieldBuilder class
 37 ///
 38 /// \author I. Hrivnacova; IJCLab, Orsay
 39 
 40 #ifndef G4FIELDBUILDER_HH
 41 #define G4FIELDBUILDER_HH
 42 
 43 #include "G4Cache.hh"
 44 #include "G4FieldParameters.hh"
 45 #include "globals.hh"
 46 
 47 #include <vector>
 48 
 49 class G4FieldBuilderMessenger;
 50 class G4FieldSetup;
 51 class G4LogicalVolume;
 52 class G4EquationOfMotion;
 53 class G4MagIntegratorStepper;
 54 
 55 /// \brief The manger class for building magnetic or other field
 56 /// using the configuration in field parameters.
 57 ///
 58 /// Purpose: Provide a single 'place' to configure field & integration
 59 ///
 60 /// - It can configure a global field, and field(s) local to a (logical) volume
 61 /// - The parameter values can be configured by the user (else use a default)
 62 /// - They can be set/changed via a messenger provided or in the code
 63 ///      of the user detector construciton
 64 /// - It retains ownership of the following object(s):
 65 ///      field parameters and field setups, field
 66 ///
 67 ///  Note MT: an object of the builder class should be created on master only
 68 ///      (in DetectorConstruction constructor)
 69 ///      The functions SetGlobal/LocalField and ConstructFieldSetup should
 70 ///      be called on workers (in DetectorConstruction::ConstructSDandField )
 71 ///
 72 ///  This design/implementation covers the most common use cases.
 73 ///  It cannot be used to create some complex setups such as
 74 ///    - equations templated on the field type,
 75 ///    - steppers/drivers templated on the equation and field types.
 76 ///
 77 /// \author I. Hrivnacova; IJCLab, Orsay
 78 
 79 class G4FieldBuilder
 80 {
 81  public:
 82   /// Destructor
 83   ~G4FieldBuilder();
 84 
 85   // Static access method
 86   //
 87 
 88   /// Create the class instance, if it does not exist,
 89   /// and return it on the next calls.
 90   static G4FieldBuilder* Instance();
 91 
 92   /// Return the information if an instance exists
 93   static G4bool IsInstance();
 94 
 95   // Functions for constructing field setup
 96   //
 97 
 98   /// Create local magnetic field parameters (configuration) which can be then
 99   /// configured by the user via UI commands.
100   /// The parameters are used in geometry only if a local magnetic field is
101   /// associated with the volumes with the given name
102   G4FieldParameters* CreateFieldParameters(const G4String& fieldVolName);
103 
104   /// Construct setups for all registered fields.
105   void ConstructFieldSetup();
106 
107   /// Update magnetic field.
108   /// This function must be called if the field parameters were changed
109   /// in other than PreInit> phase.
110   void UpdateField();
111 
112   /// Reinitialize if geometry has been modified.
113   /// This function is called by G4RunManager during ReinitializeGeometry()
114   void Reinitialize();
115 
116   // Set methods
117   //
118 
119   /// Default field type is set to kMagnetic;
120   /// this function should be called for other than magnetic field
121   /// in order to update the default equation and stepper types.
122   void SetFieldType(G4FieldType fieldType);
123 
124   // Set or reset the global field.
125   // Update field objects, if the field was already constructed.
126   // If warn, issue a warning if the previous field is deleted.
127   void SetGlobalField(G4Field* field, G4bool warn = false);
128 
129   /// Register the local field in the map.
130   /// Update field objects, if the field was already constructed.
131   /// If warn, issue a warning if the previous field is deleted.
132   /// The field is propagated to all volume daughters regardless
133   /// if they have already assigned a field manager or not.
134   /// When multiple local fields are defined (by calling this function
135   /// multiple times), they will be applied in the order they were set.
136   void SetLocalField(G4Field* field, G4LogicalVolume* lv, G4bool warn = false);
137 
138   /// Set user equation of motion
139   void SetUserEquationOfMotion(
140     G4EquationOfMotion* equation, G4String volumeName = "");
141 
142   /// Set user stepper
143   void SetUserStepper(
144     G4MagIntegratorStepper* stepper, G4String volumeName = "");
145 
146   /// Set verbose level
147   void SetVerboseLevel(G4int value);
148 
149   // Get methods
150   //
151 
152   /// Get field parameters with the given volumeName.
153   /// Return global field parameters, if volume name is empty.
154   G4FieldParameters* GetFieldParameters(const G4String& volumeName = "") const;
155 
156  private:
157   /// Default constructor
158   G4FieldBuilder();
159   /// Not implemented
160   G4FieldBuilder(const G4FieldBuilder& right) = delete;
161   /// Not implemented
162   G4FieldBuilder& operator=(const G4FieldBuilder& right) = delete;
163 
164   // Methods
165 
166   /// Get field parameters with the given volumeName or create them if they
167   /// do not exist yet
168   G4FieldParameters* GetOrCreateFieldParameters(const G4String& volumeName);
169 
170   /// Get field setup with the given logical volume
171   G4FieldSetup* GetFieldSetup(G4LogicalVolume* lv);
172 
173   /// Create magnetic, electromagnetic or gravity field setup
174   void CreateFieldSetup(G4Field* field,
175     G4FieldParameters* fieldParameters, G4LogicalVolume* lv);
176 
177   /// Construct Geant4 global magnetic field setup
178   void ConstructGlobalField();
179 
180   /// Construct Geant4 local magnetic field setups from the local fields map
181   void ConstructLocalFields();
182 
183   /// Update all field setups
184   void UpdateFieldSetups();
185 
186   // helper methods
187   std::vector<G4FieldSetup*>& GetFieldSetups();
188   std::vector<std::pair<G4LogicalVolume*, G4Field*>>& GetLocalFields();
189 
190   // Data members
191 
192   /// Information if an instance exists
193   inline static G4ThreadLocal G4bool fgIsInstance { false };
194 
195   /// Messenger for this class
196   G4FieldBuilderMessenger* fMessenger = nullptr;
197 
198   /// Field parameters
199   std::vector<G4FieldParameters*> fFieldParameters;
200 
201   /// Field setups
202   G4Cache<std::vector<G4FieldSetup*>*> fFieldSetups;
203 
204   /// Registered global field
205   static G4ThreadLocal G4Field* fGlobalField;
206 
207   /// Registered local fields
208   G4Cache<std::vector<std::pair<G4LogicalVolume*, G4Field*>>*> fLocalFields;
209 
210   /// info if field objects were constructed
211   static G4ThreadLocal G4bool fIsConstructed;
212 
213   /// verbose level
214   G4int fVerboseLevel = 1;
215 };
216 
217 // inline methods
218 
219 inline G4bool G4FieldBuilder::IsInstance()
220 {
221   // Return the information if an instance exists
222   return fgIsInstance;
223 }
224 
225 inline void G4FieldBuilder::SetVerboseLevel(G4int value)
226 {
227   // Set verbose level
228   fVerboseLevel = value;
229 }
230 
231 inline std::vector<G4FieldSetup*>& G4FieldBuilder::GetFieldSetups()
232 {
233   // Return reference to field setups from G4Cache
234   return *fFieldSetups.Get();
235 }
236 
237 inline std::vector<std::pair<G4LogicalVolume*, G4Field*>>& G4FieldBuilder::GetLocalFields()
238 {
239   // Return reference to local fields map from G4Cache
240   return *fLocalFields.Get();
241 }
242 
243 #endif // G4FIELDBUILDER_HH
244