  Several fluctuation formulas are available for calculating elastic constants
from equilibrium correlation functions in computer simulations, but the ones
available for simulations at constant pressure exhibit slow convergence
properties and cannot be used for the determination of local elastic constants.
To overcome these drawbacks, we derive a stress-stress fluctuation formula in
the $NPT$ ensemble based on known expressions in the $NVT$ ensemble. We
validate the formula in the $NPT$ ensemble by calculating elastic constants for
the simple nearest-neighbor Lennard-Jones crystal and by comparing the results
with those obtained in the $NVT$ ensemble. For both local and bulk elastic
constants we find an excellent agreement between the simulated data in the two
ensembles. To demonstrate the usefulness of the new formula, we apply it to
determine the elastic constants of a simulated lipid bilayer.
