2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements gmx::PositionCalculationCollection and functions in poscalc.h.
40 * There is probably some room for optimization in the calculation of
41 * positions with bases.
42 * In particular, the current implementation may do a lot of unnecessary
44 * The interface would need to be changed to make it possible to use the
45 * same output positions for several calculations.
48 * The current algorithm for setting up base calculations could be improved
49 * in cases when there are calculations that cannot use a common base but
50 * still overlap partially (e.g., with three calculations A, B, and C
51 * such that A could use both B and C as a base, but B and C cannot use the
53 * Setting up the bases in an optimal manner in every possible situation can
54 * be quite difficult unless several bases are allowed for one calculation,
55 * but better heuristics could probably be implemented.
56 * For best results, the setup should probably be postponed (at least
57 * partially) to gmx_ana_poscalc_init_eval().
59 * \author Teemu Murtola <teemu.murtola@gmail.com>
60 * \ingroup module_selection
64 #include "gromacs/legacyheaders/typedefs.h"
66 #include "gromacs/math/vec.h"
67 #include "gromacs/selection/centerofmass.h"
68 #include "gromacs/selection/indexutil.h"
69 #include "gromacs/selection/poscalc.h"
70 #include "gromacs/selection/position.h"
71 #include "gromacs/utility/exceptions.h"
72 #include "gromacs/utility/gmxassert.h"
73 #include "gromacs/utility/smalloc.h"
79 * Private implementation class for PositionCalculationCollection.
81 * \ingroup module_selection
83 class PositionCalculationCollection::Impl
90 * Inserts a position calculation structure into its collection.
92 * \param pc Data structure to insert.
93 * \param before Data structure before which to insert
94 * (NULL = insert at end).
96 * Inserts \p pc to its collection before \p before.
97 * If \p before is NULL, \p pc is appended to the list.
99 void insertCalculation(gmx_ana_poscalc_t *pc, gmx_ana_poscalc_t *before);
101 * Removes a position calculation structure from its collection.
103 * \param pc Data structure to remove.
105 * Removes \p pc from its collection.
107 void removeCalculation(gmx_ana_poscalc_t *pc);
109 /*! \copydoc PositionCalculationCollection::createCalculation()
111 * This method contains the actual implementation of the similarly
112 * named method in the parent class.
114 gmx_ana_poscalc_t *createCalculation(e_poscalc_t type, int flags);
119 * Can be NULL if none of the calculations require topology data or if
120 * setTopology() has not been called.
123 //! Pointer to the first data structure.
124 gmx_ana_poscalc_t *first_;
125 //! Pointer to the last data structure.
126 gmx_ana_poscalc_t *last_;
127 //! Whether the collection has been initialized for evaluation.
134 * Data structure for position calculation.
136 struct gmx_ana_poscalc_t
139 * Type of calculation.
141 * This field may differ from the type requested by the user, because
142 * it is changed internally to the most effective calculation.
143 * For example, if the user requests a COM calculation for residues
144 * consisting of single atoms, it is simply set to POS_ATOM.
145 * To provide a consistent interface to the user, the field \p itype
146 * should be used when information should be given out.
150 * Flags for calculation options.
152 * See \ref poscalc_flags "documentation of the flags".
157 * Type for the created indices.
159 * This field always agrees with the type that the user requested, but
160 * may differ from \p type.
164 * Block data for the calculation.
168 * Mapping from the blocks to the blocks of \p sbase.
170 * If \p sbase is NULL, this field is also.
174 * Maximum evaluation group.
176 gmx_ana_index_t gmax;
178 /** Position storage for calculations that are used as a base. */
181 /** true if the positions have been evaluated for the current frame. */
184 * Base position data for this calculation.
186 * If not NULL, the centers required by this calculation have
187 * already been calculated in \p sbase.
188 * The structure pointed by \p sbase is always a static calculation.
190 struct gmx_ana_poscalc_t *sbase;
191 /** Next structure in the linked list of calculations. */
192 struct gmx_ana_poscalc_t *next;
193 /** Previous structure in the linked list of calculations. */
194 struct gmx_ana_poscalc_t *prev;
195 /** Number of references to this structure. */
197 /** Collection this calculation belongs to. */
198 gmx::PositionCalculationCollection::Impl *coll;
201 const char * const gmx::PositionCalculationCollection::typeEnumValues[] = {
203 "res_com", "res_cog",
204 "mol_com", "mol_cog",
205 "whole_res_com", "whole_res_cog",
206 "whole_mol_com", "whole_mol_cog",
207 "part_res_com", "part_res_cog",
208 "part_mol_com", "part_mol_cog",
209 "dyn_res_com", "dyn_res_cog",
210 "dyn_mol_com", "dyn_mol_cog",
215 * Returns the partition type for a given position type.
217 * \param [in] type \c e_poscalc_t value to convert.
218 * \returns Corresponding \c e_indet_t.
221 index_type_for_poscalc(e_poscalc_t type)
225 case POS_ATOM: return INDEX_ATOM;
226 case POS_RES: return INDEX_RES;
227 case POS_MOL: return INDEX_MOL;
228 case POS_ALL: return INDEX_ALL;
229 case POS_ALL_PBC: return INDEX_ALL;
231 return INDEX_UNKNOWN;
239 PositionCalculationCollection::typeFromEnum(const char *post,
240 e_poscalc_t *type, int *flags)
245 *flags &= ~(POS_MASS | POS_COMPLMAX | POS_COMPLWHOLE);
249 /* Process the prefix */
250 const char *ptr = post;
253 *flags &= ~POS_COMPLMAX;
254 *flags |= POS_COMPLWHOLE;
257 else if (post[0] == 'p')
259 *flags &= ~POS_COMPLWHOLE;
260 *flags |= POS_COMPLMAX;
263 else if (post[0] == 'd')
265 *flags &= ~(POS_COMPLMAX | POS_COMPLWHOLE);
273 else if (ptr[0] == 'm')
279 GMX_THROW(InternalError("Unknown position calculation type"));
283 GMX_THROW(InternalError("Unknown position calculation type"));
289 else if (ptr[6] == 'g')
295 GMX_THROW(InternalError("Unknown position calculation type"));
299 /********************************************************************
300 * PositionCalculationCollection::Impl
303 PositionCalculationCollection::Impl::Impl()
304 : top_(NULL), first_(NULL), last_(NULL), bInit_(false)
308 PositionCalculationCollection::Impl::~Impl()
310 // Loop backwards, because there can be internal references in that are
311 // correctly handled by this direction.
312 while (last_ != NULL)
314 GMX_ASSERT(last_->refcount == 1,
315 "Dangling references to position calculations");
316 gmx_ana_poscalc_free(last_);
321 PositionCalculationCollection::Impl::insertCalculation(gmx_ana_poscalc_t *pc,
322 gmx_ana_poscalc_t *before)
324 GMX_RELEASE_ASSERT(pc->coll == this, "Inconsistent collections");
337 pc->prev = before->prev;
341 before->prev->next = pc;
345 if (pc->prev == NULL)
352 PositionCalculationCollection::Impl::removeCalculation(gmx_ana_poscalc_t *pc)
354 GMX_RELEASE_ASSERT(pc->coll == this, "Inconsistent collections");
355 if (pc->prev != NULL)
357 pc->prev->next = pc->next;
359 else if (pc == first_)
363 if (pc->next != NULL)
365 pc->next->prev = pc->prev;
367 else if (pc == last_)
371 pc->prev = pc->next = NULL;
375 PositionCalculationCollection::Impl::createCalculation(e_poscalc_t type, int flags)
377 gmx_ana_poscalc_t *pc;
381 pc->itype = index_type_for_poscalc(type);
382 gmx_ana_poscalc_set_flags(pc, flags);
385 insertCalculation(pc, NULL);
390 /********************************************************************
391 * PositionCalculationCollection
394 PositionCalculationCollection::PositionCalculationCollection()
399 PositionCalculationCollection::~PositionCalculationCollection()
404 PositionCalculationCollection::setTopology(t_topology *top)
410 PositionCalculationCollection::printTree(FILE *fp) const
412 gmx_ana_poscalc_t *pc;
415 fprintf(fp, "Position calculations:\n");
420 fprintf(fp, "%2d ", i);
423 case POS_ATOM: fprintf(fp, "ATOM"); break;
424 case POS_RES: fprintf(fp, "RES"); break;
425 case POS_MOL: fprintf(fp, "MOL"); break;
426 case POS_ALL: fprintf(fp, "ALL"); break;
427 case POS_ALL_PBC: fprintf(fp, "ALL_PBC"); break;
429 if (pc->itype != index_type_for_poscalc(pc->type))
434 case INDEX_UNKNOWN: fprintf(fp, "???"); break;
435 case INDEX_ATOM: fprintf(fp, "ATOM"); break;
436 case INDEX_RES: fprintf(fp, "RES"); break;
437 case INDEX_MOL: fprintf(fp, "MOL"); break;
438 case INDEX_ALL: fprintf(fp, "ALL"); break;
442 fprintf(fp, " flg=");
443 if (pc->flags & POS_MASS)
447 if (pc->flags & POS_DYNAMIC)
451 if (pc->flags & POS_MASKONLY)
455 if (pc->flags & POS_COMPLMAX)
459 if (pc->flags & POS_COMPLWHOLE)
467 fprintf(fp, " nr=%d nra=%d", pc->b.nr, pc->b.nra);
468 fprintf(fp, " refc=%d", pc->refcount);
470 if (pc->gmax.nalloc_index > 0)
472 fprintf(fp, " Group: ");
473 if (pc->gmax.isize > 20)
475 fprintf(fp, " %d atoms", pc->gmax.isize);
479 for (j = 0; j < pc->gmax.isize; ++j)
481 fprintf(fp, " %d", pc->gmax.index[j] + 1);
486 if (pc->b.nalloc_a > 0)
488 fprintf(fp, " Atoms: ");
491 fprintf(fp, " %d atoms", pc->b.nra);
495 for (j = 0; j < pc->b.nra; ++j)
497 fprintf(fp, " %d", pc->b.a[j] + 1);
502 if (pc->b.nalloc_index > 0)
504 fprintf(fp, " Blocks:");
507 fprintf(fp, " %d pcs", pc->b.nr);
511 for (j = 0; j <= pc->b.nr; ++j)
513 fprintf(fp, " %d", pc->b.index[j]);
520 gmx_ana_poscalc_t *base;
522 fprintf(fp, " Base: ");
524 base = impl_->first_;
525 while (base && base != pc->sbase)
530 fprintf(fp, "%d", j);
531 if (pc->baseid && pc->b.nr <= 20)
534 for (j = 0; j < pc->b.nr; ++j)
536 fprintf(fp, " %d", pc->baseid[j]+1);
547 PositionCalculationCollection::createCalculation(e_poscalc_t type, int flags)
549 return impl_->createCalculation(type, flags);
553 PositionCalculationCollection::createCalculationFromEnum(const char *post, int flags)
557 typeFromEnum(post, &type, &cflags);
558 return impl_->createCalculation(type, cflags);
561 void PositionCalculationCollection::initEvaluation()
567 gmx_ana_poscalc_t *pc = impl_->first_;
570 /* Initialize position storage for base calculations */
573 gmx_ana_poscalc_init_pos(pc, pc->p);
575 /* Construct the mapping of the base positions */
580 snew(pc->baseid, pc->b.nr);
581 for (bi = bj = 0; bi < pc->b.nr; ++bi, ++bj)
583 while (pc->sbase->b.a[pc->sbase->b.index[bj]] != pc->b.a[pc->b.index[bi]])
590 /* Free the block data for dynamic calculations */
591 if (pc->flags & POS_DYNAMIC)
593 if (pc->b.nalloc_index > 0)
596 pc->b.nalloc_index = 0;
598 if (pc->b.nalloc_a > 0)
606 impl_->bInit_ = true;
609 void PositionCalculationCollection::initFrame()
615 /* Clear the evaluation flags */
616 gmx_ana_poscalc_t *pc = impl_->first_;
627 * Initializes position calculation using the maximum possible input index.
629 * \param[in,out] pc Position calculation data structure.
630 * \param[in] g Maximum index group for the calculation.
631 * \param[in] bBase Whether \p pc will be used as a base or not.
633 * \p bBase affects on how the \p pc->gmax field is initialized.
636 set_poscalc_maxindex(gmx_ana_poscalc_t *pc, gmx_ana_index_t *g, bool bBase)
638 t_topology *top = pc->coll->top_;
639 gmx_ana_index_make_block(&pc->b, top, g, pc->itype, pc->flags & POS_COMPLWHOLE);
640 /* Set the type to POS_ATOM if the calculation in fact is such. */
641 if (pc->b.nr == pc->b.nra)
644 pc->flags &= ~(POS_MASS | POS_COMPLMAX | POS_COMPLWHOLE);
646 /* Set the POS_COMPLWHOLE flag if the calculation in fact always uses
647 * complete residues and molecules. */
648 if (!(pc->flags & POS_COMPLWHOLE)
649 && (!(pc->flags & POS_DYNAMIC) || (pc->flags & POS_COMPLMAX))
650 && (pc->type == POS_RES || pc->type == POS_MOL)
651 && gmx_ana_index_has_complete_elems(g, pc->itype, top))
653 pc->flags &= ~POS_COMPLMAX;
654 pc->flags |= POS_COMPLWHOLE;
656 /* Setup the gmax field */
657 if ((pc->flags & POS_COMPLWHOLE) && !bBase && pc->b.nra > g->isize)
659 gmx_ana_index_copy(&pc->gmax, g, true);
663 gmx_ana_index_set(&pc->gmax, pc->b.nra, pc->b.a, 0);
668 * Checks whether a position calculation should use a base at all.
670 * \param[in] pc Position calculation data to check.
671 * \returns true if \p pc can use a base and gets some benefit out of it,
675 can_use_base(gmx_ana_poscalc_t *pc)
677 /* For atoms, it should be faster to do a simple copy, so don't use a
679 if (pc->type == POS_ATOM)
683 /* For dynamic selections that do not use completion, it is not possible
685 if ((pc->type == POS_RES || pc->type == POS_MOL)
686 && (pc->flags & POS_DYNAMIC) && !(pc->flags & (POS_COMPLMAX | POS_COMPLWHOLE)))
690 /* Dynamic calculations for a single position cannot use a base. */
691 if ((pc->type == POS_ALL || pc->type == POS_ALL_PBC)
692 && (pc->flags & POS_DYNAMIC))
700 * Checks whether two position calculations should use a common base.
702 * \param[in] pc1 Calculation 1 to check for.
703 * \param[in] pc2 Calculation 2 to check for.
704 * \param[in] g1 Index group structure that contains the atoms from
706 * \param[in,out] g Working space, should have enough allocated memory to
707 * contain the intersection of the atoms in \p pc1 and \p pc2.
708 * \returns true if the two calculations should be merged to use a common
709 * base, false otherwise.
712 should_merge(gmx_ana_poscalc_t *pc1, gmx_ana_poscalc_t *pc2,
713 gmx_ana_index_t *g1, gmx_ana_index_t *g)
717 /* Do not merge calculations with different mass weighting. */
718 if ((pc1->flags & POS_MASS) != (pc2->flags & POS_MASS))
722 /* Avoid messing up complete calculations. */
723 if ((pc1->flags & POS_COMPLWHOLE) != (pc2->flags & POS_COMPLWHOLE))
727 /* Find the overlap between the calculations. */
728 gmx_ana_index_set(&g2, pc2->b.nra, pc2->b.a, 0);
729 gmx_ana_index_intersection(g, g1, &g2);
730 /* Do not merge if there is no overlap. */
735 /* Full completion calculations always match if the type is correct. */
736 if ((pc1->flags & POS_COMPLWHOLE) && (pc2->flags & POS_COMPLWHOLE)
737 && pc1->type == pc2->type)
741 /* The calculations also match if the intersection consists of full
743 if (gmx_ana_index_has_full_ablocks(g, &pc1->b)
744 && gmx_ana_index_has_full_ablocks(g, &pc2->b))
752 * Creates a static base for position calculation.
754 * \param pc Data structure to copy.
755 * \returns Pointer to a newly allocated base for \p pc.
757 * Creates and returns a deep copy of \p pc, but clears the
758 * \ref POS_DYNAMIC and \ref POS_MASKONLY flags.
759 * The newly created structure is set as the base (\c gmx_ana_poscalc_t::sbase)
760 * of \p pc and inserted in the collection before \p pc.
762 static gmx_ana_poscalc_t *
763 create_simple_base(gmx_ana_poscalc_t *pc)
765 gmx_ana_poscalc_t *base;
768 flags = pc->flags & ~(POS_DYNAMIC | POS_MASKONLY);
769 base = pc->coll->createCalculation(pc->type, flags);
770 set_poscalc_maxindex(base, &pc->gmax, true);
772 base->p = new gmx_ana_pos_t();
775 pc->coll->removeCalculation(base);
776 pc->coll->insertCalculation(base, pc);
782 * Merges a calculation into another calculation such that the new calculation
783 * can be used as a base.
785 * \param[in,out] base Base calculation to merge to.
786 * \param[in,out] pc Position calculation to merge to \p base.
788 * After the call, \p base can be used as a base for \p pc (or any calculation
789 * that used it as a base).
790 * It is assumed that any overlap between \p base and \p pc is in complete
791 * blocks, i.e., that the merge is possible.
794 merge_to_base(gmx_ana_poscalc_t *base, gmx_ana_poscalc_t *pc)
796 gmx_ana_index_t gp, gb, g;
800 base->flags |= pc->flags & (POS_VELOCITIES | POS_FORCES);
801 gmx_ana_index_set(&gp, pc->b.nra, pc->b.a, 0);
802 gmx_ana_index_set(&gb, base->b.nra, base->b.a, 0);
803 isize = gmx_ana_index_difference_size(&gp, &gb);
806 gmx_ana_index_clear(&g);
807 gmx_ana_index_reserve(&g, base->b.nra + isize);
808 /* Find the new blocks */
809 gmx_ana_index_difference(&g, &gp, &gb);
810 /* Count the blocks in g */
814 while (pc->b.a[pc->b.index[bi]] != g.index[i])
818 i += pc->b.index[bi+1] - pc->b.index[bi];
822 /* Merge the atoms into a temporary structure */
823 gmx_ana_index_merge(&g, &gb, &g);
824 /* Merge the blocks */
825 srenew(base->b.index, base->b.nr + bnr + 1);
829 bo = base->b.nr + bnr - 1;
830 base->b.index[bo+1] = i + 1;
833 if (bi < 0 || base->b.a[base->b.index[bi+1]-1] != g.index[i])
835 i -= pc->b.index[bj+1] - pc->b.index[bj];
840 if (bj >= 0 && pc->b.a[pc->b.index[bj+1]-1] == g.index[i])
844 i -= base->b.index[bi+1] - base->b.index[bi];
847 base->b.index[bo] = i + 1;
851 base->b.nalloc_index += bnr;
853 base->b.nra = g.isize;
855 base->b.nalloc_a = g.isize;
856 /* Refresh the gmax field */
857 gmx_ana_index_set(&base->gmax, base->b.nra, base->b.a, 0);
862 * Merges two bases into one.
864 * \param[in,out] tbase Base calculation to merge to.
865 * \param[in] mbase Base calculation to merge to \p tbase.
867 * After the call, \p mbase has been freed and \p tbase is used as the base
868 * for all calculations that previously had \p mbase as their base.
869 * It is assumed that any overlap between \p tbase and \p mbase is in complete
870 * blocks, i.e., that the merge is possible.
873 merge_bases(gmx_ana_poscalc_t *tbase, gmx_ana_poscalc_t *mbase)
875 gmx_ana_poscalc_t *pc;
877 merge_to_base(tbase, mbase);
878 mbase->coll->removeCalculation(mbase);
879 /* Set tbase as the base for all calculations that had mbase */
880 pc = tbase->coll->first_;
883 if (pc->sbase == mbase)
892 gmx_ana_poscalc_free(mbase);
896 * Setups the static base calculation for a position calculation.
898 * \param[in,out] pc Position calculation to setup the base for.
901 setup_base(gmx_ana_poscalc_t *pc)
903 gmx_ana_poscalc_t *base, *pbase, *next;
904 gmx_ana_index_t gp, g;
906 /* Exit immediately if pc should not have a base. */
907 if (!can_use_base(pc))
912 gmx_ana_index_set(&gp, pc->b.nra, pc->b.a, 0);
913 gmx_ana_index_clear(&g);
914 gmx_ana_index_reserve(&g, pc->b.nra);
916 base = pc->coll->first_;
919 /* Save the next calculation so that we can safely delete base */
921 /* Skip pc, calculations that already have a base (we should match the
922 * base instead), as well as calculations that should not have a base.
923 * If the above conditions are met, check whether we should do a
926 if (base != pc && !base->sbase && can_use_base(base)
927 && should_merge(pbase, base, &gp, &g))
929 /* Check whether this is the first base found */
932 /* Create a real base if one is not present */
935 pbase = create_simple_base(base);
941 /* Make it a base for pc as well */
942 merge_to_base(pbase, pc);
946 else /* This was not the first base */
950 /* If it is not a real base, just make the new base as
951 * the base for it as well. */
952 merge_to_base(pbase, base);
958 /* If base is a real base, merge it with the new base
960 merge_bases(pbase, base);
963 gmx_ana_index_set(&gp, pbase->b.nra, pbase->b.a, 0);
964 gmx_ana_index_reserve(&g, pc->b.nra);
966 /* Proceed to the next unchecked calculation */
970 gmx_ana_index_deinit(&g);
972 /* If no base was found, create one if one is required */
973 if (!pc->sbase && (pc->flags & POS_DYNAMIC)
974 && (pc->flags & (POS_COMPLMAX | POS_COMPLWHOLE)))
976 create_simple_base(pc);
981 * \param[in,out] pc Position calculation data structure.
982 * \param[in] flags New flags.
984 * \p flags are added to the old flags.
985 * If calculation type is \ref POS_ATOM, \ref POS_MASS is automatically
987 * If both \ref POS_DYNAMIC and \ref POS_MASKONLY are provided,
988 * \ref POS_DYNAMIC is cleared.
989 * If calculation type is not \ref POS_RES or \ref POS_MOL,
990 * \ref POS_COMPLMAX and \ref POS_COMPLWHOLE are automatically cleared.
993 gmx_ana_poscalc_set_flags(gmx_ana_poscalc_t *pc, int flags)
995 if (pc->type == POS_ATOM)
999 if (flags & POS_MASKONLY)
1001 flags &= ~POS_DYNAMIC;
1003 if (pc->type != POS_RES && pc->type != POS_MOL)
1005 flags &= ~(POS_COMPLMAX | POS_COMPLWHOLE);
1011 * \param[in,out] pc Position calculation data structure.
1012 * \param[in] g Maximum index group for the calculation.
1014 * Subsequent calls to gmx_ana_poscalc_update() should use only subsets of
1015 * \p g for evaluation.
1017 * The topology should have been set for the collection of which \p pc is
1021 gmx_ana_poscalc_set_maxindex(gmx_ana_poscalc_t *pc, gmx_ana_index_t *g)
1023 set_poscalc_maxindex(pc, g, false);
1028 * \param[in] pc Position calculation data structure.
1029 * \param[out] p Output positions.
1031 * Calls to gmx_ana_poscalc_update() using \p pc should use only positions
1032 * initialized with this function.
1033 * The \c p->g pointer is initialized to point to an internal group that
1034 * contains the maximum index group set with gmx_ana_poscalc_set_maxindex().
1037 gmx_ana_poscalc_init_pos(gmx_ana_poscalc_t *pc, gmx_ana_pos_t *p)
1039 gmx_ana_indexmap_init(&p->m, &pc->gmax, pc->coll->top_, pc->itype);
1040 /* Only do the static optimization when there is no completion */
1041 if (!(pc->flags & POS_DYNAMIC) && pc->b.nra == pc->gmax.isize)
1043 gmx_ana_indexmap_set_static(&p->m, &pc->b);
1045 gmx_ana_pos_reserve(p, p->m.mapb.nr, 0);
1046 if (pc->flags & POS_VELOCITIES)
1048 gmx_ana_pos_reserve_velocities(p);
1050 if (pc->flags & POS_FORCES)
1052 gmx_ana_pos_reserve_forces(p);
1057 * \param pc Position calculation data to be freed.
1059 * The \p pc pointer is invalid after the call.
1062 gmx_ana_poscalc_free(gmx_ana_poscalc_t *pc)
1070 if (pc->refcount > 0)
1075 pc->coll->removeCalculation(pc);
1076 if (pc->b.nalloc_index > 0)
1080 if (pc->b.nalloc_a > 0)
1084 if (pc->flags & POS_COMPLWHOLE)
1086 gmx_ana_index_deinit(&pc->gmax);
1091 gmx_ana_poscalc_free(pc->sbase);
1098 * \param[in] pc Position calculation data to query.
1099 * \returns true if \p pc requires topology for initialization and/or
1100 * evaluation, false otherwise.
1103 gmx_ana_poscalc_requires_top(gmx_ana_poscalc_t *pc)
1105 if ((pc->flags & POS_MASS) || pc->type == POS_RES || pc->type == POS_MOL
1106 || ((pc->flags & POS_FORCES) && pc->type != POS_ATOM))
1114 * \param[in] pc Position calculation data.
1115 * \param[in,out] p Output positions, initialized previously with
1116 * gmx_ana_poscalc_init_pos() using \p pc.
1117 * \param[in] g Index group to use for the update.
1118 * \param[in] fr Current frame.
1119 * \param[in] pbc PBC data, or NULL if no PBC should be used.
1121 * gmx_ana_poscalc_init_frame() should be called for each frame before calling
1125 gmx_ana_poscalc_update(gmx_ana_poscalc_t *pc, gmx_ana_pos_t *p,
1126 gmx_ana_index_t *g, t_trxframe *fr, t_pbc *pbc)
1130 if (pc->bEval == true && !(pc->flags & POS_MASKONLY))
1136 gmx_ana_poscalc_update(pc->sbase, NULL, NULL, fr, pbc);
1147 /* Update the index map */
1148 if (pc->flags & POS_DYNAMIC)
1150 gmx_ana_indexmap_update(&p->m, g, false);
1152 else if (pc->flags & POS_MASKONLY)
1154 gmx_ana_indexmap_update(&p->m, g, true);
1160 if (!(pc->flags & POS_DYNAMIC))
1165 /* Evaluate the positions */
1168 /* TODO: It might be faster to evaluate the positions within this
1169 * loop instead of in the beginning. */
1170 if (pc->flags & POS_DYNAMIC)
1172 for (bi = 0; bi < p->count(); ++bi)
1174 bj = pc->baseid[p->m.refid[bi]];
1175 copy_rvec(pc->sbase->p->x[bj], p->x[bi]);
1179 for (bi = 0; bi < p->count(); ++bi)
1181 bj = pc->baseid[p->m.refid[bi]];
1182 copy_rvec(pc->sbase->p->v[bj], p->v[bi]);
1187 for (bi = 0; bi < p->count(); ++bi)
1189 bj = pc->baseid[p->m.refid[bi]];
1190 copy_rvec(pc->sbase->p->f[bj], p->f[bi]);
1196 for (bi = 0; bi < p->count(); ++bi)
1198 bj = pc->baseid[bi];
1199 copy_rvec(pc->sbase->p->x[bj], p->x[bi]);
1203 for (bi = 0; bi < p->count(); ++bi)
1205 bj = pc->baseid[bi];
1206 copy_rvec(pc->sbase->p->v[bj], p->v[bi]);
1211 for (bi = 0; bi < p->count(); ++bi)
1213 bj = pc->baseid[bi];
1214 copy_rvec(pc->sbase->p->f[bj], p->f[bi]);
1219 else /* pc->sbase is NULL */
1221 if (pc->flags & POS_DYNAMIC)
1223 pc->b.nr = p->m.mapb.nr;
1224 pc->b.index = p->m.mapb.index;
1225 pc->b.nra = g->isize;
1228 if (p->v && !fr->bV)
1230 for (i = 0; i < pc->b.nra; ++i)
1232 clear_rvec(p->v[i]);
1235 if (p->f && !fr->bF)
1237 for (i = 0; i < pc->b.nra; ++i)
1239 clear_rvec(p->f[i]);
1242 /* Here, we assume that the topology has been properly initialized,
1243 * and do not check the return values of gmx_calc_comg*(). */
1244 t_topology *top = pc->coll->top_;
1245 bool bMass = pc->flags & POS_MASS;
1249 for (i = 0; i < pc->b.nra; ++i)
1251 copy_rvec(fr->x[pc->b.a[i]], p->x[i]);
1255 for (i = 0; i < pc->b.nra; ++i)
1257 copy_rvec(fr->v[pc->b.a[i]], p->v[i]);
1262 for (i = 0; i < pc->b.nra; ++i)
1264 copy_rvec(fr->f[pc->b.a[i]], p->f[i]);
1269 gmx_calc_comg(top, fr->x, pc->b.nra, pc->b.a, bMass, p->x[0]);
1272 gmx_calc_comg(top, fr->v, pc->b.nra, pc->b.a, bMass, p->v[0]);
1276 gmx_calc_comg_f(top, fr->f, pc->b.nra, pc->b.a, bMass, p->f[0]);
1280 gmx_calc_comg_pbc(top, fr->x, pbc, pc->b.nra, pc->b.a, bMass, p->x[0]);
1283 gmx_calc_comg(top, fr->v, pc->b.nra, pc->b.a, bMass, p->v[0]);
1287 gmx_calc_comg_f(top, fr->f, pc->b.nra, pc->b.a, bMass, p->f[0]);
1291 gmx_calc_comg_blocka(top, fr->x, &pc->b, bMass, p->x);
1294 gmx_calc_comg_blocka(top, fr->v, &pc->b, bMass, p->v);
1298 gmx_calc_comg_f_blocka(top, fr->f, &pc->b, bMass, p->f);