diff --git a/CHANGES/v2.1.txt b/CHANGES/v2.1.txt
index b33b27795eb174922fe63df2064fe525402a61f5..e6ea7615117133e92d2be9ca25dcd87a69dbd52e 100644
--- a/CHANGES/v2.1.txt
+++ b/CHANGES/v2.1.txt
@@ -183,6 +183,7 @@ See <a href="http://github.com/plumed/plumed2/commits/v2.1">branch v2.1 on git r
 For users:
 - Updated gromacs patch 5.0 to 5.0.4
 - Fixed bug in \ref ENSEMBLE derivatives when more than 1 argument was provided
+- Fixed bug in \ref GHOST : virial is now computed correctly.
 - Fixed a serious bug in virial communicated from plumed to gromacs, for both gromacs versions 4.6 and 5.0.
   Patch for gromacs 4.5 is not fixed. See notes <a href="http://github.com/plumed/plumed2/issues/132"> here </a>
   This fix requires gromacs to be repatched and could be very important if you run biased simulations in the NPT ensemble.
@@ -196,6 +197,8 @@ For users:
 For developers:
 - Allow external VMD plugins to be detected with --has-external-molfile. This
   is required to enable some regtest with amber files.
+- Added --dump-full-virial to \ref driver
+- Improved some regtest to check out-of-diagonal virial components
 
 
 
diff --git a/regtest/basic/rt-ghost-1/Makefile b/regtest/basic/rt-ghost-1/Makefile
new file mode 100644
index 0000000000000000000000000000000000000000..3703b27cea227aa053fb6d1d73f861e4384dbcee
--- /dev/null
+++ b/regtest/basic/rt-ghost-1/Makefile
@@ -0,0 +1 @@
+include ../../scripts/test.make
diff --git a/regtest/basic/rt-ghost-1/config b/regtest/basic/rt-ghost-1/config
new file mode 100644
index 0000000000000000000000000000000000000000..83a65f73deebe98818dbfac0e5425d34d2ae4ece
--- /dev/null
+++ b/regtest/basic/rt-ghost-1/config
@@ -0,0 +1,3 @@
+type=driver
+plumed_needs="matheval"
+arg="--plumed plumed.dat --ixyz trajectory.xyz --dump-forces ff --dump-forces-fmt=%10.6f --dump-full-virial"
diff --git a/regtest/basic/rt-ghost-1/ff.reference b/regtest/basic/rt-ghost-1/ff.reference
new file mode 100644
index 0000000000000000000000000000000000000000..c9c3d147f26801c36769bbac99c4051e0e7bf3f8
--- /dev/null
+++ b/regtest/basic/rt-ghost-1/ff.reference
@@ -0,0 +1,25 @@
+3
+  0.000000   2.000000   6.000000   0.000000   0.000000   0.000000   0.000000  -6.000000   0.000000
+X  -5.000000  -4.000000 -15.000000
+X   0.000000  -2.000000  -6.000000
+X   0.000000  -6.000000   0.000000
+3
+550.000000 682.000000 836.000000 550.000000 720.000000 910.000000 550.000000 754.000000 910.000000
+X -55.000000 -144.000000 -165.000000
+X   0.000000  38.000000  74.000000
+X   0.000000  34.000000   0.000000
+3
+  1.120000  -0.894427  -2.240000   0.000000   0.000000   0.000000   0.560000  -6.260990  -1.120000
+X   3.356068  -7.080650 -24.153466
+X  -1.120000   0.894427   2.240000
+X   0.000000  -5.813777   0.000000
+3
+  0.000000   0.000000  12.000000  -3.000000   0.000000   2.000000   0.000000   0.000000   0.000000
+X -18.000000  -6.000000 -24.000000
+X   3.000000   0.000000  -2.000000
+X   0.000000   0.000000  12.000000
+3
+ 16.157269  12.321067  26.478123   8.339769  35.731984  36.481190  21.645965  35.797572  -6.871304
+X -23.170419  -2.516138   1.507072
+X   0.866113  -1.984298  -7.323152
+X   3.548697  -1.867735   1.307415
diff --git a/regtest/basic/rt-ghost-1/plumed.dat b/regtest/basic/rt-ghost-1/plumed.dat
new file mode 100644
index 0000000000000000000000000000000000000000..90897c1fcbdb740ea89906ff25c8d282229a7f7b
--- /dev/null
+++ b/regtest/basic/rt-ghost-1/plumed.dat
@@ -0,0 +1,35 @@
+g: GHOST ATOMS=1,2,3 COORDINATES=1.0,2.0,3.0
+
+gpos: POSITION ATOM=g NOPBC
+
+# alternative explicit calculation:
+pos1: POSITION ATOM=1
+n12: DISTANCE ATOMS=1,2 COMPONENTS
+n13: DISTANCE ATOMS=1,3 COMPONENTS
+n12nx: MATHEVAL PERIODIC=NO ARG=n12.x,n12.y,n12.z FUNC=x/sqrt(x*x+y*y+z*z)
+n12ny: MATHEVAL PERIODIC=NO ARG=n12.x,n12.y,n12.z FUNC=y/sqrt(x*x+y*y+z*z)
+n12nz: MATHEVAL PERIODIC=NO ARG=n12.x,n12.y,n12.z FUNC=z/sqrt(x*x+y*y+z*z)
+n13nx: MATHEVAL PERIODIC=NO ARG=n13.x,n13.y,n13.z FUNC=x/sqrt(x*x+y*y+z*z)
+n13ny: MATHEVAL PERIODIC=NO ARG=n13.x,n13.y,n13.z FUNC=y/sqrt(x*x+y*y+z*z)
+n13nz: MATHEVAL PERIODIC=NO ARG=n13.x,n13.y,n13.z FUNC=z/sqrt(x*x+y*y+z*z)
+n14x:  MATHEVAL PERIODIC=NO ARG=n12nx,n12ny,n12nz,n13nx,n13ny,n13nz VAR=ax,ay,az,bx,by,bz FUNC=ay*bz-by*az+0*ax+0*bx
+n14y:  MATHEVAL PERIODIC=NO ARG=n12nx,n12ny,n12nz,n13nx,n13ny,n13nz VAR=ax,ay,az,bx,by,bz FUNC=az*bx-bz*ax+0*ay+0*by
+n14z:  MATHEVAL PERIODIC=NO ARG=n12nx,n12ny,n12nz,n13nx,n13ny,n13nz VAR=ax,ay,az,bx,by,bz FUNC=ax*by-bx*ay+0*az+0*bz
+n14nx: MATHEVAL PERIODIC=NO ARG=n14x,n14y,n14z FUNC=x/sqrt(x*x+y*y+z*z)
+n14ny: MATHEVAL PERIODIC=NO ARG=n14x,n14y,n14z FUNC=y/sqrt(x*x+y*y+z*z)
+n14nz: MATHEVAL PERIODIC=NO ARG=n14x,n14y,n14z FUNC=z/sqrt(x*x+y*y+z*z)
+n15x:  MATHEVAL PERIODIC=NO ARG=n12nx,n12ny,n12nz,n14nx,n14ny,n14nz VAR=ax,ay,az,bx,by,bz FUNC=ay*bz-by*az+0*ax+0*bx
+n15y:  MATHEVAL PERIODIC=NO ARG=n12nx,n12ny,n12nz,n14nx,n14ny,n14nz VAR=ax,ay,az,bx,by,bz FUNC=az*bx-bz*ax+0*ay+0*by
+n15z:  MATHEVAL PERIODIC=NO ARG=n12nx,n12ny,n12nz,n14nx,n14ny,n14nz VAR=ax,ay,az,bx,by,bz FUNC=ax*by-bx*ay+0*az+0*bz
+
+gposx: MATHEVAL PERIODIC=NO ARG=pos1.x,n12nx,n14nx,n15x VAR=a,b1,b2,b3 FUNC=a+1.0*b1+2.0*b2+3.0*b3
+gposy: MATHEVAL PERIODIC=NO ARG=pos1.y,n12ny,n14ny,n15y VAR=a,b1,b2,b3 FUNC=a+1.0*b1+2.0*b2+3.0*b3
+gposz: MATHEVAL PERIODIC=NO ARG=pos1.z,n12nz,n14nz,n15z VAR=a,b1,b2,b3 FUNC=a+1.0*b1+2.0*b2+3.0*b3
+
+#RESTRAINT ARG=gposx,gposy,gposz KAPPA=5.0,6.0,7.0 AT=0.0,0.0,0.0
+
+RESTRAINT ARG=gpos.x,gpos.y,gpos.z KAPPA=5.0,6.0,7.0 AT=0.0,0.0,0.0
+
+PRINT FILE=colvar ARG=gpos.x,gpos.y,gpos.z,gposx,gposy,gposz
+
+DUMPATOMS ATOMS=g FILE=x
diff --git a/regtest/basic/rt-ghost-1/trajectory.xyz b/regtest/basic/rt-ghost-1/trajectory.xyz
new file mode 100644
index 0000000000000000000000000000000000000000..bc995ea7a6e286d2a9856280a67624ea93a0aeed
--- /dev/null
+++ b/regtest/basic/rt-ghost-1/trajectory.xyz
@@ -0,0 +1,25 @@
+3
+0 0 0 
+X 0 0 0
+X 1 0 0
+X 0 0 -1
+3
+4 4 4
+X 10 10 10
+X 11 10 10
+X 10 10 9
+3
+0 0 0 
+X 0 0 0
+X 1 0 0.5
+X 0 0 -1
+3
+0 0 0
+X 0 0 0
+X 0 1 0
+X -1 0 0
+3
+100 1 1 1 100 1 1 1 100
+X 1 2 3
+X 4 7 2
+X 1 9 13
diff --git a/regtest/basic/rt-ghost-1/x.reference b/regtest/basic/rt-ghost-1/x.reference
new file mode 100644
index 0000000000000000000000000000000000000000..3c1c3bb4723c4305b4e277e7c56f5a578c5565f7
--- /dev/null
+++ b/regtest/basic/rt-ghost-1/x.reference
@@ -0,0 +1,15 @@
+1
+ 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
+X 1.000000 2.000000 3.000000
+1
+ 4.000000 4.000000 4.000000
+X 11.000000 12.000000 13.000000
+1
+ 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
+X -0.447214 2.000000 3.130495
+1
+ 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
+X 3.000000 1.000000 2.000000
+1
+ 100.000000 1.000000 1.000000 1.000000 100.000000 1.000000 1.000000 1.000000 100.000000
+X 3.751122 1.061362 0.644095
diff --git a/regtest/basic/rt-ghost-2/Makefile b/regtest/basic/rt-ghost-2/Makefile
new file mode 100644
index 0000000000000000000000000000000000000000..3703b27cea227aa053fb6d1d73f861e4384dbcee
--- /dev/null
+++ b/regtest/basic/rt-ghost-2/Makefile
@@ -0,0 +1 @@
+include ../../scripts/test.make
diff --git a/regtest/basic/rt-ghost-2/config b/regtest/basic/rt-ghost-2/config
new file mode 100644
index 0000000000000000000000000000000000000000..7e5cfb28124c1b1b464f9da17b9f44abcd7aa9b6
--- /dev/null
+++ b/regtest/basic/rt-ghost-2/config
@@ -0,0 +1,2 @@
+type=driver
+arg="--plumed plumed.dat --ixyz trajectory.xyz --dump-forces ff --dump-forces-fmt=%10.6f --dump-full-virial"
diff --git a/regtest/basic/rt-ghost-2/ff.reference b/regtest/basic/rt-ghost-2/ff.reference
new file mode 100644
index 0000000000000000000000000000000000000000..c350c62a269d30e18d5efa168110ff3569618e31
--- /dev/null
+++ b/regtest/basic/rt-ghost-2/ff.reference
@@ -0,0 +1,8 @@
+6
+  1.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000
+X   1.000000   0.000000   0.000000
+X   0.000000   0.000000   0.000000
+X   0.000000   0.000000   0.000000
+X  -1.000000   0.000000   0.000000
+X   0.000000   0.000000   0.000000
+X   0.000000   0.000000   0.000000
diff --git a/regtest/basic/rt-ghost-2/plumed.dat b/regtest/basic/rt-ghost-2/plumed.dat
new file mode 100644
index 0000000000000000000000000000000000000000..fc2c7b9a0433f4d57b5e3e4bf137d1b5f921b6ec
--- /dev/null
+++ b/regtest/basic/rt-ghost-2/plumed.dat
@@ -0,0 +1,8 @@
+# this is to test virial correction with pbc
+# the two ghost atoms are far away but close when distance is computed with pbc
+g1: GHOST ATOMS=1,2,3 COORDINATES=1.0,0.0,0.0
+g2: GHOST ATOMS=4,5,6 COORDINATES=1.0,0.0,0.0
+d: DISTANCE ATOMS=g1,g2
+RESTRAINT ARG=d AT=0.0 KAPPA=1.0
+
+DUMPATOMS ATOMS=1-6,g1,g2 FILE=x
diff --git a/regtest/basic/rt-ghost-2/trajectory.xyz b/regtest/basic/rt-ghost-2/trajectory.xyz
new file mode 100644
index 0000000000000000000000000000000000000000..45de7229cfa52c20ac5acbef256f2a664ac5bd1c
--- /dev/null
+++ b/regtest/basic/rt-ghost-2/trajectory.xyz
@@ -0,0 +1,8 @@
+6
+19 19 19
+X 0 0 0
+X 1 0 0
+X 0 0 -1
+X 20 0 0
+X 21 0 0
+X 20 0 -1
diff --git a/regtest/basic/rt-ghost-2/x.reference b/regtest/basic/rt-ghost-2/x.reference
new file mode 100644
index 0000000000000000000000000000000000000000..e5bc963907dbd03f88f2e4413111a1f5e724b798
--- /dev/null
+++ b/regtest/basic/rt-ghost-2/x.reference
@@ -0,0 +1,10 @@
+8
+ 19.000000 19.000000 19.000000
+X 0.000000 0.000000 0.000000
+X 1.000000 0.000000 0.000000
+X 0.000000 0.000000 -1.000000
+X 20.000000 0.000000 0.000000
+X 21.000000 0.000000 0.000000
+X 20.000000 0.000000 -1.000000
+X 1.000000 0.000000 0.000000
+X 21.000000 0.000000 0.000000
diff --git a/regtest/basic/rt63/config b/regtest/basic/rt63/config
index 597366588d82fb777644d17da03116d5e089d7a4..59b16dd31f47b2737542196fc8b694414e9e0fcc 100644
--- a/regtest/basic/rt63/config
+++ b/regtest/basic/rt63/config
@@ -1,2 +1,2 @@
 type=driver
-arg="--plumed plumed.dat --trajectory-stride 10 --timestep 0.005 --igro helix.input.gro --dump-forces ff"
+arg="--plumed plumed.dat --trajectory-stride 10 --timestep 0.005 --igro helix.input.gro --dump-forces ff --dump-full-virial"
diff --git a/regtest/basic/rt63/ff.reference b/regtest/basic/rt63/ff.reference
index 79604596d8e8445e6441b6d3195498b2ce05c53a..d2a5a87ceca309e932a8c95d1b873e7c7a87f5a2 100644
--- a/regtest/basic/rt63/ff.reference
+++ b/regtest/basic/rt63/ff.reference
@@ -1,5 +1,5 @@
 132
--0.045000 0.000000 0.000000
+-0.045000 0.000000 0.000000 -0.078333 0.000000 0.000000 0.125000 0.000000 0.000000
 X 0.673333 0.013333 0.020000
 X 0.336667 0.006667 0.010000
 X -1.010000 -0.020000 -0.030000
@@ -133,7 +133,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.041667 0.000000 0.000000
+-0.041667 0.000000 0.000000 -0.139333 0.000000 0.000000 0.024000 0.000000 0.000000
 X 0.673333 0.013333 0.020000
 X 0.336667 0.006667 0.010000
 X -1.010000 -0.020000 -0.030000
@@ -267,7 +267,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.102333 0.000000 0.000000
+-0.102333 0.000000 0.000000 -0.031000 0.000000 0.000000 0.091333 0.000000 0.000000
 X 0.673333 0.013333 0.020000
 X 0.336667 0.006667 0.010000
 X -1.010000 -0.020000 -0.030000
@@ -401,7 +401,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
-0.114333 0.000000 0.000000
+0.114333 0.000000 0.000000 -0.094000 0.000000 0.000000 -0.045333 0.000000 0.000000
 X 0.673333 0.013333 0.020000
 X 0.336667 0.006667 0.010000
 X -1.010000 -0.020000 -0.030000
@@ -535,7 +535,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.092000 0.000000 0.000000
+-0.092000 0.000000 0.000000 0.100333 0.000000 0.000000 -0.056333 0.000000 0.000000
 X 0.673333 0.013333 0.020000
 X 0.336667 0.006667 0.010000
 X -1.010000 -0.020000 -0.030000
@@ -669,7 +669,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
-0.149333 0.000000 0.000000
+0.149333 0.000000 0.000000 0.023333 0.000000 0.000000 0.009333 0.000000 0.000000
 X 0.673333 0.013333 0.020000
 X 0.336667 0.006667 0.010000
 X -1.010000 -0.020000 -0.030000
@@ -803,7 +803,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.068333 0.000000 0.000000
+-0.068333 0.000000 0.000000 -0.138000 0.000000 0.000000 -0.002333 0.000000 0.000000
 X 0.673333 0.013333 0.020000
 X 0.336667 0.006667 0.010000
 X -1.010000 -0.020000 -0.030000
@@ -937,7 +937,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.130000 0.000000 0.000000
+-0.130000 0.000000 0.000000 0.048667 0.000000 0.000000 0.060667 0.000000 0.000000
 X 0.673333 0.013333 0.020000
 X 0.336667 0.006667 0.010000
 X -1.010000 -0.020000 -0.030000
@@ -1071,7 +1071,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.058000 0.000000 0.000000
+-0.058000 0.000000 0.000000 0.119667 0.000000 0.000000 -0.053000 0.000000 0.000000
 X 0.673333 0.013333 0.020000
 X 0.336667 0.006667 0.010000
 X -1.010000 -0.020000 -0.030000
@@ -1205,7 +1205,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.052667 0.000000 0.000000
+-0.052667 0.000000 0.000000 -0.096333 0.000000 0.000000 -0.101667 0.000000 0.000000
 X 0.673333 0.013333 0.020000
 X 0.336667 0.006667 0.010000
 X -1.010000 -0.020000 -0.030000
@@ -1339,7 +1339,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.001333 0.000000 0.000000
+-0.001333 0.000000 0.000000 0.100000 0.000000 0.000000 0.120333 0.000000 0.000000
 X 0.673333 0.013333 0.020000
 X 0.336667 0.006667 0.010000
 X -1.010000 -0.020000 -0.030000
@@ -1473,7 +1473,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.012333 0.000000 0.000000
+-0.012333 0.000000 0.000000 0.095667 0.000000 0.000000 0.120667 0.000000 0.000000
 X 0.673333 0.013333 0.020000
 X 0.336667 0.006667 0.010000
 X -1.010000 -0.020000 -0.030000
diff --git a/regtest/basic/rt63b/config b/regtest/basic/rt63b/config
index 597366588d82fb777644d17da03116d5e089d7a4..59b16dd31f47b2737542196fc8b694414e9e0fcc 100644
--- a/regtest/basic/rt63b/config
+++ b/regtest/basic/rt63b/config
@@ -1,2 +1,2 @@
 type=driver
-arg="--plumed plumed.dat --trajectory-stride 10 --timestep 0.005 --igro helix.input.gro --dump-forces ff"
+arg="--plumed plumed.dat --trajectory-stride 10 --timestep 0.005 --igro helix.input.gro --dump-forces ff --dump-full-virial"
diff --git a/regtest/basic/rt63b/ff.reference b/regtest/basic/rt63b/ff.reference
index 10c4d5c93e74cc1abc3e4123060b2fec97bb9fee..6d7f3fe68e823b9ea594d0bc1a42b9b8e70772f5 100644
--- a/regtest/basic/rt63b/ff.reference
+++ b/regtest/basic/rt63b/ff.reference
@@ -1,5 +1,5 @@
 132
--0.045000 -0.156667 0.375000
+-0.045000 -0.090000 -0.135000 -0.078333 -0.156667 -0.235000 0.125000 0.250000 0.375000
 X 0.666667 1.333333 2.000000
 X 0.333333 0.666667 1.000000
 X -1.000000 -2.000000 -3.000000
@@ -133,7 +133,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.041667 -0.278667 0.072000
+-0.041667 -0.083333 -0.125000 -0.139333 -0.278667 -0.418000 0.024000 0.048000 0.072000
 X 0.666667 1.333333 2.000000
 X 0.333333 0.666667 1.000000
 X -1.000000 -2.000000 -3.000000
@@ -267,7 +267,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.102333 -0.062000 0.274000
+-0.102333 -0.204667 -0.307000 -0.031000 -0.062000 -0.093000 0.091333 0.182667 0.274000
 X 0.666667 1.333333 2.000000
 X 0.333333 0.666667 1.000000
 X -1.000000 -2.000000 -3.000000
@@ -401,7 +401,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
-0.114333 -0.188000 -0.136000
+0.114333 0.228667 0.343000 -0.094000 -0.188000 -0.282000 -0.045333 -0.090667 -0.136000
 X 0.666667 1.333333 2.000000
 X 0.333333 0.666667 1.000000
 X -1.000000 -2.000000 -3.000000
@@ -535,7 +535,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.092000 0.200667 -0.169000
+-0.092000 -0.184000 -0.276000 0.100333 0.200667 0.301000 -0.056333 -0.112667 -0.169000
 X 0.666667 1.333333 2.000000
 X 0.333333 0.666667 1.000000
 X -1.000000 -2.000000 -3.000000
@@ -669,7 +669,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
-0.149333 0.046667 0.028000
+0.149333 0.298667 0.448000 0.023333 0.046667 0.070000 0.009333 0.018667 0.028000
 X 0.666667 1.333333 2.000000
 X 0.333333 0.666667 1.000000
 X -1.000000 -2.000000 -3.000000
@@ -803,7 +803,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.068333 -0.276000 -0.007000
+-0.068333 -0.136667 -0.205000 -0.138000 -0.276000 -0.414000 -0.002333 -0.004667 -0.007000
 X 0.666667 1.333333 2.000000
 X 0.333333 0.666667 1.000000
 X -1.000000 -2.000000 -3.000000
@@ -937,7 +937,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.130000 0.097333 0.182000
+-0.130000 -0.260000 -0.390000 0.048667 0.097333 0.146000 0.060667 0.121333 0.182000
 X 0.666667 1.333333 2.000000
 X 0.333333 0.666667 1.000000
 X -1.000000 -2.000000 -3.000000
@@ -1071,7 +1071,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.058000 0.239333 -0.159000
+-0.058000 -0.116000 -0.174000 0.119667 0.239333 0.359000 -0.053000 -0.106000 -0.159000
 X 0.666667 1.333333 2.000000
 X 0.333333 0.666667 1.000000
 X -1.000000 -2.000000 -3.000000
@@ -1205,7 +1205,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.052667 -0.192667 -0.305000
+-0.052667 -0.105333 -0.158000 -0.096333 -0.192667 -0.289000 -0.101667 -0.203333 -0.305000
 X 0.666667 1.333333 2.000000
 X 0.333333 0.666667 1.000000
 X -1.000000 -2.000000 -3.000000
@@ -1339,7 +1339,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.001333 0.200000 0.361000
+-0.001333 -0.002667 -0.004000 0.100000 0.200000 0.300000 0.120333 0.240667 0.361000
 X 0.666667 1.333333 2.000000
 X 0.333333 0.666667 1.000000
 X -1.000000 -2.000000 -3.000000
@@ -1473,7 +1473,7 @@ X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 X 0.000000 0.000000 0.000000
 132
--0.012333 0.191333 0.362000
+-0.012333 -0.024667 -0.037000 0.095667 0.191333 0.287000 0.120667 0.241333 0.362000
 X 0.666667 1.333333 2.000000
 X 0.333333 0.666667 1.000000
 X -1.000000 -2.000000 -3.000000
diff --git a/src/cltools/Driver.cpp b/src/cltools/Driver.cpp
index f87d5115ba11e6fa50bb746616be6ce7631c680d..c887f732fd814f5287a818eae55d6b4fb5248227 100644
--- a/src/cltools/Driver.cpp
+++ b/src/cltools/Driver.cpp
@@ -179,6 +179,7 @@ void Driver<real>::registerKeywords( Keywords& keys ){
   keys.add("optional","--length-units","units for length, either as a string or a number");
   keys.add("optional","--dump-forces","dump the forces on a file");
   keys.add("optional","--dump-forces-fmt","( default=%%f ) the format to use to dump the forces");
+  keys.addFlag("--dump-full-virial",false,"with --dump-forces, it dumps the 9 components of the virial");
   keys.add("optional","--pdb","provides a pdb with masses and charges");
   keys.add("optional","--mc","provides a file with masses and charges as produced with DUMPMASSCHARGE");
   keys.add("optional","--box","comma-separated box dimensions (3 for orthorombic, 9 for generic)");
@@ -285,8 +286,10 @@ int Driver<real>::main(FILE* in,FILE*out,Communicator& pc){
   unsigned stride; parse("--trajectory-stride",stride);
 // are we writing forces
   string dumpforces(""), dumpforcesFmt("%f");; 
+  bool dumpfullvirial=false;
   if(!noatoms) parse("--dump-forces",dumpforces);
   if(dumpforces!="") parse("--dump-forces-fmt",dumpforcesFmt);
+  if(dumpforces!="") parseFlag("--dump-full-virial",dumpfullvirial);
 
   string trajectory_fmt;
 
@@ -803,8 +806,13 @@ int Driver<real>::main(FILE* in,FILE*out,Communicator& pc){
 
    if(fp_forces){
      fprintf(fp_forces,"%d\n",natoms);
+     string fmtv=dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+"\n";
      string fmt=dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+"\n";
-     fprintf(fp_forces,fmt.c_str(),virial[0],virial[4],virial[8]);
+     if(dumpfullvirial){
+       fprintf(fp_forces,fmtv.c_str(),virial[0],virial[1],virial[2],virial[3],virial[4],virial[5],virial[6],virial[7],virial[8]);
+     } else {
+       fprintf(fp_forces,fmt.c_str(),virial[0],virial[4],virial[8]);
+     }
      fmt="X "+fmt;
      for(int i=0;i<natoms;i++)
        fprintf(fp_forces,fmt.c_str(),forces[3*i],forces[3*i+1],forces[3*i+2]);
diff --git a/src/core/ActionAtomistic.cpp b/src/core/ActionAtomistic.cpp
index 2b794873c480e97364061447ceda42c9c651d482..a392b09a8bd9fc19fc9ff754615a7060ce9c0dc1 100644
--- a/src/core/ActionAtomistic.cpp
+++ b/src/core/ActionAtomistic.cpp
@@ -250,6 +250,7 @@ void ActionAtomistic::applyForces(){
 }
 
 void ActionAtomistic::clearOutputForces(){
+  virial.zero();
   if(donotforce) return;
   for(unsigned i=0;i<forces.size();++i)forces[i].zero();
   forceOnEnergy=0.0;
diff --git a/src/core/ActionWithVirtualAtom.cpp b/src/core/ActionWithVirtualAtom.cpp
index df3b09ccbc8919dc6cbde932f93f6125c9bc6225..ae05fb724dc4f3bc117813b6f277b38d93971377 100644
--- a/src/core/ActionWithVirtualAtom.cpp
+++ b/src/core/ActionWithVirtualAtom.cpp
@@ -34,7 +34,8 @@ void ActionWithVirtualAtom::registerKeywords(Keywords& keys){
 
 ActionWithVirtualAtom::ActionWithVirtualAtom(const ActionOptions&ao):
   Action(ao),
-  ActionAtomistic(ao)
+  ActionAtomistic(ao),
+  boxDerivatives(3)
 {
   index=atoms.addVirtualAtom(this);
   log.printf("  serial associated to this virtual atom is %u\n",index.serial());
@@ -47,6 +48,8 @@ ActionWithVirtualAtom::~ActionWithVirtualAtom(){
 void ActionWithVirtualAtom::apply(){
   const Vector & f(atoms.forces[index.index()]);
   for(unsigned i=0;i<getNumberOfAtoms();i++) modifyForces()[i]=matmul(derivatives[i],f);
+  Tensor & v(modifyVirial());
+  for(unsigned i=0;i<3;i++) v+=boxDerivatives[i]*f[i];
 }
 
 void ActionWithVirtualAtom::requestAtoms(const std::vector<AtomNumber> & a){
@@ -71,6 +74,15 @@ void ActionWithVirtualAtom::setGradients(){
   }
 }
 
+void ActionWithVirtualAtom::setBoxDerivatives(const std::vector<Tensor> &d){
+  boxDerivatives=d;
+// Subtract the trivial part coming from a distorsion applied to the ghost atom first.
+// Notice that this part alone should exactly cancel the already accumulated virial
+// due to forces on this atom.
+  Vector pos=atoms.positions[index.index()];
+  for(unsigned i=0;i<3;i++) for(unsigned j=0;j<3;j++) boxDerivatives[j][i][j]+=pos[i];
+}
+
 
 void ActionWithVirtualAtom::setGradientsIfNeeded(){
   if(isOptionOn("GRADIENTS")) { 
diff --git a/src/core/ActionWithVirtualAtom.h b/src/core/ActionWithVirtualAtom.h
index 7bee0f36ad75e31b9cefda66716d1287ac229a8c..69397e98601d2a88f2769e970984958e08317e64 100644
--- a/src/core/ActionWithVirtualAtom.h
+++ b/src/core/ActionWithVirtualAtom.h
@@ -42,6 +42,7 @@ class ActionWithVirtualAtom:
 {
   AtomNumber index;
   std::vector<Tensor> derivatives;
+  std::vector<Tensor> boxDerivatives;
   std::map<AtomNumber,Tensor> gradients;
   void apply();
 protected:
@@ -55,6 +56,16 @@ protected:
   void requestAtoms(const std::vector<AtomNumber> & a);
 /// Set the derivatives of virtual atom coordinate wrt atoms on which it dependes
   void setAtomsDerivatives(const std::vector<Tensor> &d);
+/// Set the box derivatives.
+/// This should be a vector of size 3. First index corresponds
+/// to the components of the virtual atom.
+/// Notice that this routine subtract the trivial term coming from cell deformation
+/// since this term is already implicitly included. Indeed, if the vatom
+/// position is a linear function of atomic coordinates it is not necessary
+/// to call this function (implicit term is fine) (e.g. vatom::COM and vatom::Center).
+/// On the other hand if the vatom position is a non-linear function of atomic coordinates this
+/// should be called (see vatom::Ghost).
+  void setBoxDerivatives(const std::vector<Tensor> &d);
 public:
   void setGradients();
   const std::map<AtomNumber,Tensor> & getGradients()const;
diff --git a/src/vatom/Ghost.cpp b/src/vatom/Ghost.cpp
index d61a14df36928522aff945e41657471110a553ae..9d123eed31b017c12ece3892d6883179d6acb361 100644
--- a/src/vatom/Ghost.cpp
+++ b/src/vatom/Ghost.cpp
@@ -47,10 +47,6 @@ PRINT ARG=d1
 \endverbatim
 (See also \ref DISTANCE and \ref PRINT).
 
-\warning If a force is added to a ghost atom, the contribution
-to the virial could contain small artifacts. It is therefore discouraged to
-use GHOST in a constant pressure simulation.
-
 */
 //+ENDPLUMEDOC
 
@@ -87,7 +83,6 @@ Ghost::Ghost(const ActionOptions&ao):
   log.printf("  of atoms");
   for(unsigned i=0;i<atoms.size();++i) log.printf(" %d",atoms[i].serial());
   log.printf("\n");
-  log<<"  WARNING: GHOST does not include virial contributions, please avoid using it with constant pressure molecular dynamics\n";
   requestAtoms(atoms);
 }
 
@@ -95,6 +90,11 @@ void Ghost::calculate(){
   Vector pos;
   vector<Tensor> deriv(getNumberOfAtoms());
   vector<Vector> n;
+  Vector pp[3];
+
+  pp[0]=getPosition(0);
+  pp[1]=pp[0]+delta(getPosition(0), getPosition(1));
+  pp[2]=pp[0]+delta(getPosition(0), getPosition(2));
 
 // first versor 
   Vector n01 = delta(getPosition(0), getPosition(1));
@@ -182,6 +182,19 @@ void Ghost::calculate(){
   deriv[2] =                                       coord[1]*dn1d2 + coord[2]*dn2d2;
 
   setAtomsDerivatives(deriv);
+
+// Virial contribution
+  std::vector<Tensor> bd(3);
+  for(unsigned i=0;i<3;i++) for(unsigned j=0;j<3;j++) for(unsigned k=0;k<3;k++){
+// Notice that this expression is very similar to the one used in Colvar::setBoxDerivativesNoPbc().
+// Indeed, we have the negative of a sum over dependent atoms (l) of the external product between positions
+// and derivatives. Notice that positions here should be computed in the same periodic image
+// as pos.
+    for(unsigned l=0;l<3;l++){
+      bd[k][i][j]-=pp[l][i]*deriv[l][j][k];
+    }
+  }
+  setBoxDerivatives(bd);
 }
 
 }