Please fill out the following information to help in answering your question, and also see tips for posting code snippets. If you don’t provide this information it will take more time to help with your problem!
_Geant4 Version:_11.3
_Operating System:_Windows 11
_Compiler/Version:_Microsoft (R) C/C++ Optimizing Compiler Version 19.42.34433 for x86
_CMake Version:_3.31.0
As the title suggests, I am trying to create an electric field inside a specific volume within the world volume.
As you can see from the code below, I have added code that creates a magnetic field. Adding a magnetic field instead of an electric field gives exactly what I expect (the field inside the field volume), but adding an electric field instead causes g4 gui to crash.
Here is my detector construction. Please let me know if you need to see more:
#include "G4UniformElectricField.hh"
#include "G4UniformMagField.hh"
#include "G4SystemOfUnits.hh"
#include "G4FieldBuilder.hh"
#include "G4Material.hh"
VolumeConstruction::VolumeConstruction()
{
// create field builder
// this will create commands for field parameters
G4FieldBuilder* fieldBuilder = G4FieldBuilder::Instance();
// fieldBuilder->SetVerboseLevel(2);
auto localFieldParameters = fieldBuilder->CreateFieldParameters("logicalFieldVolume");
// set default min step 0.25 mm
localFieldParameters->SetMinimumStep(0.25 * mm);
}
VolumeConstruction::~VolumeConstruction()
{}
G4VPhysicalVolume* VolumeConstruction::Construct()
{
G4NistManager* nist = G4NistManager::Instance();
//-------Build world (mother volume)------------
G4Material* worldMat = nist->FindOrBuildMaterial("G4_Galactic");
//Creating solid volume
G4double worldSizeX = 400*mm;
G4double worldSizeY = 400*mm;
G4double worldSizeZ = 600*mm;
G4Box* solidWorld = new G4Box("solidWorld", worldSizeX/2, worldSizeY/2, worldSizeZ/2);
//Creating logical volume
logical volume
G4LogicalVolume* logicWorld = new G4LogicalVolume(solidWorld, worldMat, "logicWorld");
// Creating physical volume for interactions,
G4VPhysicalVolume* physWorld = new G4PVPlacement(0, G4ThreeVector(0., 0., 0.), logicWorld,
"physWorld", 0, false, 0, true);
//-------Defining Electric Field Volume------
// Creating solid volume of field region
fieldRegionSizeX = 49.5 * mm;
fieldRegionSizeY = 49.5 * mm;
fieldRegionSizeZ = 100 * mm;
G4Box *eFieldVolume = new G4Box("eFieldVolume", fieldRegionSizeX/2, fieldRegionSizeY/2, fieldRegionSizeZ/2);
// Creating logical volume of field region
logicalFieldVolume = new G4LogicalVolume(eFieldVolume, worldMat, "logicalFieldVolume");
// Creating physical volume of field region
fieldZ0 = -10*cm;
G4VPhysicalVolume *physFieldVolume = new G4PVPlacement(0, G4ThreeVector(0., 0., fieldZ0), logicalFieldVolume,
"physFieldVolume", logicWorld, false, 0, true);
return physWorld;
}
void VolumeConstruction::SetLocalFieldValue(G4ThreeVector value)
{
fLocalFieldVector = value;
G4UniformElectricField* eField = nullptr;
//G4UniformMagField* magField = nullptr;
if (fLocalFieldVector != G4ThreeVector(0.,0.,0.)) {
eField = new G4UniformElectricField(fLocalFieldVector);
//magField = new G4UniformMagField(fLocalFieldVector);
}
// Set field to the field builder
auto fieldBuilder = G4FieldBuilder::Instance();
fieldBuilder->SetLocalField(eField, logicalFieldVolume);
//fieldBuilder->SetLocalField(magField, logicalFieldVolume);
}
void VolumeConstruction::ConstructSDandField()
{
// Create detector fields
SetLocalFieldValue(fLocalFieldVector);
// Construct all Geant4 field objects
auto fieldBuilder = G4FieldBuilder::Instance();
fieldBuilder->SetFieldType(kElectroMagnetic);
fieldBuilder->ConstructFieldSetup();
}
Before the gui crashes, the below exception is shown:
G4Exception : GeomFieldParameters0001
issued by : G4FieldSetup::CreateEquation:
Incompatible field and equation.
The field type must be set explicitly for other than magnetic field.
Fatal Error in Argument *** core dump ***
Track information is not available at this moment
Step information is not available at this moment
I tried to address this problem by adding the line:
fieldBuilder->SetFieldType(kElectroMagnetic);
but this doesn’t seem to have fixed the problem.