From 9b644057a06e98bf9fbf0831ba0666904e87d41c Mon Sep 17 00:00:00 2001 From: Gareth Aneurin Tribello Date: Fri, 12 Apr 2024 08:48:53 +0100 Subject: [PATCH] Fixed a bug that appears when you try to sum all the elements in a matrix --- regtest/adjmat/rt-matrixsum/Makefile | 1 + regtest/adjmat/rt-matrixsum/colvar.reference | 18 ++ regtest/adjmat/rt-matrixsum/config | 5 + regtest/adjmat/rt-matrixsum/forces.reference | 272 +++++++++++++++++++ regtest/adjmat/rt-matrixsum/plumed.dat | 6 + regtest/symfunc/rt-smac/forces.reference | 4 +- src/adjmat/ActionWithMatrix.cpp | 5 +- src/adjmat/ActionWithMatrix.h | 2 + src/adjmat/AdjacencyMatrixBase.h | 1 + src/adjmat/FunctionOfMatrix.h | 8 +- src/adjmat/MatrixTimesMatrix.cpp | 5 +- src/core/ActionWithVector.cpp | 3 +- 12 files changed, 319 insertions(+), 11 deletions(-) create mode 100644 regtest/adjmat/rt-matrixsum/Makefile create mode 100644 regtest/adjmat/rt-matrixsum/colvar.reference create mode 100644 regtest/adjmat/rt-matrixsum/config create mode 100644 regtest/adjmat/rt-matrixsum/forces.reference create mode 100644 regtest/adjmat/rt-matrixsum/plumed.dat diff --git a/regtest/adjmat/rt-matrixsum/Makefile b/regtest/adjmat/rt-matrixsum/Makefile new file mode 100644 index 0000000000..3703b27cea --- /dev/null +++ b/regtest/adjmat/rt-matrixsum/Makefile @@ -0,0 +1 @@ +include ../../scripts/test.make diff --git a/regtest/adjmat/rt-matrixsum/colvar.reference b/regtest/adjmat/rt-matrixsum/colvar.reference new file mode 100644 index 0000000000..131bd01eb7 --- /dev/null +++ b/regtest/adjmat/rt-matrixsum/colvar.reference @@ -0,0 +1,18 @@ +#! FIELDS time s + 0.000000 0.1870 + 1.000000 0.1453 + 2.000000 0.5477 + 3.000000 -0.6148 + 4.000000 -0.7783 + 5.000000 -4.3542 + 6.000000 2.8148 + 7.000000 0.1168 + 8.000000 2.1041 + 9.000000 1.6821 + 10.000000 -2.4892 + 11.000000 -1.7454 + 12.000000 4.1613 + 13.000000 4.8999 + 14.000000 4.9588 + 15.000000 0.0031 + 16.000000 0.5671 diff --git a/regtest/adjmat/rt-matrixsum/config b/regtest/adjmat/rt-matrixsum/config new file mode 100644 index 0000000000..a5e321ba9c --- /dev/null +++ b/regtest/adjmat/rt-matrixsum/config @@ -0,0 +1,5 @@ +type=driver +plumed_modules=adjmat +# this is to test a different name +arg="--plumed plumed.dat --ixyz isomers.xyz --dump-forces forces --dump-forces-fmt=%8.4f" # --debug-forces forces.num" +extra_files="../../trajectories/isomers.xyz" diff --git a/regtest/adjmat/rt-matrixsum/forces.reference b/regtest/adjmat/rt-matrixsum/forces.reference new file mode 100644 index 0000000000..f996902d51 --- /dev/null +++ b/regtest/adjmat/rt-matrixsum/forces.reference @@ -0,0 +1,272 @@ +14 + 0.0551 0.1000 0.0318 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + 0.0891 0.0179 0.0383 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + 0.7019 -0.0828 -0.0714 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + -0.5030 -0.6063 0.4945 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + -0.7070 -0.5915 0.5203 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + -0.0001 0.0006 -4.3546 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + 0.8508 2.7512 -0.7873 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + -0.8875 0.8609 0.1435 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + 1.2470 1.6980 -0.8409 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + 1.2582 1.0255 -0.6016 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + -1.5972 -0.4468 -0.4453 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + -0.7369 0.0000 -1.0085 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + 3.0714 1.5978 -0.5078 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + 2.8973 2.0113 -0.0086 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + 2.8124 2.0181 0.1283 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + -0.0714 0.0767 -0.0021 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +14 + 0.0625 0.0127 0.4918 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 1.0000 1.0000 1.0000 +X -1.0000 -1.0000 -1.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 +X 0.0000 0.0000 0.0000 diff --git a/regtest/adjmat/rt-matrixsum/plumed.dat b/regtest/adjmat/rt-matrixsum/plumed.dat new file mode 100644 index 0000000000..c6c6cdbcc0 --- /dev/null +++ b/regtest/adjmat/rt-matrixsum/plumed.dat @@ -0,0 +1,6 @@ +d1: DISTANCE COMPONENTS ATOMS1=1,2 ATOMS2=3,4 ATOMS3=5,6 +v: VSTACK ARG=d1.x,d1.y,d1.z +vT: TRANSPOSE ARG=v +s: SUM ARG=vT PERIODIC=NO +PRINT ARG=s FILE=colvar FMT=%8.4f +BIASVALUE ARG=s diff --git a/regtest/symfunc/rt-smac/forces.reference b/regtest/symfunc/rt-smac/forces.reference index 5a31cf543f..f1a9e9b760 100644 --- a/regtest/symfunc/rt-smac/forces.reference +++ b/regtest/symfunc/rt-smac/forces.reference @@ -167398,8 +167398,8 @@ X 0.0000 0.0000 0.0000 X 0.0000 0.0000 0.0000 X 0.0000 0.0000 0.0000 X 0.0000 0.0000 0.0000 -X -0.0028 -0.0064 -0.0065 -X -0.0006 0.0000 0.0054 +X -0.0028 0.0150 -0.0065 +X -0.0006 -0.0214 0.0054 X 0.0000 0.0000 0.0000 X 0.0000 0.0000 0.0000 X 0.0000 0.0000 0.0000 diff --git a/src/adjmat/ActionWithMatrix.cpp b/src/adjmat/ActionWithMatrix.cpp index 30d0e4a137..79fb0feedc 100644 --- a/src/adjmat/ActionWithMatrix.cpp +++ b/src/adjmat/ActionWithMatrix.cpp @@ -20,7 +20,6 @@ along with plumed. If not, see . +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ #include "ActionWithMatrix.h" -#include "AdjacencyMatrixBase.h" namespace PLMD { namespace adjmat { @@ -72,7 +71,7 @@ void ActionWithMatrix::finishChainBuild( ActionWithVector* act ) { else { next_action_in_chain=am; // Build the list of things we are going to loop over in runTask - AdjacencyMatrixBase* aa=dynamic_cast(act); if( aa || act->getName()=="VSTACK" ) return ; + if( am->isAdjacencyMatrix() || act->getName()=="VSTACK" ) return ; plumed_massert( !matrix_to_do_after, "cannot add " + act->getLabel() + " in " + getLabel() + " as have already added " + matrix_to_do_after->getLabel() ); matrix_to_do_after=am; am->matrix_to_do_before=this; } @@ -134,7 +133,7 @@ void ActionWithMatrix::performTask( const unsigned& task_index, MultiValue& myva void ActionWithMatrix::runTask( const std::string& controller, const unsigned& current, const unsigned colno, MultiValue& myvals ) const { double outval=0; myvals.setTaskIndex(current); myvals.setSecondTaskIndex( colno ); if( isActive() ) performTask( controller, current, colno, myvals ); - bool hasval=false; + bool hasval = !isAdjacencyMatrix(); for(int i=0; igetPositionInStream()) )>0 ) { hasval=true; break; } } diff --git a/src/adjmat/ActionWithMatrix.h b/src/adjmat/ActionWithMatrix.h index 1f062e0aaa..a5a08724e5 100644 --- a/src/adjmat/ActionWithMatrix.h +++ b/src/adjmat/ActionWithMatrix.h @@ -59,6 +59,8 @@ class ActionWithMatrix : public ActionWithVector { static void registerKeywords( Keywords& keys ); explicit ActionWithMatrix(const ActionOptions&); virtual ~ActionWithMatrix(); +/// + virtual bool isAdjacencyMatrix() const { return false; } /// void getAllActionLabelsInMatrixChain( std::vector& mylabels ) const override ; /// Get the first matrix in this chain diff --git a/src/adjmat/AdjacencyMatrixBase.h b/src/adjmat/AdjacencyMatrixBase.h index f1a8b95f04..9053da9713 100644 --- a/src/adjmat/AdjacencyMatrixBase.h +++ b/src/adjmat/AdjacencyMatrixBase.h @@ -49,6 +49,7 @@ class AdjacencyMatrixBase : public ActionWithMatrix { public: static void registerKeywords( Keywords& keys ); explicit AdjacencyMatrixBase(const ActionOptions&); + bool isAdjacencyMatrix() const override { return true; } unsigned getNumberOfDerivatives() override ; unsigned getNumberOfColumns() const override; void prepare() override; diff --git a/src/adjmat/FunctionOfMatrix.h b/src/adjmat/FunctionOfMatrix.h index 3a1d49777a..a332fe356b 100644 --- a/src/adjmat/FunctionOfMatrix.h +++ b/src/adjmat/FunctionOfMatrix.h @@ -324,12 +324,16 @@ void FunctionOfMatrix::performTask( const std::string& controller, const unsi } } else { unsigned base=0; ind2 = index2; - if( index2>=getConstPntrToComponent(0)->getShape()[0] ) ind2 = index2 - getConstPntrToComponent(0)->getShape()[0]; + for(unsigned j=argstart; jgetRank()!=2 ) continue ; + if( index2>=getPntrToArgument(j)->getShape()[0] ) ind2 = index2 - getPntrToArgument(j)->getShape()[0]; + break; + } for(unsigned j=argstart; jgetRank()==2 ) { for(int i=0; igetPositionInStream(); - unsigned myind = base + getConstPntrToComponent(i)->getShape()[1]*index1 + ind2; + unsigned myind = base + getPntrToArgument(j)->getShape()[1]*index1 + ind2; myvals.addDerivative( ostrn, myind, derivatives(i,j) ); myvals.updateIndex( ostrn, myind ); } diff --git a/src/adjmat/MatrixTimesMatrix.cpp b/src/adjmat/MatrixTimesMatrix.cpp index 3ffb850d40..4f5e2a56b1 100644 --- a/src/adjmat/MatrixTimesMatrix.cpp +++ b/src/adjmat/MatrixTimesMatrix.cpp @@ -20,7 +20,6 @@ along with plumed. If not, see . +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ #include "ActionWithMatrix.h" -#include "AdjacencyMatrixBase.h" #include "core/ActionRegister.h" //+PLUMEDOC MCOLVAR MATRIX_PRODUCT @@ -101,8 +100,8 @@ void MatrixTimesMatrix::prepare() { void MatrixTimesMatrix::getAdditionalTasksRequired( ActionWithVector* action, std::vector& atasks ) { - AdjacencyMatrixBase* adj=dynamic_cast( getPntrToArgument(0)->getPntrToAction() ); - if( !adj ) return; + ActionWithMatrix* adj=dynamic_cast( getPntrToArgument(0)->getPntrToAction() ); + if( !adj->isAdjacencyMatrix() ) return; adj->retrieveAtoms(); adj->getAdditionalTasksRequired( action, atasks ); } diff --git a/src/core/ActionWithVector.cpp b/src/core/ActionWithVector.cpp index 68fcf438aa..6871a9b4cf 100644 --- a/src/core/ActionWithVector.cpp +++ b/src/core/ActionWithVector.cpp @@ -512,7 +512,8 @@ bool ActionWithVector::checkForGrids( unsigned& nder ) const { void ActionWithVector::getNumberOfTasks( unsigned& ntasks ) { if( ntasks==0 ) { if( getNumberOfArguments()==1 && getNumberOfComponents()==1 && getPntrToComponent(0)->getRank()==0 ) { - ntasks = getPntrToArgument(0)->getNumberOfValues(); + if( !getPntrToArgument(0)->hasDerivatives() && getPntrToArgument(0)->getRank()==2 ) ntasks = getPntrToArgument(0)->getShape()[0]; + else ntasks = getPntrToArgument(0)->getNumberOfValues(); } else { plumed_assert( getNumberOfComponents()>0 && getPntrToComponent(0)->getRank()>0 ); if( getPntrToComponent(0)->hasDerivatives() ) ntasks = getPntrToComponent(0)->getNumberOfValues();