# Accessing a time-averaged variable every time step

Hi,

I am using LAMMPS (29 Sep 2021 - Update 3)

Is there a way to access a variable using fix addforce every time step, given that this variable is averaged over the last 1000 time steps using fix ave/time?

## Example:

``````variable                energyAtom atom \${massAtom}*vx*vx*1e10

compute                 energy_in group_fluid reduce/region inlet sum v_energyAtom
variable                energy_density_in equal c_energy_in/(0.03*lx*ly*v_thickness_div)
variable                Fin equal v_energy_density_in*(ly*v_thickness_div)/count(group_inlet)

compute                 energy_out group_fluid reduce/region outlet sum v_energyAtom
variable                energy_density_out equal c_energy_out/(0.03*lx*ly*v_thickness)
variable                Fout equal v_energy_density_out*(ly*v_thickness)/count(group_outlet)

## Average the momentum flux forxes (Fin and Fout) every 1000 time steps
fix                     mom_flux_forces group_fluid ave/time 10 100 1000 v_Fin v_Fout
variable                net_mom_flux_force equal f_mom_flux_forces[1]-f_mom_flux_forces[2]

## And impose the net force every time step
fix                     mom_flux_force group_pump_dyn addforce v_net_mom_flux 0.0 0.0
``````

This gives the error of time incompatibility

``````"ERROR: Variable net_mom_flux: Fix in variable not computed at a compatible time (../variable.cpp:1676)"
``````

because â€śthe variable attempts to access the fix on non-allowed timestepsâ€ť.

Indeed the first time-average will only be produced on the 1000th timestep and I can use:

``````fix                     .. addforce v_net_mom_flux 0.0 0.0 every 1000
``````

but that is not what i want.

I simply want to access the time average of the variable in 0-1000 steps and use it in the fix addforce on the 1001th step, then average the 1-1001 steps and use it on the 1002nd step and so on. i.e. a moving average of the last N timesteps that is accessible every timestep. I would appreciate any help.

Thanks,
Mohamed Tarek ELEWA, M.Sc.

Karlsruhe Institute of Technology (KIT)
Institute for Applied Materials -
Reliability and Microstructure (IAM-ZM)

You could â€ścacheâ€ť the results from fix ave/time with fix store/state

I think something like
`fix mom_flux_forces group_fluid ave/time 1 1 1 v_Fin v_Fout ave window 1000`
should work. The only difference is that it will take an average from every step instead of every tenth.

Hi Axel, Thanks for the reply.
I believe the `fix store/state` needs per-atom quantities. The output from the ` fix ave/time` are global variables. Am I missing something here?

Hi, Thanks for the reply.
I forgot to mention I tried this one out before posting, but I got a different, unrelated error of bad dynamics,

``````"ERROR on proc 13: Angle atoms 3796 3797 3798 missing on proc 13 at step 100977 (../ntopo_angle_all.cpp:68)"
``````

so I thought that this is not the correct route because the force is too large!
After reading your comment, I thought about using a larger averaging window or running for longer time before averaging. I am still testing thisâ€¦

This works fine. Thanks a lot!

1 Like