Outputting data at t = 0

How do I output results at time zero, please? adding 0 to this line does not seem to work:
F.TXTExport(
field=“solute”,
times=[0,1,2,3,1000],
filename=results_folder + “/mobile_concentration.txt”,

Hi @HelenS ,

:bulb: Quick tip: you can format your code using three ` signs like this

F.TXTExport(
    field=“solute”,
    times=[0,1,2,3,1000],
    filename=results_folder + “/mobile_concentration.txt”,
)

It makes it easier for us to read your code :slight_smile:

It is not possible to export the field at time 0 at the moment.

Hi Helen,

I have been giving this some thoughts and this is how you can hack your way to export everything at t=0s.

In this example (adapted from task 1 of the workshop), there is an initial condition of c=1e21 H/m3 everywhere in the 1D domain.

The concentration on the surfaces is set to zero.

import numpy as np

import festim as F

my_model = F.Simulation()

my_model.mesh = F.MeshFromVertices(vertices=np.linspace(0, 7e-6, num=1001))

my_model.materials = F.Material(id=1, D_0=1e-7, E_D=0.2)

my_model.T = 300

my_model.boundary_conditions = [
    F.DirichletBC(surfaces=[1, 2], value=0, field=0)  # H/m3/s
]

my_model.initial_conditions = [F.InitialCondition(field=0, value=1e21)]

my_model.settings = F.Settings(
    absolute_tolerance=1e10, relative_tolerance=1e-10, final_time=2  # s
)
results_folder = "results"

xdmf_export = F.XDMFExport(
    field="solute",
    filename=results_folder + "/hydrogen_concentration.xdmf",
    checkpoint=False,  # needed in 1D
)
txt_export = F.TXTExport(
    field="solute",
    times=[0.0, 0.1, 0.2, 0.5, 1],
    filename=results_folder + "/mobile_concentration.txt",
)
my_model.exports = [xdmf_export, txt_export]
my_model.dt = F.Stepsize(0.05, milestones=[0.1, 0.2, 0.5, 1])  # s
my_model.initialise()

# little hack to export for t=0s after calling .initialise()

my_model.exports.final_time = my_model.settings.final_time
my_model.h_transport_problem.u.assign(my_model.h_transport_problem.u_n)
my_model.run_post_processing()


# run the simulation
my_model.run()


# plot solution

import matplotlib.pyplot as plt
import numpy as np

data = np.genfromtxt(
    results_folder + "/mobile_concentration.txt", skip_header=1, delimiter=","
)

for i, t in enumerate(txt_export.times):
    plt.plot(data[:, 0], data[:, i + 1], label=f"t={t:.1f}s")

plt.xlabel("x (m)")
plt.ylabel("Mobile concentration (H/m3)")
plt.legend()
plt.show()

This produces the following plot: