3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements internal selection method for comparison expressions.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_selection
46 #include "gromacs/selection/selmethod.h"
47 #include "gromacs/utility/exceptions.h"
49 /** Defines the comparison operator for comparison expressions. */
52 CMP_INVALID, /**< Indicates an error */
57 CMP_EQUAL, /**< '==' */
61 /** The operand has a single value. */
62 #define CMP_SINGLEVAL 1
63 /** The operand value is dynamic. */
64 #define CMP_DYNAMICVAL 2
65 /** The value is real. */
67 /** The integer array is allocated. */
68 #define CMP_ALLOCINT 16
69 /** The real array is allocated. */
70 #define CMP_ALLOCREAL 32
73 * Data structure for comparison expression operand values.
77 /** Flags that describe the type of the operand. */
79 /** (Array of) integer value(s). */
81 /** (Array of) real value(s). */
86 * Data structure for comparison expression evaluation.
90 /** Comparison operator as a string. */
92 /** Comparison operator type. */
97 t_compare_value right;
98 } t_methoddata_compare;
100 /** Allocates data for comparison expression evaluation. */
102 init_data_compare(int npar, gmx_ana_selparam_t *param);
103 /** Initializes data for comparison expression evaluation. */
105 init_compare(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
106 /** Frees the memory allocated for comparison expression evaluation. */
108 free_data_compare(void *data);
109 /** Evaluates comparison expressions. */
111 evaluate_compare(t_topology *top, t_trxframe *fr, t_pbc *pbc,
112 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
114 /** Parameters for comparison expression evaluation. */
115 static gmx_ana_selparam_t smparams_compare[] = {
116 {"int1", {INT_VALUE, -1, {NULL}}, NULL,
117 SPAR_OPTIONAL | SPAR_DYNAMIC | SPAR_ATOMVAL},
118 {"real1", {REAL_VALUE, -1, {NULL}}, NULL,
119 SPAR_OPTIONAL | SPAR_DYNAMIC | SPAR_ATOMVAL},
120 {"op", {STR_VALUE, 1, {NULL}}, NULL, 0},
121 {"int2", {INT_VALUE, -1, {NULL}}, NULL,
122 SPAR_OPTIONAL | SPAR_DYNAMIC | SPAR_ATOMVAL},
123 {"real2", {REAL_VALUE, -1, {NULL}}, NULL,
124 SPAR_OPTIONAL | SPAR_DYNAMIC | SPAR_ATOMVAL},
127 /** \internal Selection method data for comparison expression evaluation. */
128 gmx_ana_selmethod_t sm_compare = {
129 "cmp", GROUP_VALUE, SMETH_SINGLEVAL,
130 asize(smparams_compare), smparams_compare,
143 * Returns a \c e_comparison_t value corresponding to an operator.
145 * \param[in] str String to process.
146 * \returns The comparison type corresponding to the first one or two
147 * characters of \p str.
149 * \p str can contain any number of characters; only the first two
151 * If the beginning of \p str does not match any of the recognized types,
152 * \ref CMP_INVALID is returned.
154 static e_comparison_t
155 comparison_type(char *str)
159 case '<': return (str[1] == '=') ? CMP_LEQ : CMP_LESS;
160 case '>': return (str[1] == '=') ? CMP_GEQ : CMP_GTR;
161 case '=': return (str[1] == '=') ? CMP_EQUAL : CMP_INVALID;
162 case '!': return (str[1] == '=') ? CMP_NEQ : CMP_INVALID;
168 * Returns a string corresponding to a \c e_comparison_t value.
170 * \param[in] cmpt Comparison type to convert.
171 * \returns Pointer to a string that corresponds to \p cmpt.
173 * The return value points to a string constant and should not be \p free'd.
175 * The function returns NULL if \p cmpt is not one of the valid values.
178 comparison_type_str(e_comparison_t cmpt)
182 case CMP_INVALID: return "INVALID"; break;
183 case CMP_LESS: return "<"; break;
184 case CMP_LEQ: return "<="; break;
185 case CMP_GTR: return ">"; break;
186 case CMP_GEQ: return ">="; break;
187 case CMP_EQUAL: return "=="; break;
188 case CMP_NEQ: return "!="; break;
194 * \param[in] fp File to receive the output.
195 * \param[in] data Should point to a \c t_methoddata_compare.
198 _gmx_selelem_print_compare_info(FILE *fp, void *data)
200 t_methoddata_compare *d = (t_methoddata_compare *)data;
203 /* Print the left value */
204 if ((d->left.flags & CMP_SINGLEVAL) && !(d->left.flags & CMP_DYNAMICVAL))
206 if (d->left.flags & CMP_REALVAL)
208 fprintf(fp, "%f ", d->left.r[0]);
212 fprintf(fp, "%d ", d->left.i[0]);
215 /* Print the operator */
216 if (d->cmpt != CMP_INVALID)
218 fprintf(fp, "%s", comparison_type_str(d->cmpt));
222 fprintf(fp, "%s", d->cmpop);
224 /* Print the right value */
225 if ((d->right.flags & CMP_SINGLEVAL) && !(d->right.flags & CMP_DYNAMICVAL))
227 if (d->right.flags & CMP_REALVAL)
229 fprintf(fp, " %f", d->right.r[0]);
233 fprintf(fp, " %d", d->right.i[0]);
240 * \param[in] npar Not used (should be 5).
241 * \param[in,out] param Method parameters (should point to a copy of
242 * \ref smparams_compare).
243 * \returns Pointer to the allocated data (\c t_methoddata_compare).
245 * Allocates memory for a \c t_methoddata_compare structure.
248 init_data_compare(int npar, gmx_ana_selparam_t *param)
250 t_methoddata_compare *data;
253 param[2].val.u.s = &data->cmpop;
258 * Reverses a comparison operator.
260 * \param[in] type Comparison operator to reverse.
261 * \returns The correct comparison operator that equals \p type when the
262 * left and right sides are interchanged.
264 static e_comparison_t
265 reverse_comparison_type(e_comparison_t type)
269 case CMP_LESS: return CMP_GTR;
270 case CMP_LEQ: return CMP_GEQ;
271 case CMP_GTR: return CMP_LESS;
272 case CMP_GEQ: return CMP_LEQ;
279 * Initializes the value storage for comparison expression.
281 * \param[out] val Value structure to initialize.
282 * \param[in] param Parameters to use for initialization.
283 * \returns The number of values provided for the value, 0 on error.
286 init_comparison_value(t_compare_value *val, gmx_ana_selparam_t param[2])
291 if (param[0].flags & SPAR_SET)
293 val->flags |= (param[0].flags & SPAR_DYNAMIC) ? CMP_DYNAMICVAL : 0;
294 val->flags |= !(param[0].flags & SPAR_ATOMVAL) ? CMP_SINGLEVAL : 0;
296 val->i = param[0].val.u.i;
298 else if (param[1].flags & SPAR_SET)
300 val->flags |= (param[1].flags & SPAR_DYNAMIC) ? CMP_DYNAMICVAL : 0;
301 val->flags |= !(param[1].flags & SPAR_ATOMVAL) ? CMP_SINGLEVAL : 0;
302 val->flags |= CMP_REALVAL;
304 val->r = param[1].val.u.r;
316 * Converts an integer value to floating point.
318 * \param[in] n Number of values in the \p val->u array.
319 * \param[in,out] val Value to convert.
322 convert_int_real(int n, t_compare_value *val)
328 for (i = 0; i < n; ++i)
330 rv[i] = (real)val->i[i];
332 /* Free the previous value if one is present. */
335 val->flags |= CMP_REALVAL | CMP_ALLOCREAL;
339 * Converts a floating point value to integer.
341 * \param[in] n Number of values in the \p val->u array.
342 * \param[in,out] val Value to convert.
343 * \param[in] cmpt Comparison operator type.
344 * \param[in] bRight true if \p val appears on the right hand size of
346 * \returns 0 on success, EINVAL on error.
348 * The values are rounded such that the same comparison operator can be used.
351 convert_real_int(int n, t_compare_value *val, e_comparison_t cmpt, bool bRight)
358 cmpt = reverse_comparison_type(cmpt);
361 /* Round according to the comparison type */
362 for (i = 0; i < n; ++i)
368 iv[i] = (int)ceil(val->r[i]);
372 iv[i] = (int)floor(val->r[i]);
377 /* TODO: Implement, although it isn't typically very useful.
378 * Implementation is only a matter or proper initialization,
379 * the evaluation function can already handle this case with
380 * proper preparations. */
381 GMX_THROW(gmx::NotImplementedError("Equality comparison between dynamic integer and static real expressions not implemented"));
382 case CMP_INVALID: /* Should not be reached */
384 GMX_THROW(gmx::InternalError("Invalid comparison type"));
387 /* Free the previous value if one is present. */
390 val->flags &= ~CMP_REALVAL;
391 val->flags |= CMP_ALLOCINT;
395 * \param[in] top Not used.
396 * \param[in] npar Not used (should be 5).
397 * \param[in] param Method parameters (should point to \ref smparams_compare).
398 * \param[in] data Should point to a \c t_methoddata_compare.
399 * \returns 0 if the input data is valid, -1 on error.
402 init_compare(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
404 t_methoddata_compare *d = (t_methoddata_compare *)data;
407 /* Store the values */
408 n1 = init_comparison_value(&d->left, ¶m[0]);
409 n2 = init_comparison_value(&d->right, ¶m[3]);
410 if (n1 == 0 || n2 == 0)
412 GMX_THROW(gmx::InternalError("One of the values for comparison missing"));
414 /* Store the comparison type */
415 d->cmpt = comparison_type(d->cmpop);
416 if (d->cmpt == CMP_INVALID)
418 GMX_THROW(gmx::InternalError("Invalid comparison type"));
420 /* Convert the values to the same type */
421 /* TODO: Currently, there are no dynamic integer-valued selection methods,
422 * which means that only the branches with convert_int_real() will ever be
423 * taken. It should be considered whether it is necessary to support these
424 * other cases at all.
426 if ((d->left.flags & CMP_REALVAL) && !(d->right.flags & CMP_REALVAL))
428 if (d->left.flags & d->right.flags & CMP_DYNAMICVAL)
430 /* Nothing can be done */
432 else if (!(d->right.flags & CMP_DYNAMICVAL))
434 convert_int_real(n2, &d->right);
436 else /* d->left is static */
438 convert_real_int(n1, &d->left, d->cmpt, false);
441 else if (!(d->left.flags & CMP_REALVAL) && (d->right.flags & CMP_REALVAL))
443 if (d->left.flags & d->right.flags & CMP_DYNAMICVAL)
445 /* Reverse the sides to place the integer on the right */
447 d->left.r = d->right.r;
449 d->right.i = d->left.i;
451 flags = d->left.flags;
452 d->left.flags = d->right.flags;
453 d->right.flags = flags;
454 d->cmpt = reverse_comparison_type(d->cmpt);
456 else if (!(d->left.flags & CMP_DYNAMICVAL))
458 convert_int_real(n1, &d->left);
460 else /* d->right is static */
462 convert_real_int(n2, &d->right, d->cmpt, true);
468 * \param data Data to free (should point to a \c t_methoddata_compare).
470 * Frees the memory allocated for \c t_methoddata_compare.
473 free_data_compare(void *data)
475 t_methoddata_compare *d = (t_methoddata_compare *)data;
478 if (d->left.flags & CMP_ALLOCINT)
482 if (d->left.flags & CMP_ALLOCREAL)
486 if (d->right.flags & CMP_ALLOCINT)
490 if (d->right.flags & CMP_ALLOCREAL)
498 * \param[in] top Not used.
499 * \param[in] fr Not used.
500 * \param[in] pbc Not used.
501 * \param[in] g Evaluation index group.
502 * \param[out] out Output data structure (\p out->u.g is used).
503 * \param[in] data Should point to a \c t_methoddata_compare.
506 evaluate_compare_int(t_topology *top, t_trxframe *fr, t_pbc *pbc,
507 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
509 t_methoddata_compare *d = (t_methoddata_compare *)data;
514 for (i = i1 = i2 = ig = 0; i < g->isize; ++i)
521 case CMP_INVALID: break;
522 case CMP_LESS: bAccept = a < b; break;
523 case CMP_LEQ: bAccept = a <= b; break;
524 case CMP_GTR: bAccept = a > b; break;
525 case CMP_GEQ: bAccept = a >= b; break;
526 case CMP_EQUAL: bAccept = a == b; break;
527 case CMP_NEQ: bAccept = a != b; break;
531 out->u.g->index[ig++] = g->index[i];
533 if (!(d->left.flags & CMP_SINGLEVAL))
537 if (!(d->right.flags & CMP_SINGLEVAL))
542 out->u.g->isize = ig;
546 * \param[in] top Not used.
547 * \param[in] fr Not used.
548 * \param[in] pbc Not used.
549 * \param[in] g Evaluation index group.
550 * \param[out] out Output data structure (\p out->u.g is used).
551 * \param[in] data Should point to a \c t_methoddata_compare.
554 evaluate_compare_real(t_topology *top, t_trxframe *fr, t_pbc *pbc,
555 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
557 t_methoddata_compare *d = (t_methoddata_compare *)data;
562 for (i = i1 = i2 = ig = 0; i < g->isize; ++i)
565 b = (d->right.flags & CMP_REALVAL) ? d->right.r[i2] : d->right.i[i2];
569 case CMP_INVALID: break;
570 case CMP_LESS: bAccept = a < b; break;
571 case CMP_LEQ: bAccept = a <= b; break;
572 case CMP_GTR: bAccept = a > b; break;
573 case CMP_GEQ: bAccept = a >= b; break;
574 case CMP_EQUAL: bAccept = gmx_within_tol(a, b, GMX_REAL_EPS); break;
575 case CMP_NEQ: bAccept = !gmx_within_tol(a, b, GMX_REAL_EPS); break;
579 out->u.g->index[ig++] = g->index[i];
581 if (!(d->left.flags & CMP_SINGLEVAL))
585 if (!(d->right.flags & CMP_SINGLEVAL))
590 out->u.g->isize = ig;
594 * \param[in] top Not used.
595 * \param[in] fr Not used.
596 * \param[in] pbc Not used.
597 * \param[in] g Evaluation index group.
598 * \param[out] out Output data structure (\p out->u.g is used).
599 * \param[in] data Should point to a \c t_methoddata_compare.
602 evaluate_compare(t_topology *top, t_trxframe *fr, t_pbc *pbc,
603 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
605 t_methoddata_compare *d = (t_methoddata_compare *)data;
607 if (!((d->left.flags | d->right.flags) & CMP_REALVAL))
609 evaluate_compare_int(top, fr, pbc, g, out, data);
613 evaluate_compare_real(top, fr, pbc, g, out, data);