2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013 by the GROMACS development team.
5 * Copyright (c) 2014,2015,2016,2017,2018 by the GROMACS development team.
6 * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
39 * Implements gmx::PositionCalculationCollection and functions in poscalc.h.
42 * There is probably some room for optimization in the calculation of
43 * positions with bases.
44 * In particular, the current implementation may do a lot of unnecessary
46 * The interface would need to be changed to make it possible to use the
47 * same output positions for several calculations.
50 * The current algorithm for setting up base calculations could be improved
51 * in cases when there are calculations that cannot use a common base but
52 * still overlap partially (e.g., with three calculations A, B, and C
53 * such that A could use both B and C as a base, but B and C cannot use the
55 * Setting up the bases in an optimal manner in every possible situation can
56 * be quite difficult unless several bases are allowed for one calculation,
57 * but better heuristics could probably be implemented.
58 * For best results, the setup should probably be postponed (at least
59 * partially) to gmx_ana_poscalc_init_eval().
61 * \author Teemu Murtola <teemu.murtola@gmail.com>
62 * \ingroup module_selection
73 #include "gromacs/math/vec.h"
74 #include "gromacs/selection/indexutil.h"
75 #include "gromacs/trajectory/trajectoryframe.h"
76 #include "gromacs/utility/arrayref.h"
77 #include "gromacs/utility/exceptions.h"
78 #include "gromacs/utility/gmxassert.h"
79 #include "gromacs/utility/smalloc.h"
81 #include "centerofmass.h"
88 * Private implementation class for PositionCalculationCollection.
90 * \ingroup module_selection
92 class PositionCalculationCollection::Impl
99 * Inserts a position calculation structure into its collection.
101 * \param pc Data structure to insert.
102 * \param before Data structure before which to insert
103 * (NULL = insert at end).
105 * Inserts \p pc to its collection before \p before.
106 * If \p before is NULL, \p pc is appended to the list.
108 void insertCalculation(gmx_ana_poscalc_t* pc, gmx_ana_poscalc_t* before);
110 * Removes a position calculation structure from its collection.
112 * \param pc Data structure to remove.
114 * Removes \p pc from its collection.
116 void removeCalculation(gmx_ana_poscalc_t* pc);
118 /*! \copydoc PositionCalculationCollection::createCalculation()
120 * This method contains the actual implementation of the similarly
121 * named method in the parent class.
123 gmx_ana_poscalc_t* createCalculation(e_poscalc_t type, int flags);
126 * Maps given topology indices into frame indices.
128 * Only one position calculation at a time needs to access this (and
129 * there are also other thread-unsafe constructs here), so a temporary
130 * array is used to avoid repeated memory allocation.
132 ArrayRef<const int> getFrameIndices(int size, int index[])
134 if (mapToFrameAtoms_.empty())
136 return constArrayRefFromArray(index, size);
138 tmpFrameAtoms_.resize(size);
139 for (int i = 0; i < size; ++i)
141 const int ii = index[i];
142 GMX_ASSERT(ii >= 0 && ii <= ssize(mapToFrameAtoms_) && mapToFrameAtoms_[ii] != -1,
143 "Invalid input atom index");
144 tmpFrameAtoms_[i] = mapToFrameAtoms_[ii];
146 return tmpFrameAtoms_;
152 * Can be NULL if none of the calculations require topology data or if
153 * setTopology() has not been called.
155 const gmx_mtop_t* top_;
156 //! Pointer to the first data structure.
157 gmx_ana_poscalc_t* first_;
158 //! Pointer to the last data structure.
159 gmx_ana_poscalc_t* last_;
160 //! Whether the collection has been initialized for evaluation.
162 //! Mapping from topology atoms to frame atoms (one index for each topology atom).
163 std::vector<int> mapToFrameAtoms_;
164 //! Working array for updating positions.
165 std::vector<int> tmpFrameAtoms_;
171 * Data structure for position calculation.
173 struct gmx_ana_poscalc_t
176 * Type of calculation.
178 * This field may differ from the type requested by the user, because
179 * it is changed internally to the most effective calculation.
180 * For example, if the user requests a COM calculation for residues
181 * consisting of single atoms, it is simply set to POS_ATOM.
182 * To provide a consistent interface to the user, the field \p itype
183 * should be used when information should be given out.
187 * Flags for calculation options.
189 * See \ref poscalc_flags "documentation of the flags".
194 * Type for the created indices.
196 * This field always agrees with the type that the user requested, but
197 * may differ from \p type.
201 * Block data for the calculation.
205 * Mapping from the blocks to the blocks of \p sbase.
207 * If \p sbase is NULL, this field is also.
211 * Maximum evaluation group.
213 gmx_ana_index_t gmax;
215 /** Position storage for calculations that are used as a base. */
218 /** true if the positions have been evaluated for the current frame. */
221 * Base position data for this calculation.
223 * If not NULL, the centers required by this calculation have
224 * already been calculated in \p sbase.
225 * The structure pointed by \p sbase is always a static calculation.
227 gmx_ana_poscalc_t* sbase;
228 /** Next structure in the linked list of calculations. */
229 gmx_ana_poscalc_t* next;
230 /** Previous structure in the linked list of calculations. */
231 gmx_ana_poscalc_t* prev;
232 /** Number of references to this structure. */
234 /** Collection this calculation belongs to. */
235 gmx::PositionCalculationCollection::Impl* coll;
238 const char* const gmx::PositionCalculationCollection::typeEnumValues[] = {
239 "atom", "res_com", "res_cog", "mol_com", "mol_cog",
240 "whole_res_com", "whole_res_cog", "whole_mol_com", "whole_mol_cog", "part_res_com",
241 "part_res_cog", "part_mol_com", "part_mol_cog", "dyn_res_com", "dyn_res_cog",
242 "dyn_mol_com", "dyn_mol_cog", nullptr,
246 * Returns the partition type for a given position type.
248 * \param [in] type \c e_poscalc_t value to convert.
249 * \returns Corresponding \c e_indet_t.
251 static e_index_t index_type_for_poscalc(e_poscalc_t type)
255 case POS_ATOM: return INDEX_ATOM;
256 case POS_RES: return INDEX_RES;
257 case POS_MOL: return INDEX_MOL;
258 case POS_ALL: // Intended fall through
259 case POS_ALL_PBC: return INDEX_ALL;
261 return INDEX_UNKNOWN;
270 //! Helper function for determining required topology information.
271 PositionCalculationCollection::RequiredTopologyInfo requiredTopologyInfo(e_poscalc_t type, int flags)
273 if (type != POS_ATOM)
275 if ((flags & POS_MASS) || (flags & POS_FORCES))
277 return PositionCalculationCollection::RequiredTopologyInfo::TopologyAndMasses;
279 if (type == POS_RES || type == POS_MOL)
281 return PositionCalculationCollection::RequiredTopologyInfo::Topology;
284 return PositionCalculationCollection::RequiredTopologyInfo::None;
290 void PositionCalculationCollection::typeFromEnum(const char* post, e_poscalc_t* type, int* flags)
295 *flags &= ~(POS_MASS | POS_COMPLMAX | POS_COMPLWHOLE);
299 /* Process the prefix */
300 const char* ptr = post;
303 *flags &= ~POS_COMPLMAX;
304 *flags |= POS_COMPLWHOLE;
307 else if (post[0] == 'p')
309 *flags &= ~POS_COMPLWHOLE;
310 *flags |= POS_COMPLMAX;
313 else if (post[0] == 'd')
315 *flags &= ~(POS_COMPLMAX | POS_COMPLWHOLE);
323 else if (ptr[0] == 'm')
329 GMX_THROW(InternalError("Unknown position calculation type"));
333 GMX_THROW(InternalError("Unknown position calculation type"));
339 else if (ptr[6] == 'g')
345 GMX_THROW(InternalError("Unknown position calculation type"));
350 PositionCalculationCollection::RequiredTopologyInfo
351 PositionCalculationCollection::requiredTopologyInfoForType(const char* post, bool forces)
354 int flags = (forces ? POS_FORCES : 0);
355 PositionCalculationCollection::typeFromEnum(post, &type, &flags);
356 return requiredTopologyInfo(type, flags);
359 /********************************************************************
360 * PositionCalculationCollection::Impl
363 PositionCalculationCollection::Impl::Impl() :
364 top_(nullptr), first_(nullptr), last_(nullptr), bInit_(false)
368 PositionCalculationCollection::Impl::~Impl()
370 // Loop backwards, because there can be internal references in that are
371 // correctly handled by this direction.
372 while (last_ != nullptr)
374 GMX_ASSERT(last_->refcount == 1, "Dangling references to position calculations");
375 gmx_ana_poscalc_free(last_);
379 void PositionCalculationCollection::Impl::insertCalculation(gmx_ana_poscalc_t* pc, gmx_ana_poscalc_t* before)
381 GMX_RELEASE_ASSERT(pc->coll == this, "Inconsistent collections");
382 if (before == nullptr)
386 if (last_ != nullptr)
394 pc->prev = before->prev;
398 before->prev->next = pc;
402 if (pc->prev == nullptr)
408 void PositionCalculationCollection::Impl::removeCalculation(gmx_ana_poscalc_t* pc)
410 GMX_RELEASE_ASSERT(pc->coll == this, "Inconsistent collections");
411 if (pc->prev != nullptr)
413 pc->prev->next = pc->next;
415 else if (pc == first_)
419 if (pc->next != nullptr)
421 pc->next->prev = pc->prev;
423 else if (pc == last_)
427 pc->prev = pc->next = nullptr;
430 gmx_ana_poscalc_t* PositionCalculationCollection::Impl::createCalculation(e_poscalc_t type, int flags)
432 gmx_ana_poscalc_t* pc;
436 pc->itype = index_type_for_poscalc(type);
437 gmx_ana_poscalc_set_flags(pc, flags);
440 insertCalculation(pc, nullptr);
445 /********************************************************************
446 * PositionCalculationCollection
449 PositionCalculationCollection::PositionCalculationCollection() : impl_(new Impl) {}
451 PositionCalculationCollection::~PositionCalculationCollection() {}
453 void PositionCalculationCollection::setTopology(const gmx_mtop_t* top)
458 void PositionCalculationCollection::printTree(FILE* fp) const
460 gmx_ana_poscalc_t* pc;
463 fprintf(fp, "Position calculations:\n");
468 fprintf(fp, "%2d ", i);
471 case POS_ATOM: fprintf(fp, "ATOM"); break;
472 case POS_RES: fprintf(fp, "RES"); break;
473 case POS_MOL: fprintf(fp, "MOL"); break;
474 case POS_ALL: fprintf(fp, "ALL"); break;
475 case POS_ALL_PBC: fprintf(fp, "ALL_PBC"); break;
477 if (pc->itype != index_type_for_poscalc(pc->type))
482 case INDEX_UNKNOWN: fprintf(fp, "???"); break;
483 case INDEX_ATOM: fprintf(fp, "ATOM"); break;
484 case INDEX_RES: fprintf(fp, "RES"); break;
485 case INDEX_MOL: fprintf(fp, "MOL"); break;
486 case INDEX_ALL: fprintf(fp, "ALL"); break;
490 fprintf(fp, " flg=");
491 if (pc->flags & POS_MASS)
495 if (pc->flags & POS_DYNAMIC)
499 if (pc->flags & POS_MASKONLY)
503 if (pc->flags & POS_COMPLMAX)
507 if (pc->flags & POS_COMPLWHOLE)
515 fprintf(fp, " nr=%d nra=%d", pc->b.nr, pc->b.nra);
516 fprintf(fp, " refc=%d", pc->refcount);
518 if (pc->gmax.nalloc_index > 0)
520 fprintf(fp, " Group: ");
521 if (pc->gmax.isize > 20)
523 fprintf(fp, " %d atoms", pc->gmax.isize);
527 for (j = 0; j < pc->gmax.isize; ++j)
529 fprintf(fp, " %d", pc->gmax.index[j] + 1);
534 if (pc->b.nalloc_a > 0)
536 fprintf(fp, " Atoms: ");
539 fprintf(fp, " %d atoms", pc->b.nra);
543 for (j = 0; j < pc->b.nra; ++j)
545 fprintf(fp, " %d", pc->b.a[j] + 1);
550 if (pc->b.nalloc_index > 0)
552 fprintf(fp, " Blocks:");
555 fprintf(fp, " %d pcs", pc->b.nr);
559 for (j = 0; j <= pc->b.nr; ++j)
561 fprintf(fp, " %d", pc->b.index[j]);
568 gmx_ana_poscalc_t* base;
570 fprintf(fp, " Base: ");
572 base = impl_->first_;
573 while (base && base != pc->sbase)
578 fprintf(fp, "%d", j);
579 if (pc->baseid && pc->b.nr <= 20)
582 for (j = 0; j < pc->b.nr; ++j)
584 fprintf(fp, " %d", pc->baseid[j] + 1);
594 gmx_ana_poscalc_t* PositionCalculationCollection::createCalculation(e_poscalc_t type, int flags)
596 return impl_->createCalculation(type, flags);
599 gmx_ana_poscalc_t* PositionCalculationCollection::createCalculationFromEnum(const char* post, int flags)
603 typeFromEnum(post, &type, &cflags);
604 return impl_->createCalculation(type, cflags);
607 void PositionCalculationCollection::getRequiredAtoms(gmx_ana_index_t* out) const
609 gmx_ana_poscalc_t* pc = impl_->first_;
612 // Calculations with a base just copy positions from the base, so
613 // those do not need to be considered in the check.
617 gmx_ana_index_set(&g, pc->b.nra, pc->b.a, 0);
618 gmx_ana_index_union_unsorted(out, out, &g);
624 void PositionCalculationCollection::initEvaluation()
630 gmx_ana_poscalc_t* pc = impl_->first_;
633 /* Initialize position storage for base calculations */
636 gmx_ana_poscalc_init_pos(pc, pc->p);
638 /* Construct the mapping of the base positions */
643 snew(pc->baseid, pc->b.nr);
644 for (bi = bj = 0; bi < pc->b.nr; ++bi, ++bj)
646 while (pc->sbase->b.a[pc->sbase->b.index[bj]] != pc->b.a[pc->b.index[bi]])
653 /* Free the block data for dynamic calculations */
654 if (pc->flags & POS_DYNAMIC)
656 if (pc->b.nalloc_index > 0)
659 pc->b.nalloc_index = 0;
661 if (pc->b.nalloc_a > 0)
669 impl_->bInit_ = true;
672 void PositionCalculationCollection::initFrame(const t_trxframe* fr)
678 /* Clear the evaluation flags */
679 gmx_ana_poscalc_t* pc = impl_->first_;
685 if (fr->bIndex && fr->natoms > 0)
687 const int highestAtom = *std::max_element(fr->index, fr->index + fr->natoms);
688 impl_->mapToFrameAtoms_.resize(highestAtom + 1);
689 std::fill(impl_->mapToFrameAtoms_.begin(), impl_->mapToFrameAtoms_.end(), -1);
690 for (int i = 0; i < fr->natoms; ++i)
692 impl_->mapToFrameAtoms_[fr->index[i]] = i;
697 impl_->mapToFrameAtoms_.clear();
704 * Initializes position calculation using the maximum possible input index.
706 * \param[in,out] pc Position calculation data structure.
707 * \param[in] g Maximum index group for the calculation.
708 * \param[in] bBase Whether \p pc will be used as a base or not.
710 * \p bBase affects on how the \p pc->gmax field is initialized.
712 static void set_poscalc_maxindex(gmx_ana_poscalc_t* pc, gmx_ana_index_t* g, bool bBase)
714 const gmx_mtop_t* top = pc->coll->top_;
715 gmx_ana_index_make_block(&pc->b, top, g, pc->itype, (pc->flags & POS_COMPLWHOLE) != 0);
716 /* Set the type to POS_ATOM if the calculation in fact is such. */
717 if (pc->b.nr == pc->b.nra)
720 pc->flags &= ~(POS_MASS | POS_COMPLMAX | POS_COMPLWHOLE);
722 /* Set the POS_COMPLWHOLE flag if the calculation in fact always uses
723 * complete residues and molecules. */
724 if (!(pc->flags & POS_COMPLWHOLE) && (!(pc->flags & POS_DYNAMIC) || (pc->flags & POS_COMPLMAX))
725 && (pc->type == POS_RES || pc->type == POS_MOL)
726 && gmx_ana_index_has_complete_elems(g, pc->itype, top))
728 pc->flags &= ~POS_COMPLMAX;
729 pc->flags |= POS_COMPLWHOLE;
731 /* Setup the gmax field */
732 if ((pc->flags & POS_COMPLWHOLE) && !bBase && pc->b.nra > g->isize)
734 gmx_ana_index_copy(&pc->gmax, g, true);
738 gmx_ana_index_set(&pc->gmax, pc->b.nra, pc->b.a, 0);
743 * Checks whether a position calculation should use a base at all.
745 * \param[in] pc Position calculation data to check.
746 * \returns true if \p pc can use a base and gets some benefit out of it,
749 static bool can_use_base(gmx_ana_poscalc_t* pc)
751 /* For atoms, it should be faster to do a simple copy, so don't use a
753 if (pc->type == POS_ATOM)
757 /* For dynamic selections that do not use completion, it is not possible
759 if ((pc->type == POS_RES || pc->type == POS_MOL) && (pc->flags & POS_DYNAMIC)
760 && !(pc->flags & (POS_COMPLMAX | POS_COMPLWHOLE)))
764 /* Dynamic calculations for a single position cannot use a base. */
765 if ((pc->type == POS_ALL || pc->type == POS_ALL_PBC) && (pc->flags & POS_DYNAMIC))
773 * Checks whether two position calculations should use a common base.
775 * \param[in] pc1 Calculation 1 to check for.
776 * \param[in] pc2 Calculation 2 to check for.
777 * \param[in] g1 Index group structure that contains the atoms from
779 * \param[in,out] g Working space, should have enough allocated memory to
780 * contain the intersection of the atoms in \p pc1 and \p pc2.
781 * \returns true if the two calculations should be merged to use a common
782 * base, false otherwise.
784 static bool should_merge(gmx_ana_poscalc_t* pc1, gmx_ana_poscalc_t* pc2, gmx_ana_index_t* g1, gmx_ana_index_t* g)
788 /* Do not merge calculations with different mass weighting. */
789 if ((pc1->flags & POS_MASS) != (pc2->flags & POS_MASS))
793 /* Avoid messing up complete calculations. */
794 if ((pc1->flags & POS_COMPLWHOLE) != (pc2->flags & POS_COMPLWHOLE))
798 /* Find the overlap between the calculations. */
799 gmx_ana_index_set(&g2, pc2->b.nra, pc2->b.a, 0);
800 gmx_ana_index_intersection(g, g1, &g2);
801 /* Do not merge if there is no overlap. */
806 /* Full completion calculations always match if the type is correct. */
807 if ((pc1->flags & POS_COMPLWHOLE) && (pc2->flags & POS_COMPLWHOLE) && pc1->type == pc2->type)
811 /* The calculations also match if the intersection consists of full
813 if (gmx_ana_index_has_full_ablocks(g, &pc1->b) && gmx_ana_index_has_full_ablocks(g, &pc2->b))
821 * Creates a static base for position calculation.
823 * \param pc Data structure to copy.
824 * \returns Pointer to a newly allocated base for \p pc.
826 * Creates and returns a deep copy of \p pc, but clears the
827 * \ref POS_DYNAMIC and \ref POS_MASKONLY flags.
828 * The newly created structure is set as the base (\c gmx_ana_poscalc_t::sbase)
829 * of \p pc and inserted in the collection before \p pc.
831 static gmx_ana_poscalc_t* create_simple_base(gmx_ana_poscalc_t* pc)
833 gmx_ana_poscalc_t* base;
836 flags = pc->flags & ~(POS_DYNAMIC | POS_MASKONLY);
837 base = pc->coll->createCalculation(pc->type, flags);
838 set_poscalc_maxindex(base, &pc->gmax, true);
840 base->p = new gmx_ana_pos_t();
843 pc->coll->removeCalculation(base);
844 pc->coll->insertCalculation(base, pc);
850 * Merges a calculation into another calculation such that the new calculation
851 * can be used as a base.
853 * \param[in,out] base Base calculation to merge to.
854 * \param[in,out] pc Position calculation to merge to \p base.
856 * After the call, \p base can be used as a base for \p pc (or any calculation
857 * that used it as a base).
858 * It is assumed that any overlap between \p base and \p pc is in complete
859 * blocks, i.e., that the merge is possible.
861 static void merge_to_base(gmx_ana_poscalc_t* base, gmx_ana_poscalc_t* pc)
863 gmx_ana_index_t gp, gb, g;
867 base->flags |= pc->flags & (POS_VELOCITIES | POS_FORCES);
868 gmx_ana_index_set(&gp, pc->b.nra, pc->b.a, 0);
869 gmx_ana_index_set(&gb, base->b.nra, base->b.a, 0);
870 isize = gmx_ana_index_difference_size(&gp, &gb);
873 gmx_ana_index_clear(&g);
874 gmx_ana_index_reserve(&g, base->b.nra + isize);
875 /* Find the new blocks */
876 gmx_ana_index_difference(&g, &gp, &gb);
877 /* Count the blocks in g */
881 while (pc->b.a[pc->b.index[bi]] != g.index[i])
885 i += pc->b.index[bi + 1] - pc->b.index[bi];
889 /* Merge the atoms into a temporary structure */
890 gmx_ana_index_merge(&g, &gb, &g);
891 /* Merge the blocks */
892 srenew(base->b.index, base->b.nr + bnr + 1);
896 bo = base->b.nr + bnr - 1;
897 base->b.index[bo + 1] = i + 1;
900 if (bi < 0 || base->b.a[base->b.index[bi + 1] - 1] != g.index[i])
902 i -= pc->b.index[bj + 1] - pc->b.index[bj];
907 if (bj >= 0 && pc->b.a[pc->b.index[bj + 1] - 1] == g.index[i])
911 i -= base->b.index[bi + 1] - base->b.index[bi];
914 base->b.index[bo] = i + 1;
918 base->b.nalloc_index += bnr;
920 base->b.nra = g.isize;
922 base->b.nalloc_a = g.isize;
923 /* Refresh the gmax field */
924 gmx_ana_index_set(&base->gmax, base->b.nra, base->b.a, 0);
929 * Merges two bases into one.
931 * \param[in,out] tbase Base calculation to merge to.
932 * \param[in] mbase Base calculation to merge to \p tbase.
934 * After the call, \p mbase has been freed and \p tbase is used as the base
935 * for all calculations that previously had \p mbase as their base.
936 * It is assumed that any overlap between \p tbase and \p mbase is in complete
937 * blocks, i.e., that the merge is possible.
939 static void merge_bases(gmx_ana_poscalc_t* tbase, gmx_ana_poscalc_t* mbase)
941 gmx_ana_poscalc_t* pc;
943 merge_to_base(tbase, mbase);
944 mbase->coll->removeCalculation(mbase);
945 /* Set tbase as the base for all calculations that had mbase */
946 pc = tbase->coll->first_;
949 if (pc->sbase == mbase)
958 gmx_ana_poscalc_free(mbase);
962 * Setups the static base calculation for a position calculation.
964 * \param[in,out] pc Position calculation to setup the base for.
966 static void setup_base(gmx_ana_poscalc_t* pc)
968 gmx_ana_poscalc_t *base, *pbase, *next;
969 gmx_ana_index_t gp, g;
971 /* Exit immediately if pc should not have a base. */
972 if (!can_use_base(pc))
977 gmx_ana_index_set(&gp, pc->b.nra, pc->b.a, 0);
978 gmx_ana_index_clear(&g);
979 gmx_ana_index_reserve(&g, pc->b.nra);
981 base = pc->coll->first_;
984 /* Save the next calculation so that we can safely delete base */
986 /* Skip pc, calculations that already have a base (we should match the
987 * base instead), as well as calculations that should not have a base.
988 * If the above conditions are met, check whether we should do a
991 if (base != pc && !base->sbase && can_use_base(base) && should_merge(pbase, base, &gp, &g))
993 /* Check whether this is the first base found */
996 /* Create a real base if one is not present */
999 pbase = create_simple_base(base);
1005 /* Make it a base for pc as well */
1006 merge_to_base(pbase, pc);
1010 else /* This was not the first base */
1014 /* If it is not a real base, just make the new base as
1015 * the base for it as well. */
1016 merge_to_base(pbase, base);
1017 base->sbase = pbase;
1022 /* If base is a real base, merge it with the new base
1024 merge_bases(pbase, base);
1027 gmx_ana_index_set(&gp, pbase->b.nra, pbase->b.a, 0);
1028 gmx_ana_index_reserve(&g, pc->b.nra);
1030 /* Proceed to the next unchecked calculation */
1034 gmx_ana_index_deinit(&g);
1036 /* If no base was found, create one if one is required */
1037 if (!pc->sbase && (pc->flags & POS_DYNAMIC) && (pc->flags & (POS_COMPLMAX | POS_COMPLWHOLE)))
1039 create_simple_base(pc);
1044 * \param[in,out] pc Position calculation data structure.
1045 * \param[in] flags New flags.
1047 * \p flags are added to the old flags.
1048 * If calculation type is \ref POS_ATOM, \ref POS_MASS is automatically
1050 * If both \ref POS_DYNAMIC and \ref POS_MASKONLY are provided,
1051 * \ref POS_DYNAMIC is cleared.
1052 * If calculation type is not \ref POS_RES or \ref POS_MOL,
1053 * \ref POS_COMPLMAX and \ref POS_COMPLWHOLE are automatically cleared.
1055 void gmx_ana_poscalc_set_flags(gmx_ana_poscalc_t* pc, int flags)
1057 if (pc->type == POS_ATOM)
1061 if (flags & POS_MASKONLY)
1063 flags &= ~POS_DYNAMIC;
1065 if (pc->type != POS_RES && pc->type != POS_MOL)
1067 flags &= ~(POS_COMPLMAX | POS_COMPLWHOLE);
1073 * \param[in,out] pc Position calculation data structure.
1074 * \param[in] g Maximum index group for the calculation.
1076 * Subsequent calls to gmx_ana_poscalc_update() should use only subsets of
1077 * \p g for evaluation.
1079 * The topology should have been set for the collection of which \p pc is
1082 void gmx_ana_poscalc_set_maxindex(gmx_ana_poscalc_t* pc, gmx_ana_index_t* g)
1084 set_poscalc_maxindex(pc, g, false);
1089 * \param[in] pc Position calculation data structure.
1090 * \param[out] p Output positions.
1092 * Calls to gmx_ana_poscalc_update() using \p pc should use only positions
1093 * initialized with this function.
1094 * The \c p->g pointer is initialized to point to an internal group that
1095 * contains the maximum index group set with gmx_ana_poscalc_set_maxindex().
1097 void gmx_ana_poscalc_init_pos(gmx_ana_poscalc_t* pc, gmx_ana_pos_t* p)
1099 gmx_ana_indexmap_init(&p->m, &pc->gmax, pc->coll->top_, pc->itype);
1100 /* Only do the static optimization when there is no completion */
1101 if (!(pc->flags & POS_DYNAMIC) && pc->b.nra == pc->gmax.isize)
1103 gmx_ana_indexmap_set_static(&p->m, &pc->b);
1105 gmx_ana_pos_reserve(p, p->m.mapb.nr, -1);
1106 if (pc->flags & POS_VELOCITIES)
1108 gmx_ana_pos_reserve_velocities(p);
1110 if (pc->flags & POS_FORCES)
1112 gmx_ana_pos_reserve_forces(p);
1117 * \param pc Position calculation data to be freed.
1119 * The \p pc pointer is invalid after the call.
1121 void gmx_ana_poscalc_free(gmx_ana_poscalc_t* pc)
1129 if (pc->refcount > 0)
1134 pc->coll->removeCalculation(pc);
1135 if (pc->b.nalloc_index > 0)
1139 if (pc->b.nalloc_a > 0)
1143 if (pc->flags & POS_COMPLWHOLE)
1145 gmx_ana_index_deinit(&pc->gmax);
1150 gmx_ana_poscalc_free(pc->sbase);
1156 gmx::PositionCalculationCollection::RequiredTopologyInfo gmx_ana_poscalc_required_topology_info(gmx_ana_poscalc_t* pc)
1158 return gmx::requiredTopologyInfo(pc->type, pc->flags);
1162 * \param[in] pc Position calculation data.
1163 * \param[in,out] p Output positions, initialized previously with
1164 * gmx_ana_poscalc_init_pos() using \p pc.
1165 * \param[in] g Index group to use for the update.
1166 * \param[in] fr Current frame.
1167 * \param[in] pbc PBC data, or NULL if no PBC should be used.
1169 * gmx_ana_poscalc_init_frame() should be called for each frame before calling
1172 void gmx_ana_poscalc_update(gmx_ana_poscalc_t* pc,
1180 if (pc->bEval && !(pc->flags & POS_MASKONLY))
1186 gmx_ana_poscalc_update(pc->sbase, nullptr, nullptr, fr, pbc);
1197 /* Update the index map */
1198 if (pc->flags & POS_DYNAMIC)
1200 gmx_ana_indexmap_update(&p->m, g, false);
1202 else if (pc->flags & POS_MASKONLY)
1204 gmx_ana_indexmap_update(&p->m, g, true);
1210 if (!(pc->flags & POS_DYNAMIC))
1215 /* Evaluate the positions */
1218 /* TODO: It might be faster to evaluate the positions within this
1219 * loop instead of in the beginning. */
1220 if (pc->flags & POS_DYNAMIC)
1222 for (bi = 0; bi < p->count(); ++bi)
1224 bj = pc->baseid[p->m.refid[bi]];
1225 copy_rvec(pc->sbase->p->x[bj], p->x[bi]);
1229 for (bi = 0; bi < p->count(); ++bi)
1231 bj = pc->baseid[p->m.refid[bi]];
1232 copy_rvec(pc->sbase->p->v[bj], p->v[bi]);
1237 for (bi = 0; bi < p->count(); ++bi)
1239 bj = pc->baseid[p->m.refid[bi]];
1240 copy_rvec(pc->sbase->p->f[bj], p->f[bi]);
1246 for (bi = 0; bi < p->count(); ++bi)
1248 bj = pc->baseid[bi];
1249 copy_rvec(pc->sbase->p->x[bj], p->x[bi]);
1253 for (bi = 0; bi < p->count(); ++bi)
1255 bj = pc->baseid[bi];
1256 copy_rvec(pc->sbase->p->v[bj], p->v[bi]);
1261 for (bi = 0; bi < p->count(); ++bi)
1263 bj = pc->baseid[bi];
1264 copy_rvec(pc->sbase->p->f[bj], p->f[bi]);
1269 else /* pc->sbase is NULL */
1271 if (pc->flags & POS_DYNAMIC)
1273 pc->b.nr = p->m.mapb.nr;
1274 pc->b.index = p->m.mapb.index;
1275 pc->b.nra = g->isize;
1278 if (p->v && !fr->bV)
1280 for (i = 0; i < pc->b.nra; ++i)
1282 clear_rvec(p->v[i]);
1285 if (p->f && !fr->bF)
1287 for (i = 0; i < pc->b.nra; ++i)
1289 clear_rvec(p->f[i]);
1292 gmx::ArrayRef<const int> index = pc->coll->getFrameIndices(pc->b.nra, pc->b.a);
1293 const gmx_mtop_t* top = pc->coll->top_;
1294 const bool bMass = (pc->flags & POS_MASS) != 0;
1298 for (i = 0; i < pc->b.nra; ++i)
1300 copy_rvec(fr->x[index[i]], p->x[i]);
1304 for (i = 0; i < pc->b.nra; ++i)
1306 copy_rvec(fr->v[index[i]], p->v[i]);
1311 for (i = 0; i < pc->b.nra; ++i)
1313 copy_rvec(fr->f[index[i]], p->f[i]);
1318 gmx_calc_comg(top, fr->x, index.size(), index.data(), bMass, p->x[0]);
1321 gmx_calc_comg(top, fr->v, index.size(), index.data(), bMass, p->v[0]);
1325 gmx_calc_comg_f(top, fr->f, index.size(), index.data(), bMass, p->f[0]);
1329 gmx_calc_comg_pbc(top, fr->x, pbc, index.size(), index.data(), bMass, p->x[0]);
1332 gmx_calc_comg(top, fr->v, index.size(), index.data(), bMass, p->v[0]);
1336 gmx_calc_comg_f(top, fr->f, index.size(), index.data(), bMass, p->f[0]);
1340 // TODO: It would probably be better to do this without the type casts.
1341 gmx_calc_comg_block(
1342 top, fr->x, reinterpret_cast<t_block*>(&pc->b), index.data(), bMass, p->x);
1345 gmx_calc_comg_block(
1346 top, fr->v, reinterpret_cast<t_block*>(&pc->b), index.data(), bMass, p->v);
1350 gmx_calc_comg_f_block(
1351 top, fr->f, reinterpret_cast<t_block*>(&pc->b), index.data(), bMass, p->f);