# DriftNegativeIon hangs at certain point even in valid region

**URL:** <https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575>\
**Category:** Garfield++\
**Created:** [May 19, 2025, 7:57am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575 "2025-05-19T07:57:13Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![dm-leo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dm-leo/32/29761_2.png) [@dm-leo](https://root-forum.cern.ch/u/dm-leo)\
**Post date:** [May 19, 2025, 7:57am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/1 "2025-05-19T07:57:14Z")

</div>

Dear all,  
I’m currently simulating negative ion drift using AvalancheMC::DriftNegativeIon with a field map from COMSOL (ComponentComsol). The simulation generally works well, but for certain starting points, the drift appears to hang — the function never returns, memory usage increases continuously, and the program does not proceed beyond that call.

Here are some details:

1. I tried to debug what happened

```auto
std::cout<<(*x_s)[i]/10<<" "<<(*y_s)[i]/10<<" "<<(*z_s)[i]/10+250<<" "<<i<<std::endl;
drift.DriftNegativeIon((*x_s)[i]/10,(*y_s)[i]/10,(*z_s)[i]/10+250,0);
std::cout<<"Drift done"<<std::endl;

```

The terminal shows

```auto
......
50.1142 -16.9341 160.907 1180
Drift done
50.1145 -17 160.832 1181
Drift done
50.122 -17.0694 160.761 1182

```

So (x, y, z) = (50.122, -17.0694, 160.761) caused the problem.

1. Step size configuration

```auto
auto step = [](double x, double y, double z) {
                if (z>3.) return 1.;
                else return 1e-3;
        };
        drift.SetStepDistanceFunction(step);

```

No error message is shown, but DriftNegativeIon(…) does not complete.

1. I attempted to check the drift region using:

```auto
sensor.IsInside(x, y, z)

```

and it returns true.

Questions:

1. What could cause DriftNegativeIon to hang even when the point seems inside the drift region?
2. Is there any built-in timeout or maximum step count mechanism in AvalancheMC to prevent infinite loops?
3. Is there a way to safely skip problematic points during simulation?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 19, 2025, 7:58am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/2 "2025-05-19T07:58:46Z")

</div>

I guess @hschindl can help you.

---

<div class="post-metadata">

**Author:** ![dm-leo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dm-leo/32/29761_2.png) [@dm-leo](https://root-forum.cern.ch/u/dm-leo)\
**Post date:** [May 26, 2025, 7:13am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/3 "2025-05-26T07:13:29Z")

</div>

Could anyone help me?

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [May 26, 2025, 7:39am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/4 "2025-05-26T07:39:12Z")

</div>

Hi,  
sorry for the late reply! Can you try to plot the drift line for this starting point?  
By default, AvalancheMC does not apply a maximum number of steps or timeout. You can set a time window though.

---

<div class="post-metadata">

**Author:** ![dm-leo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dm-leo/32/29761_2.png) [@dm-leo](https://root-forum.cern.ch/u/dm-leo)\
**Post date:** [May 26, 2025, 8:37am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/5 "2025-05-26T08:37:40Z")

</div>

Sorry, the DriftNegativeIon() does not complete, I could not get the drift line. How to set time window? Could it be done in Garfield++? Or using C++？ （ std::async + std::future\_status::timeout）

---

<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:** [May 26, 2025, 10:43am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/6 "2025-05-26T10:43:12Z")

</div>

You can set the time window in the sensor class: `sensor. SetTimeWindow()`. Instead the driftline you should setup a `ViewDrift` object and associate it to a canvas before launching the driftline, this way you should be able to plot the driftline while the calculation is ongoing. Of course if you are just stuck in a single point from the beginning, then you will likely see just a single point.

---

<div class="post-metadata">

**Author:** ![dm-leo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dm-leo/32/29761_2.png) [@dm-leo](https://root-forum.cern.ch/u/dm-leo)\
**Post date:** [May 26, 2025, 11:26am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/7 "2025-05-26T11:26:51Z")

</div>

Hi Piet,  
I think we have a misunderstanding. I know sensor. SetTimeWindow(). The time range of simulation could be set by this function. The problem is that when DriftNegativeIon() running with the certain point, it stuck.

```auto
        Sensor sensor(&fm);
        sensor.AddElectrode(&fm,"anode");
        const unsigned int nBins = 1e5;
        double tend = 5e10;
        sensor.SetTimeWindow(0, tend/nBins, nBins);

       AvalancheMC drift;
        drift.SetSensor(&sensor);
        drift.UseWeightingPotential(true);
        drift.DisableDiffusion();
        drift.EnableSignalCalculation();

        ViewDrift driftView;
        constexpr bool plotdrift = true;
        if (plotdrift){
                TCanvas* c2 = new TCanvas("c2", "Drift Path", 600, 600);
                driftView.SetCanvas(c2);
                drift.EnableSignalCalculation();
                drift.EnablePlotting(&driftView);
                driftView.Plot();
        }

drift.DriftNegativeIon(50.122 -17.0694 160.761,0);

```

If I’m not mistaken, the coordinates of drift line could be stored after drift.DriftNegativeIon() finishing and called by drift.GetNegativeIons(). But the drift.DriftNegativeIon() could not be finished 😭

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [May 26, 2025, 11:30am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/8 "2025-05-26T11:30:07Z")

</div>

- Indeed, `Sensor::SetTimeWindow` sets the range and binning for the signal calculation. You can use the function `SetTimeWindow` of `AvalancheMC` to impose a time limit for the drift line calculation.
- You’re right, the plotting happens after the drift line is terminated; so my suggestion to plot it was indeed not very helpful… Could you switch on debugging in `AvalancheMC` (`EnableDebugging`) and send the output for the starting point that causes the calculation to hang?

---

<div class="post-metadata">

**Author:** ![dm-leo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dm-leo/32/29761_2.png) [@dm-leo](https://root-forum.cern.ch/u/dm-leo)\
**Post date:** [May 26, 2025, 11:46am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/9 "2025-05-26T11:46:38Z")

</div>

Hi hschindl  
I use EnableDebugging, and terminal is loopy printing

```auto
    Next point: (49.907523, -17.059318, 1.607878).
AvalancheMC::GetVelocity: Velocity at (49.907523, -17.059318, 1.607878) = -6.1624e-10, 1.28349e-09, -1.79169e-10
AvalancheMC::GetVelocity: Velocity at (49.907094, -17.058424, 1.607754) = 2.83941e-08, -5.81179e-08, 8.24161e-09
AvalancheMC::GetVelocity: Velocity at (49.907308, -17.058871, 1.607816) = 2.83481e-08, -5.86053e-08, 8.23616e-09
Avalan^CcheMC::GetVelocity: Velocity at (49.907416, -17.059095, 1.607847) = 2.83251e-08, -5.88489e-08, 8.23344e-09
    Next point: (49.907469, -17.059207, 1.607863).
AvalancheMC::GetVelocity: Velocity at (49.907469, -17.059207, 1.607863) = 2.83136e-08, -5.89708e-08, 8.23207e-09
AvalancheMC::GetVelocity: Velocity at (49.907899, -17.060101, 1.607988) = -4.58455e-10, -4.31539e-11, -6.31958e-10
AvalancheMC::GetVelocity: Velocity at (49.907684, -17.059654, 1.607925) = -5.48618e-10, 7.14927e-10, -3.73222e-10
AvalancheMC::GetVelocity: Velocity at (49.907577, -17.059430, 1.607894) = -5.937e-10, 1.09397e-09, -2.43853e-10

```

It seems that (49.907523, -17.059318, 1.607878) and (49.907469, -17.059207, 1.607863) fall in infinite loop.

---

<div class="post-metadata">

**Author:** ![dm-leo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dm-leo/32/29761_2.png) [@dm-leo](https://root-forum.cern.ch/u/dm-leo)\
**Post date:** [May 29, 2025, 8:24am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/10 "2025-05-29T08:24:50Z")

</div>

Oh, maybe I found the solution. The step length must be suitable. A non integer step length should be used which could skip the oscillation points. That may caused by the accurate of mesh file from COMSOL.

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [June 2, 2025, 5:18am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/11 "2025-06-02T05:18:54Z")

</div>

Hi,  
sorry for the late reply. Changing the step size might indeed have an effect (although integer or non-integer should not really matter). I can try to reproduce the issue and do some more in-depth debugging, but I would need your COMSOL field map and a **minimal** working example.

---

<div class="post-metadata">

**Author:** ![dm-leo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dm-leo/32/29761_2.png) [@dm-leo](https://root-forum.cern.ch/u/dm-leo)\
**Post date:** [June 2, 2025, 7:29am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/12 "2025-06-02T07:29:13Z")

</div>

Hello  
Thanks for your reply. I am glad to share my comsol files. But these files are so large (about 100G). Let me try to build a simple example which can repeat it.

---

<div class="post-metadata">

**Author:** ![dm-leo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dm-leo/32/29761_2.png) [@dm-leo](https://root-forum.cern.ch/u/dm-leo)\
**Post date:** [June 3, 2025, 7:43am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/13 "2025-06-03T07:43:11Z")

</div>

Hi  
I have built a simple project and uploaded the package in Google drive.

> **[debug.zip](https://drive.google.com/file/d/1GUcZ5a-BcQTmKFLd9qQuRSfdvuhvfhGF/view?usp=share_link)**
>
> Google Drive file.

Please let me know if you need any additional files or details.  
Thank you very much for your reply and your willingness to help.

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [June 5, 2025, 4:26am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/14 "2025-06-05T04:26:13Z")

</div>

Many thanks! I will try to debug asap…

---

<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:** [June 19, 2025, 4:27am UTC](https://root-forum.cern.ch/t/driftnegativeion-hangs-at-certain-point-even-in-valid-region/63575/15 "2025-06-19T04:27:05Z")

</div>

Closed on June 19, 2025, 4:27am UTC

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