LCOV - code coverage report
Current view: top level - xdrfile - xdrfile_xtc.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage (other modules) Lines: 27 33 81.8 %
Date: 2024-10-11 08:09:49 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             : Copyright (c) 2009-2014, Erik Lindahl & David van der Spoel
       3             : All rights reserved.
       4             : 
       5             : Redistribution and use in source and binary forms, with or without
       6             : modification, are permitted provided that the following conditions are met:
       7             : 
       8             : 1. Redistributions of source code must retain the above copyright notice, this
       9             : list of conditions and the following disclaimer.
      10             : 
      11             : 2. Redistributions in binary form must reproduce the above copyright notice,
      12             : this list of conditions and the following disclaimer in the documentation
      13             : and/or other materials provided with the distribution.
      14             : 
      15             : THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
      16             : AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      17             : IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
      18             : DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
      19             : FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      20             : DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
      21             : SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
      22             : CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
      23             : OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      24             : OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      25             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      26             : /* -*- mode: c; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- 
      27             :  *
      28             :  * $Id$
      29             :  *
      30             :  * Copyright (c) 2009-2014, Erik Lindahl & David van der Spoel
      31             :  * All rights reserved.
      32             :  *
      33             :  * Redistribution and use in source and binary forms, with or without
      34             :  * modification, are permitted provided that the following conditions are met:
      35             :  *
      36             :  * 1. Redistributions of source code must retain the above copyright notice, this
      37             :  * list of conditions and the following disclaimer.
      38             :  *
      39             :  * 2. Redistributions in binary form must reproduce the above copyright notice,
      40             :  * this list of conditions and the following disclaimer in the documentation
      41             :  * and/or other materials provided with the distribution.
      42             :  *
      43             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
      44             :  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      45             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
      46             :  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
      47             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      48             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
      49             :  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
      50             :  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
      51             :  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      52             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      53             :  */
      54             :  
      55             : #include <stdlib.h>
      56             : #include "xdrfile.h"
      57             : #include "xdrfile_xtc.h"
      58             :         
      59             : #define MAGIC 1995
      60             : 
      61             : namespace PLMD {
      62             : namespace xdrfile {
      63             : 
      64             : enum { FALSE, TRUE };
      65             : 
      66         104 : static int xtc_header(XDRFILE *xd,int *natoms,int *step,float *time,mybool bRead)
      67             : {
      68             :         int result,magic,n=1;
      69             :         
      70             :         /* Note: read is same as write. He he he */
      71         104 :         magic  = MAGIC;
      72         104 :         if ((result = xdrfile_write_int(&magic,n,xd)) != n)
      73             :                 {
      74           2 :                         if (bRead)
      75             :                                 return exdrENDOFFILE;
      76             :                         else
      77           0 :                                 return exdrINT;
      78             :                 }
      79         102 :         if (magic != MAGIC)
      80             :                 return exdrMAGIC;
      81         102 :         if ((result = xdrfile_write_int(natoms,n,xd)) != n)
      82             :                 return exdrINT;
      83         102 :         if ((result = xdrfile_write_int(step,n,xd)) != n)
      84             :                 return exdrINT;
      85         102 :         if ((result = xdrfile_write_float(time,n,xd)) != n)
      86           0 :                 return exdrFLOAT;
      87             :         
      88             :         return exdrOK;
      89             : }
      90             : 
      91         100 : static int xtc_coord(XDRFILE *xd,int *natoms,matrix box,rvec *x,float *prec,
      92             :                                          mybool bRead)
      93             : {
      94             :         int i,j,result;
      95             :     
      96             :         /* box */
      97         100 :         result = xdrfile_read_float(box[0],DIM*DIM,xd);
      98         100 :         if (DIM*DIM != result)
      99             :                 return exdrFLOAT;
     100             :         else 
     101             :                 {
     102         100 :                         if (bRead)
     103             :                                 {
     104          28 :                                         result = xdrfile_decompress_coord_float(x[0],natoms,prec,xd); 
     105          28 :                                         if (result != *natoms)
     106           0 :                                                 return exdr3DX;
     107             :                                 }
     108             :                         else
     109             :                                 {
     110          72 :                                         result = xdrfile_compress_coord_float(x[0],*natoms,*prec,xd); 
     111          72 :                                         if (result != *natoms)
     112           0 :                                                 return exdr3DX;
     113             :                                 }
     114             :                 }
     115             :         return exdrOK;
     116             : }
     117             : 
     118           2 : int read_xtc_natoms(char *fn,int *natoms)
     119             : {
     120             :         XDRFILE *xd;
     121             :         int step,result;
     122             :         float time;
     123             :         
     124           2 :         xd = xdrfile_open(fn,"r");
     125           2 :         if (NULL == xd)
     126             :                 return exdrFILENOTFOUND;
     127           2 :         result = xtc_header(xd,natoms,&step,&time,TRUE);
     128           2 :         xdrfile_close(xd);
     129             :         
     130             :         return result;
     131             : }
     132             : 
     133          30 : int read_xtc(XDRFILE *xd,
     134             :                          int natoms,int *step,float *time,
     135             :                          matrix box,rvec *x,float *prec)
     136             : /* Read subsequent frames */
     137             : {
     138             :         int result;
     139             :   
     140          30 :         if ((result = xtc_header(xd,&natoms,step,time,TRUE)) != exdrOK)
     141             :                 return result;
     142             :           
     143          28 :         if ((result = xtc_coord(xd,&natoms,box,x,prec,1)) != exdrOK)
     144           0 :                 return result;
     145             :   
     146             :         return exdrOK;
     147             : }
     148             : 
     149          72 : int write_xtc(XDRFILE *xd,
     150             :                           int natoms,int step,float time,
     151             :                           matrix box,rvec *x,float prec)
     152             : /* Write a frame to xtc file */
     153             : {
     154             :         int result;
     155             :   
     156          72 :         if ((result = xtc_header(xd,&natoms,&step,&time,FALSE)) != exdrOK)
     157             :                 return result;
     158             : 
     159          72 :         if ((result = xtc_coord(xd,&natoms,box,x,&prec,0)) != exdrOK)
     160           0 :                 return result;
     161             :   
     162             :         return exdrOK;
     163             : }
     164             : }
     165             : }

Generated by: LCOV version 1.15