How to Set ElectricField in my code using Garfield++


Hi, I had a problem with my simulation using Garfield++. I have a txt file that records the field distribution, and it’s formatted like this
z r Er Ez
0.0000 0.0000 0.0000 0.0000
0.0000 0.0010000 0.0000 0.0000
0.0000 0.0020000 0.0000 0.0000
0.0000 0.0030000 0.0000 0.0000
0.0000 0.0030000 231.24 -3.3500E-4
0.0000 0.0040489 231.24 -3.5830E-4
0.0000 0.0050979 231.23 -4.6605E-5
0.0000 0.0061468 231.23 -1.3203E-4
0.0000 0.0073039 231.23 -2.3166E-4
0.0000 0.0084610 231.23 1.8453E-4

This file has a lot of data, but its data is not complete, nor is it a regular fetch point. Is there any way I can set up such an electric field in the simulation?
I guess I’ll use interpolation method, but I don’t know how to implement it
I sincerely hope there will be a solution.It’s part of my first simulation work by using Garfield++

@hschindl

_ROOT Version:6.34
_Platform:vscode
_Compiler:CMake


Hi,

Adding @hschindl in the loop.

Cheers,
D

Hi,
one option is to use ComponentGrid which allows you to load a set of electric fields in cylindrical coordinates. This example

is for loading a magnetic field map on an rz grid but it’s very similar for electric fields.
ComponentGrid only works with a regular grid though, so you would indeed have to interpolate/extrapolate your data to a regular grid.

Thank you for your help!
I have tried the method,but got this feedback in terminal

And the field image got nothing,

What is wrong?I’m confused.@hschindl

(Here’s my code)
#include <Garfield/ComponentGrid.hh>
#include <TROOT.h>
#include <TApplication.h>
#include <Garfield/MediumMagboltz.hh>
#include <Garfield/ViewField.hh>
#include <Garfield/GeometrySimple.hh>
#include <Garfield/SolidTube.hh>
#include <Garfield/Sensor.hh>
#include <TH2D.h>
#include <TFile.h>

using namespace Garfield;

int main() {
const double rTube = 36;
const double halflength = 48;

MediumMagboltz gas;
gas.SetComposition(“CF4”,100.);
gas.SetPressure(760.0);
gas.SetTemperature(293.15);
gas.LoadGasFile(“CF4_1atm.gas”);

SolidTube tube(0., 0., 0., rTube, halflength);

GeometrySimple geo;
geo.AddSolid(&tube,&gas);

ComponentGrid cmp;
cmp.SetMedium(&gas);
cmp.SetCylindricalCoordinates();
cmp.LoadElectricField(“field_data.txt”,“XZ”,false,false);
cmp.SetGeometry(&geo);

Sensor sensor;
sensor.AddComponent(&cmp);

ViewField fieldView(&cmp);
fieldView.SetPlaneXZ();
// Set the plot limits in the current viewing plane.
const double xmin = -rTube;
const double xmax = rTube;
const double zmin = 0;
const double zmax = 2 * halflength;
fieldView.SetArea(xmin, zmin, xmax, zmax);
fieldView.SetElectricFieldRange(0.,50000);
fieldView.GetCanvas()->SetLeftMargin(0.16);
fieldView.Plot(“v”,“COLZ”);

std::vector xf;
std::vector yf;
std::vector zf;
fieldView.PlotFieldLines(xf, yf, zf, true, false);
fieldView.GetCanvas()->SaveAs(“field_image.png”);
}

Hi,
thanks for the code. There is one issue that I noticed: you are loading only the electric field (not the potential), but you’re trying to make a contour plot of the potential (fieldView.Plot(“v”,“COLZ”);).

Hi,thanks for your advice!
So does that mean I need to put the corresponding potential data in my txt file?
Because if I just set the bool value of “withpotential” to true, I’ll get an error:

ComponentGrid::ElectricField: Map not available

@hschindl (Sorry to bother you again, :crying_cat_face:

Well, yes, if you want to import potentials you need to include them in your txt file.
Note that you don’t need the potentials for the actual simulations (the charge transport algorithms only use the electric field), but they are of course useful for plotting purposes.

Thank you for your help!
The problem solved,and I have now been able to simulate the movement of electrons in such an electric field.
It is really wonderful because it was the first successful Garfield example I wrote.
Sincerely,thanks for your help! :heart: