# Time-dependent source

**URL:** <https://festim.discourse.group/t/time-dependent-source/119>\
**Category:** User Support\
**Created:** [June 3, 2025, 1:39pm UTC](https://festim.discourse.group/t/time-dependent-source/119 "2025-06-03T13:39:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![miklavrent](https://yyz2.discourse-cdn.com/free1/user_avatar/festim.discourse.group/miklavrent/32/94_2.png) [@miklavrent](https://festim.discourse.group/u/miklavrent)\
**Post date:** [June 3, 2025, 1:39pm UTC](https://festim.discourse.group/t/time-dependent-source/119/1 "2025-06-03T13:39:45Z")

</div>

Hi,

I guess this is a simple question, but couldn’t find a solution fast. If I have a source:

my\_model.sources = [F.Source(value=1e20, volume=1, field=0)]

then I believe it should be possible for value to be time-dependent, like 1e20\*festim.t. But what if I want to have non-smooth time dependence, e.g. source is on for one hour followed by another hour off and so on. Is it possible to introduce this kind of time dependence?

Best wishes,

Mikhail

---

<div class="post-metadata">

**Author:** ![VVKulagin](https://yyz2.discourse-cdn.com/free1/user_avatar/festim.discourse.group/vvkulagin/32/51_2.png) [@VVKulagin](https://festim.discourse.group/u/VVKulagin)\
**Post date:** [June 3, 2025, 2:04pm UTC](https://festim.discourse.group/t/time-dependent-source/119/2 "2025-06-03T14:04:51Z")

</div>

Hi @miklavrent!

`sympy.Piecewise` can be used to set up a piece-wise source in FESTIM1. Consider the following [example from the FESTIM V&V book](https://festim-vv-report.readthedocs.io/en/latest/validation/thermodesorption_spectra/ogorodnikova/ogorodnikova-tds.html):

```auto
ion_flux = sp.Piecewise((incident_flux, F.t <= 1), (0, True))

source_term = F.ImplantationFlux(
    flux=ion_flux, imp_depth=4.5e-9, width=2.5e-9, volume=1
)

```

If you use FESTIM2, then you can do it with `ufl.conditional` as shown in [FESTIM2 V&V](https://festim-vv-report.readthedocs.io/en/festim-2/validation/thermodesorption_spectra/ogorodnikova/ogorodnikova-tds.html).

Please, let me know if it helps.

---

<div class="post-metadata">

**Author:** ![miklavrent](https://yyz2.discourse-cdn.com/free1/user_avatar/festim.discourse.group/miklavrent/32/94_2.png) [@miklavrent](https://festim.discourse.group/u/miklavrent)\
**Post date:** [June 3, 2025, 2:23pm UTC](https://festim.discourse.group/t/time-dependent-source/119/3 "2025-06-03T14:23:31Z")

</div>

Thank you Vladimir,

Yes this works, like:

h\_source = sp.Piecewise((1e20, F.t \<= 3600), (0, F.t \<= 7200), (1e20, F.t \<= 10800), (0, True))

my\_model.sources = [F.Source(value=h\_source, volume=1, field=0)]

Just if I want to do this switch on and off for say a year, do you think there is a way to avoid writing a line for h\_source with about 8760 conditions? 🙂

Best wishes,

Mikhail

---

<div class="post-metadata">

**Author:** ![remidm](https://yyz2.discourse-cdn.com/free1/user_avatar/festim.discourse.group/remidm/32/4_2.png) [@remidm](https://festim.discourse.group/u/remidm)\
**Post date:** [June 3, 2025, 2:39pm UTC](https://festim.discourse.group/t/time-dependent-source/119/4 "2025-06-03T14:39:24Z")

</div>

I remember having to write pulsed sources like the one you describe @miklavrent but I’m afraid I can’t remember how (it’s still early here in CA so it may come back later!!)

@VVKulagin do you know if Mikhail could use a modulo here?

---

<div class="post-metadata">

**Author:** ![VVKulagin](https://yyz2.discourse-cdn.com/free1/user_avatar/festim.discourse.group/vvkulagin/32/51_2.png) [@VVKulagin](https://festim.discourse.group/u/VVKulagin)\
**Post date:** [June 3, 2025, 2:54pm UTC](https://festim.discourse.group/t/time-dependent-source/119/5 "2025-06-03T14:54:12Z")

</div>

@miklavrent @remidm

Yeah, how about modulo with some periodicity. Here is an example with `ImplantationFlux`:

```auto
def h_source(t):
    t1 = t % 7200 # period
    return sp.Piecewise((1e20, t1 < 3600), (0, True))

source_term = F.ImplantationFlux(
    flux=h_source(F.t), imp_depth=4.5e-9, width=2.5e-9, volume=1
)

```

> **Example from FESTIM simulation**
>
> ![image](https://global.discourse-cdn.com/free1/uploads/festim/original/1X/1e0ea866550a15849c12be10e88bd2ba824a6e21.png)

> **Draft script**
>
> ```auto
> import festim as F
> import numpy as np
> import sympy as sp
> import matplotlib.pyplot as plt
> 
> model = F.Simulation()
> 
> sample_depth = 5e-4
> 
> vertices = np.concatenate(
> [
> np.linspace(0, 30e-9, num=200),
> np.linspace(30e-9, 3e-6, num=300),
> np.linspace(3e-6, 20e-6, num=200),
> np.linspace(20e-6, sample_depth, num=100),
> ]
> )
> 
> model.mesh = F.MeshFromVertices(vertices)
> # Material Setup, only W
> tungsten = F.Material(
> id=1,
> D_0=4.1e-07, # m2/s
> E_D=0.39, # eV
> )
> 
> model.materials = tungsten
> 
> def h_source(t):
> t1 = t % 7200 # period
> return sp.Piecewise((1e20, t1 < 3600), (0, True))
> 
> source_term = F.ImplantationFlux(
> flux=h_source(F.t), imp_depth=4.5e-9, width=2.5e-9, volume=1
> )
> 
> model.sources = [source_term]
> 
> # boundary conditions
> model.boundary_conditions = [F.DirichletBC(surfaces=[1, 2], value=0, field=0)]
> 
> model.T = 293
> 
> model.dt = F.Stepsize(
> initial_value=0.1,
> stepsize_change_ratio=1.1,
> max_stepsize=100,
> dt_min=1e-05,
> )
> 
> model.settings = F.Settings(
> absolute_tolerance=1e10,
> relative_tolerance=1e-09,
> final_time=18000, # time to reach max temp
> )
> 
> derived_quantities = F.DerivedQuantities(
> [
> F.HydrogenFlux(surface=1),
> ],
> )
> 
> model.exports = [derived_quantities]
> 
> model.initialise()
> model.run()
> 
> plt.plot(derived_quantities.t, -np.array(derived_quantities[0].data))
> plt.ylabel("Hydrogen flux")
> plt.xlabel("Time")
> plt.yscale("log")
> plt.show()
> 
> ```

---

<div class="post-metadata">

**Author:** ![miklavrent](https://yyz2.discourse-cdn.com/free1/user_avatar/festim.discourse.group/miklavrent/32/94_2.png) [@miklavrent](https://festim.discourse.group/u/miklavrent)\
**Post date:** [June 3, 2025, 4:55pm UTC](https://festim.discourse.group/t/time-dependent-source/119/6 "2025-06-03T16:55:20Z")

</div>

Thank you both, yes this should work, also if on and off times are different, like

def h\_source(t):  
t1 = t % 7200 # period  
return sp.Piecewise((1e20, t1 \< 5400), (0, True))

Sorry, this is probably more a Python than a FESTIM question.

Best wishes,

Mikhail

---

<div class="post-metadata">

**Author:** ![remidm](https://yyz2.discourse-cdn.com/free1/user_avatar/festim.discourse.group/remidm/32/4_2.png) [@remidm](https://festim.discourse.group/u/remidm)\
**Post date:** [June 3, 2025, 6:02pm UTC](https://festim.discourse.group/t/time-dependent-source/119/7 "2025-06-03T18:02:42Z")

</div>

Yes I think this should work all the same
