# Import electric field from COMSOL

**URL:** <https://root-forum.cern.ch/t/import-electric-field-from-comsol/62971>\
**Category:** Garfield++\
**Created:** [February 18, 2025, 12:04pm UTC](https://root-forum.cern.ch/t/import-electric-field-from-comsol/62971 "2025-02-18T12:04:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Linnuo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/439d5e/32.png) [@Linnuo](https://root-forum.cern.ch/u/Linnuo)\
**Post date:** [February 18, 2025, 12:04pm UTC](https://root-forum.cern.ch/t/import-electric-field-from-comsol/62971/1 "2025-02-18T12:04:56Z")

</div>

Dear experts,

I try to import the field map from COMSOL and I have checked that the voltage of different place has been import successfully. But the electric field isn’t defined. I also checked that the electric filed could be plotted normally.

```auto
  ComponentComsol fm;
  fm.Initialise("mesh.mphtxt", "dielectrics.dat", "field.txt", "cm");
  fm.EnableMirrorPeriodicityX();
  fm.EnableMirrorPeriodicityY();
  fm.PrintRange();
  fm.SetMedium(0,&gas);  
  fm.SetMedium(1,&metal);
  fm.PrintMaterials();

  Sensor sensor;
  sensor.AddComponent(&fm);
  sensor.SetArea(
    -1, -1, 0,
    1, 1, 30
  );

  std::vector<std::array<double,3>> points;
  std::array good_point = {0.3,0.1,15.0};
  std::array bad_point = {0.8,0.0,0.0};
  points.push_back(good_point);
  points.push_back(bad_point);
  
  for(int i=0;i<points.size();i++){
      double ex,ey,ez,v;
      Medium* m;
      int status;
      double x=points[i][0];
      double y=points[i][1];
      double z=points[i][2];
      // Check if electric field at point is defined
      sensor.ElectricField(x,y,z,ex,ey,ez,v,m,status);
      if(!status){
          drift.DriftElectron(x,y,z,0.0);
          std::cout<<"Successfully drifted ("<<x<<","<<y<<","<<z<<")"<<std::endl;
      }else{
          std::cout<<"No field at ("<<x<<","<<y<<","<<z<<")"<<std::endl;
      }
  } 

```

The output is

> No field at (0.3,0.1,15)
> 
> No field at (0.8,0,0)

The status # is -5

I would appreciate it very much if you could help me to figure it out.

---

<div class="post-metadata">

**Author:** ![djjansse](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/8baadc/32.png) [@djjansse](https://root-forum.cern.ch/u/djjansse)\
**Post date:** [February 18, 2025, 12:48pm UTC](https://root-forum.cern.ch/t/import-electric-field-from-comsol/62971/2 "2025-02-18T12:48:33Z")

</div>

Dear @Linnuo,

Only the potential map can be imported, not the field. See [COMSOL |&nbsp;Garfield++](https://garfieldpp.web.cern.ch/garfieldpp/examples/comsol/) for further instructions.

Kind regards,  
Djunes

---

<div class="post-metadata">

**Author:** ![Linnuo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/439d5e/32.png) [@Linnuo](https://root-forum.cern.ch/u/Linnuo)\
**Post date:** [February 18, 2025, 12:51pm UTC](https://root-forum.cern.ch/t/import-electric-field-from-comsol/62971/3 "2025-02-18T12:51:12Z")

</div>

Dear @djjansse

Thank you so much. I think the electric field has been successfully imported. Is there any way to define the active mediu,. It seems that the point is onside the mesh but not in an active medium.

Best,  
Linnuo

---

<div class="post-metadata">

**Author:** ![djjansse](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/8baadc/32.png) [@djjansse](https://root-forum.cern.ch/u/djjansse)\
**Post date:** [February 18, 2025, 1:57pm UTC](https://root-forum.cern.ch/t/import-electric-field-from-comsol/62971/4 "2025-02-18T13:57:10Z")

</div>

Dear @Linnuo,

Oh good, in that case I misunderstood.In principle` fm.SetGas(&gas)` should do the job. Maybe try that instead of `fm.SetMedium(0,&gas) fm.SetMedium(1,&metal)`?

Kind regards,  
Djunes

---

<div class="post-metadata">

**Author:** ![Linnuo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/439d5e/32.png) [@Linnuo](https://root-forum.cern.ch/u/Linnuo)\
**Post date:** [February 18, 2025, 3:06pm UTC](https://root-forum.cern.ch/t/import-electric-field-from-comsol/62971/5 "2025-02-18T15:06:05Z")

</div>

Dear @djjansse ,

Thank you so much for your reply. I solved it by changing the Permittivity of the gas in the dielectric.dat file to absolute zero, though I do not know why.

Best,  
Linnuo

---

<div class="post-metadata">

**Author:** ![Piet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/piet/32/12536_2.png) [@Piet](https://root-forum.cern.ch/u/Piet)\
**Post date:** [February 18, 2025, 4:41pm UTC](https://root-forum.cern.ch/t/import-electric-field-from-comsol/62971/6 "2025-02-18T16:41:29Z")

</div>

Dear @Linnuo  
A material with permittivity is automatically recognised in the code as a gas and set as a driftable medium. Alternatively you can use the methods provided by Djunes.  
greets  
Piet

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 4, 2025, 4:48pm UTC](https://root-forum.cern.ch/t/import-electric-field-from-comsol/62971/8 "2025-03-04T16:48:30Z")

</div>

Closed on March 4, 2025, 4:48pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
