I understand that when we specify a coefficient in a fix command in LAMMPS, it is typically used as a constant input throughout the simulation.
For example: fix 1 all nvt temp 100 100
This command maintains the system at a constant temperature of 100 Kelvin.
However, I am interested in updating the coefficient dynamically based on the instantaneous pressure:
Temperature = instantaneous_pressure^2 / 20.0 + 20.
Is there a way to instruct LAMMPS to update the fix coefficient dynamically during the simulation?
Here is what I attempted in my previous try: run 0 variable Temp_Usr equal press^2/20.0+20 print ${Temp_Usr} fix 1 all nvt temp ${Temp_Usr} ${Temp_Usr} 5.0 run 10000
Although from the log file, the variable ${Temp_Usr} is recalculated continuously, the temperature coefficient in the fix command does not seem to change during the simulation.
Could anyone provide any advice on how to achieve dynamic updating of the fix coefficient?
The information you seek is in the LAMMPS manual, you just need to study it more carefully.
Your approach is halfway there. The problem is that the ${variable} expansion expands the current value of that variable right when the command is processed during input pre-processing. That means, the fix command does not even know it is a variable, since when the fix command line is parsed, the value of ${variable} is “seen” by the fix.
This is a misconception. Variables are only evaluated when they are used. It is the processing of the line of the “print” command that triggers the evaluation.
The solution is that some commands accept a variable reference (e.g. v_variable) as an argument to a keyword and that includes the “temp” keyword of fix nvt. The issue is that command styles need to be specifically programmed for that purpose so that they recognize that v_variable is a variable reference as an argument and not a string (and then exit with an error, since they expected it to be a number).
As mentioned above, the details are in the LAMMPS manual. You just need to keep reading it with more care.
You are correct about the method for defining the variable.
Indeed, we need to use “v_Temp_Usr” to enable the log file to print the variable continuously.
I apologise for my earlier statement, “Although from the log file, the variable ${Temp_Usr} is recalculated continuously.” In reality, the variable “v_Temp_Usr” was included in the thermo_style command, which allowed me to observe its updates throughout the simulation (not the print command).
Here are my actual earlier inputs: variable Temp_Usr equal press^2/20.0+20 print ${Temp_Usr} fix 2 all nvt temp ${Temp_Usr} ${Temp_Usr} 5.0 thermo_style custom step v_Temp_Usr time tpcpu etotal ke pe enthalpy temp press vol density thermo 100 run 10000
I would like to confirm one thing, if possible.
In the fix command, I understand that replacing ${Temp_Usr} with v_Temp_Usr: fix 2 all nvt temp v_Temp_Usr v_Temp_Usr 5.0
…results in an error: ERROR: Expected floating point parameter instead of 'v_Temp_Usr' in input script or data file (src/fix_nh.cpp:132) Last command: fix 2 no_rigid nvt temp v_Temp_Usr v_Temp_Usr 5.0
Is it not possible to use an updating variable like “v_Temp_Usr” inside the fix command specification?
Is it true that we must specify an exact number (or a number derived from a variable such as ${Temp_Usr}) for the fix command, and that this number will remain locked-in throughout the simulation?
If this is the case, I suppose a more feasible option would be to create a loop in which we can unfix and then fix a new target temperature at each iteration to update the coefficient.
Please be aware of the following really fundamental difficulties with what you’re trying to do:
Neither temperature nor pressure are properly defined for a microscopic configuration. The temperature and pressure estimators fluctuate wildly, as you would expect for very small systems simulated over very short timescales.
The Nose-Hoover thermostat is fundamentally designed to permit fast fluctuations of the temperature estimator in order to correctly sample the canonical ensemble. So even if there were such a thing as microscopic temperature, the NH thermostat would be the wrong tool for enforcing it instantaneously.
The pressure estimator is calculated based on a specified value of temperature. Giving the pressure a direct dependence on temperature (instead of indirectly through the material’s constitutive moduli) thus introduces a circular dependence which would be even less representative of any realistic material behaviour.