Skip to content

Commit

Permalink
Same trick on wholemolecules
Browse files Browse the repository at this point in the history
speedup 3%, but code is a mess
  • Loading branch information
GiovanniBussi committed Mar 13, 2024
1 parent b2a66a5 commit 0511455
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 24 deletions.
10 changes: 10 additions & 0 deletions src/core/ActionAtomistic.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class ActionAtomistic :
/// \warning Should be only used by actions that need to modify the shared position array.
/// This array is insensitive to local changes such as makeWhole(), numerical derivatives, etc.
void setGlobalPosition(const std::pair<std::size_t,std::size_t>&, const Vector& pos);
std::array<double*,3> getValueData(std::size_t nn);
/// Get total number of atoms, including virtual ones.
/// Can be used to make a loop on modifyGlobalPosition or getGlobalPosition.
unsigned getTotAtoms()const;
Expand Down Expand Up @@ -277,6 +278,15 @@ void ActionAtomistic::setGlobalPosition(const std::pair<std::size_t, std::size_t
zpos[a.first]->data[a.second]=pos[2];
}

inline
std::array<double*,3> ActionAtomistic::getValueData(std::size_t nn) {
std::array<double*,3> ptr;
ptr[0]=xpos[nn]->data.data();
ptr[1]=ypos[nn]->data.data();
ptr[2]=zpos[nn]->data.data();
return ptr;
}

inline
Vector ActionAtomistic::getForce( const std::pair<std::size_t, std::size_t>& a ) const {
Vector f;
Expand Down
79 changes: 55 additions & 24 deletions src/generic/WholeMolecules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ class WholeMolecules:
public ActionPilot,
public ActionAtomistic
{
std::vector<std::vector<std::pair<std::size_t,std::size_t> > > p_groups;
std::vector<std::vector<std::pair<std::size_t,std::size_t> > > p_roots;
std::vector<std::vector<std::pair<std::size_t,std::vector<std::size_t>>>> p_groups;
std::vector<std::vector<std::pair<std::size_t,std::vector<std::size_t>>>> p_roots;
std::vector<Vector> refs;
bool doemst, addref;
public:
Expand Down Expand Up @@ -221,17 +221,35 @@ WholeMolecules::WholeMolecules(const ActionOptions&ao):
// Convert groups to p_groups
p_groups.resize( groups.size() );
for(unsigned i=0; i<groups.size(); ++i) {
p_groups[i].resize( groups[i].size() );
for(unsigned j=0; j<groups[i].size(); ++j) p_groups[i][j] = getValueIndices( groups[i][j] );
std::size_t prev_nn=0;
bool first=true;
for(unsigned j=0; j<groups[i].size(); ++j) {
auto a = getValueIndices( groups[i][j] );
auto nn=a.first;
auto kk=a.second;
if(first || nn!=prev_nn) p_groups.back().push_back(std::pair<std::size_t,std::vector<std::size_t>>(nn,{}));
p_groups.back().back().second.push_back(kk);
first=false;
prev_nn=nn;
}
}

// Convert roots to p_roots
p_roots.resize( roots.size() );
for(unsigned i=0; i<roots.size(); ++i) {
p_roots[i].resize( roots[i].size() );
for(unsigned j=0; j<roots[i].size(); ++j) p_roots[i][j] = getValueIndices( roots[i][j] );
bool first=true;
std::size_t prev_nn=0;
for(unsigned j=0; j<roots[i].size(); ++j) {
auto a = getValueIndices( roots[i][j] );
auto nn=a.first;
auto kk=a.second;
if(first || nn!=prev_nn) p_roots.back().push_back(std::pair<std::size_t,std::vector<std::size_t>>(nn,{}));
p_roots.back().back().second.push_back(kk);
first=false;
prev_nn=nn;
}
}


checkRead();
Tools::removeDuplicates(merge);
requestAtoms(merge);
Expand All @@ -241,25 +259,38 @@ WholeMolecules::WholeMolecules(const ActionOptions&ao):

void WholeMolecules::calculate() {
for(unsigned i=0; i<p_groups.size(); ++i) {
Vector first = getGlobalPosition(p_groups[i][0]);
if(addref) {
first = refs[i]+pbcDistance(refs[i],first);
setGlobalPosition( p_groups[i][0], first );
}
if(!doemst) {
for(unsigned j=1; j<p_groups[i].size(); ++j) {
Vector second=getGlobalPosition(p_groups[i][j]);
first = first+pbcDistance(first,second);
setGlobalPosition(p_groups[i][j], first );
}
} else {
for(unsigned j=1; j<p_groups[i].size(); ++j) {
Vector first=getGlobalPosition(p_roots[i][j-1]);
Vector second=getGlobalPosition(p_groups[i][j]);
second=first+pbcDistance(first,second);
setGlobalPosition(p_groups[i][j], second );
bool start=true;
// if(addref) {
// first = refs[i]+pbcDistance(refs[i],first);
// setGlobalPosition( p_groups[i][0], first );
// }
// if(!doemst) {

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

Vector first;
for(unsigned j=0; j<p_groups[i].size(); ++j) {
auto data = getValueData(p_groups[i][j].first);
for(const auto kk : p_groups[i][j].second) {
Vector second{
data[0][kk],
data[1][kk],
data[2][kk],
};
if(start) first=second;
else first += pbcDistance(first,second);
start=false;
data[0][kk]=first[0];
data[1][kk]=first[1];
data[2][kk]=first[2];
}
}
// } else {
// for(unsigned j=1; j<p_groups[i].size(); ++j) {
// Vector first=getGlobalPosition(p_roots[i][j-1]);
// Vector second=getGlobalPosition(p_groups[i][j]);
// second=first+pbcDistance(first,second);
// setGlobalPosition(p_groups[i][j], second );
// }
// }

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
}
}

Expand Down

1 comment on commit 0511455

@PlumedBot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found broken examples in automatic/ADAPTIVE_PATH.tmp
Found broken examples in automatic/ANGLES.tmp
Found broken examples in automatic/ANN.tmp
Found broken examples in automatic/AROUND.tmp
Found broken examples in automatic/CAVITY.tmp
Found broken examples in automatic/CLUSTER_DIAMETER.tmp
Found broken examples in automatic/CLUSTER_DISTRIBUTION.tmp
Found broken examples in automatic/CLUSTER_PROPERTIES.tmp
Found broken examples in automatic/CONSTANT.tmp
Found broken examples in automatic/CONTACT_MATRIX.tmp
Found broken examples in automatic/CONVERT_TO_FES.tmp
Found broken examples in automatic/COORDINATIONNUMBER.tmp
Found broken examples in automatic/DFSCLUSTERING.tmp
Found broken examples in automatic/DISTANCE_FROM_CONTOUR.tmp
Found broken examples in automatic/DUMPCUBE.tmp
Found broken examples in automatic/DUMPGRID.tmp
Found broken examples in automatic/EDS.tmp
Found broken examples in automatic/EMMI.tmp
Found broken examples in automatic/ENVIRONMENTSIMILARITY.tmp
Found broken examples in automatic/FIND_CONTOUR.tmp
Found broken examples in automatic/FIND_CONTOUR_SURFACE.tmp
Found broken examples in automatic/FIND_SPHERICAL_CONTOUR.tmp
Found broken examples in automatic/FOURIER_TRANSFORM.tmp
Found broken examples in automatic/FUNCPATHGENERAL.tmp
Found broken examples in automatic/FUNCPATHMSD.tmp
Found broken examples in automatic/FUNNEL.tmp
Found broken examples in automatic/FUNNEL_PS.tmp
Found broken examples in automatic/GHBFIX.tmp
Found broken examples in automatic/GPROPERTYMAP.tmp
Found broken examples in automatic/HBOND_MATRIX.tmp
Found broken examples in automatic/HISTOGRAM.tmp
Found broken examples in automatic/INCLUDE.tmp
Found broken examples in automatic/INCYLINDER.tmp
Found broken examples in automatic/INENVELOPE.tmp
Found broken examples in automatic/INSPHERE.tmp
Found broken examples in automatic/INTERPOLATE_GRID.tmp
Found broken examples in automatic/LOCAL_AVERAGE.tmp
Found broken examples in automatic/LOCAL_Q3.tmp
Found broken examples in automatic/LOCAL_Q4.tmp
Found broken examples in automatic/LOCAL_Q6.tmp
Found broken examples in automatic/MAZE_OPTIMIZER_BIAS.tmp
Found broken examples in automatic/MAZE_RANDOM_ACCELERATION_MD.tmp
Found broken examples in automatic/MAZE_SIMULATED_ANNEALING.tmp
Found broken examples in automatic/MAZE_STEERED_MD.tmp
Found broken examples in automatic/MULTICOLVARDENS.tmp
Found broken examples in automatic/OUTPUT_CLUSTER.tmp
Found broken examples in automatic/PAMM.tmp
Found broken examples in automatic/PARABETARMSD.tmp
Found broken examples in automatic/PATH.tmp
Found broken examples in automatic/PCAVARS.tmp
Found broken examples in automatic/PIV.tmp
Found broken examples in automatic/PLUMED.tmp
Found broken examples in automatic/PYCVINTERFACE.tmp
Found broken examples in automatic/PYTHONFUNCTION.tmp
Found broken examples in automatic/QUATERNION.tmp
Found broken examples in automatic/REWEIGHT_BIAS.tmp
Found broken examples in automatic/REWEIGHT_METAD.tmp
Found broken examples in automatic/SPRINT.tmp
Found broken examples in automatic/TETRAHEDRALPORE.tmp
Found broken examples in automatic/TORSION.tmp
Found broken examples in automatic/TORSIONS.tmp
Found broken examples in automatic/WHAM_HISTOGRAM.tmp
Found broken examples in automatic/WHAM_WEIGHTS.tmp
Found broken examples in AnalysisPP.md
Found broken examples in CollectiveVariablesPP.md
Found broken examples in MiscelaneousPP.md

Please sign in to comment.