Yes, that is possible using a hook function during Molecular Dynamics.
To do so, you first have to define a suitable hook function in your script, e.g.
def chemical_potential_hook(step, time, configuration):
"""
Hook function to calculate and print the chemical potential at
every MD step.
"""
chemical_potential = ChemicalPotential(configuration)
nlprint(chemical_potential)
Then, you define your MD simulation as usual, but with the additional argument
post_step_hook=chemical_potential_hook
e.g.
md_trajectory = MolecularDynamics(
bulk_configuration,
trajectory_filename='trajectory.nc',
steps=100000,
log_interval=2,
post_step_hook=chemical_potential_hook,
method=method
)
The hook function will now be called after each MD step and the specified operation, in this case the calculation and printing of the chemical potential, will be performed.
Of course, the calculator that you use for running molecular dynamics must support the calculation of the chemical potential (i.e. it must be an electronic-structure-based calculator such as the LCAOCalculator, as this is the electronic chemical potential).