LCOV - code coverage report
Current view: top level - molfile - crdplugin.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage (other modules) Lines: 55 85 64.7 %
Date: 2024-10-11 08:09:49 Functions: 5 9 55.6 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             : University of Illinois Open Source License
       3             : Copyright 2003 Theoretical and Computational Biophysics Group, 
       4             : All rights reserved.
       5             : 
       6             : Developed by:           Theoretical and Computational Biophysics Group
       7             :                         University of Illinois at Urbana-Champaign
       8             :                         http://www.ks.uiuc.edu/
       9             : 
      10             : Permission is hereby granted, free of charge, to any person obtaining a copy of
      11             : this software and associated documentation files (the Software), to deal with 
      12             : the Software without restriction, including without limitation the rights to 
      13             : use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
      14             : of the Software, and to permit persons to whom the Software is furnished to 
      15             : do so, subject to the following conditions:
      16             : 
      17             : Redistributions of source code must retain the above copyright notice, 
      18             : this list of conditions and the following disclaimers.
      19             : 
      20             : Redistributions in binary form must reproduce the above copyright notice, 
      21             : this list of conditions and the following disclaimers in the documentation 
      22             : and/or other materials provided with the distribution.
      23             : 
      24             : Neither the names of Theoretical and Computational Biophysics Group, 
      25             : University of Illinois at Urbana-Champaign, nor the names of its contributors 
      26             : may be used to endorse or promote products derived from this Software without 
      27             : specific prior written permission.
      28             : 
      29             : THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
      30             : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
      31             : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL 
      32             : THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
      33             : OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
      34             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
      35             : OTHER DEALINGS WITH THE SOFTWARE.
      36             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      37             : #if defined(__PLUMED_HAS_MOLFILE_PLUGINS) && ! defined(__PLUMED_HAS_EXTERNAL_MOLFILE_PLUGINS)
      38             : /***************************************************************************
      39             :  *cr
      40             :  *cr            (C) Copyright 1995-2009 The Board of Trustees of the
      41             :  *cr                        University of Illinois
      42             :  *cr                         All Rights Reserved
      43             :  *cr
      44             :  ***************************************************************************/
      45             : 
      46             : /***************************************************************************
      47             :  * RCS INFORMATION:
      48             :  *
      49             :  *      $RCSfile: crdplugin.c,v $
      50             :  *      $Author: johns $       $Locker:  $             $State: Exp $
      51             :  *      $Revision: 1.39 $       $Date: 2013/04/13 03:29:48 $
      52             :  *
      53             :  ***************************************************************************/
      54             : 
      55             : /*
      56             :  * TODO: This plugin should probably be merged with the 'rst7' plugin, since
      57             :  *       the differences between them are minor, and there's no logical reason
      58             :  *       for them to be implemented completely independently as they are now.
      59             :  *       The major differences in formatting are in regard to the 6F12.7 (rst7)
      60             :  *       versus 10F8.3 (crd) ascii floating point conversion modes. 
      61             :  */
      62             : 
      63             : #include "largefiles.h"   /* platform dependent 64-bit file I/O defines */
      64             : 
      65             : #include <stdio.h>
      66             : #include <stdlib.h>
      67             : #include <string.h>
      68             : #include "molfile_plugin.h"
      69             : 
      70             : namespace PLMD{
      71             : namespace molfile{
      72             : 
      73             : typedef struct {
      74             :   FILE *file;
      75             :   int has_box;
      76             :   int numatoms;
      77             : } crddata;
      78             :  
      79           2 : static void *open_crd_read(const char *filename, const char *filetype, 
      80             :     int *natoms) {
      81             :  
      82             :   FILE *fd;
      83             :   crddata *data;
      84             :  
      85           2 :   fd = fopen(filename, "rb");
      86           2 :   if (!fd) return NULL;
      87             :   
      88             :   /* first line is title, so skip past it */
      89         162 :   while (getc(fd) != '\n');
      90             : 
      91             :   /* 
      92             :    * CRD's don't store the number of atoms in the timestep, so we assume that
      93             :    * the application will determine this for us.  
      94             :    */
      95           2 :   data = (crddata *)malloc(sizeof(crddata));
      96           2 :   data->file = fd;
      97           2 :   *natoms = MOLFILE_NUMATOMS_UNKNOWN;
      98             :   /* filetype "crd" has no box; filetype "crdbox" does. */
      99           2 :   data->has_box = strcmp(filetype, "crd"); 
     100           2 :   return data;
     101             : }
     102             : 
     103             : /*
     104             :  * CRD files with box info are indistinguishable from regular CRD's.  
     105             :  * We regard CRD's with box info as a different file format.
     106             :  * CRD's don't tell how many atoms there are in each frame.  We therefore
     107             :  * rely on the numatoms field in the molfile_timestep_t parameter.
     108             :  */
     109           5 : static int read_crd_timestep(void *mydata, int natoms, molfile_timestep_t *ts) {
     110             :   crddata *crd = (crddata *)mydata;
     111             :   int i, j;
     112             :   float x, y, z;
     113             :   float a, b, c;
     114             : 
     115             :   /* Read in the atom coordinates */
     116       21382 :   for (i=0; i<natoms; i++) {
     117       21379 :     j = fscanf(crd->file, "%f %f %f", &x, &y, &z);
     118       21379 :     if (j == EOF) {
     119             :       return MOLFILE_ERROR;
     120       21377 :     } else if (j <= 0) {
     121           0 :       fprintf(stderr, "Problem reading CRD file\n");
     122           0 :       return MOLFILE_ERROR;
     123             :     }
     124             : 
     125             :     /* only save coords if we're given a valid ts pointer */ 
     126             :     /* otherwise assume that VMD wants us to skip it.     */
     127       21377 :     if (ts != NULL) {
     128       21377 :       ts->coords[3*i  ] = x;
     129       21377 :       ts->coords[3*i+1] = y;
     130       21377 :       ts->coords[3*i+2] = z;
     131             :     }
     132             :   }
     133             : 
     134             : 
     135             :   /* Read the PBC box info. */
     136           3 :   if (crd->has_box) {
     137           2 :     j = fscanf(crd->file, "%f %f %f", &a, &b, &c);
     138           2 :     if (j == EOF) {
     139             :       printf("EOF in box\n");
     140           0 :       return MOLFILE_ERROR;
     141           2 :     } else if (j <= 0) {
     142             :       printf("Problem reading box part of CRD file, scanf returned %d\n",j);
     143           0 :       return MOLFILE_ERROR;
     144             :     }
     145             : 
     146             :     /* only save coords if we're given a valid ts pointer */ 
     147             :     /* otherwise assume that VMD wants us to skip it.     */
     148           2 :     if (ts != NULL) {
     149           2 :       ts->A = a;
     150           2 :       ts->B = b;
     151           2 :       ts->C = c;
     152             : 
     153             :       /* XXX periodic cell angles are only stored in the PARM file */
     154             :       /* we should probably retrieve these from the already-loaded */
     155             :       /* molecule when possible.                                   */
     156           2 :       ts->alpha = 90.0;
     157           2 :       ts->beta  = 90.0;
     158           2 :       ts->gamma = 90.0;
     159             :     }
     160             :   }
     161             : 
     162             :   return MOLFILE_SUCCESS;
     163             : }
     164             :     
     165           2 : static void close_crd_read(void *mydata) {
     166             :   crddata *crd = (crddata *)mydata;
     167           2 :   fclose(crd->file);
     168           2 :   free(crd);
     169           2 : }
     170             : 
     171           0 : static void *open_crd_write(const char *path, const char *filetype,
     172             :     int natoms) {
     173             :   crddata *crd;
     174             :   FILE *fd;
     175             : 
     176           0 :   fd = fopen(path, "wb");
     177           0 :   if (!fd) {
     178           0 :     fprintf(stderr, "Could not open file %s for writing\n", path);
     179           0 :     return NULL;
     180             :   }
     181             :   fprintf(fd, "TITLE : Created by VMD with %d atoms\n", natoms);
     182             :   
     183           0 :   crd = (crddata *)malloc(sizeof(crddata));
     184           0 :   crd->file = fd;
     185           0 :   crd->numatoms = natoms;
     186           0 :   crd->has_box = strcmp(filetype, "crd"); 
     187           0 :   return crd;
     188             : }    
     189             :   
     190           0 : static int write_crd_timestep(void *v, const molfile_timestep_t *ts) {
     191             :   crddata *crd = (crddata *)v;
     192             :   int i;
     193             :   int lfdone=0;
     194           0 :   const int ndata = crd->numatoms * 3;
     195             : 
     196           0 :   for (i=0; i<ndata; i++) {
     197             :     lfdone = 0;
     198           0 :     fprintf(crd->file, "%8.3f", ts->coords[i]);
     199           0 :     if ((i+1) % 10 == 0) {
     200           0 :       fprintf(crd->file, "\n"); 
     201             :       lfdone = 1;
     202             :     }
     203             :   }
     204           0 :   if (!lfdone)
     205           0 :     fprintf(crd->file, "\n"); 
     206             :     
     207           0 :   if (crd->has_box) {
     208           0 :     fprintf (crd->file, "%8.3f%8.3f%8.3f\n", ts->A, ts->B, ts->C);
     209             :   }
     210             : 
     211           0 :   return MOLFILE_SUCCESS;
     212             : }
     213             : 
     214           0 : static void close_crd_write(void *v) {
     215             :   crddata *crd = (crddata *)v;
     216           0 :   fclose(crd->file);
     217           0 :   free(crd);
     218           0 : }
     219             : 
     220             : /* registration stuff */
     221             :     
     222             : static molfile_plugin_t plugin;
     223             : static molfile_plugin_t crdboxplugin;
     224             : 
     225        6946 : VMDPLUGIN_API int VMDPLUGIN_init(void) { 
     226             :   memset(&plugin, 0, sizeof(molfile_plugin_t));
     227        6946 :   plugin.abiversion = vmdplugin_ABIVERSION;
     228        6946 :   plugin.type = MOLFILE_PLUGIN_TYPE;
     229        6946 :   plugin.name = "crd";
     230        6946 :   plugin.prettyname = "AMBER Coordinates";
     231        6946 :   plugin.author = "Justin Gullingsrud, John Stone";
     232             :   plugin.majorv = 0;
     233        6946 :   plugin.minorv = 9;
     234        6946 :   plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
     235        6946 :   plugin.filename_extension = "mdcrd,crd";
     236        6946 :   plugin.open_file_read = open_crd_read;
     237        6946 :   plugin.read_next_timestep = read_crd_timestep;
     238        6946 :   plugin.close_file_read = close_crd_read;
     239        6946 :   plugin.open_file_write = open_crd_write;
     240        6946 :   plugin.write_timestep = write_crd_timestep;
     241        6946 :   plugin.close_file_write = close_crd_write;
     242             : 
     243             :   memcpy(&crdboxplugin, &plugin, sizeof(molfile_plugin_t));
     244        6946 :   crdboxplugin.name = "crdbox";
     245        6946 :   crdboxplugin.prettyname = "AMBER Coordinates with Periodic Box";
     246             : 
     247        6946 :   return VMDPLUGIN_SUCCESS; 
     248             : }
     249             : 
     250        6946 : VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
     251        6946 :   (*cb)(v, (vmdplugin_t *)&plugin);
     252        6946 :   (*cb)(v, (vmdplugin_t *)&crdboxplugin);
     253        6946 :   return VMDPLUGIN_SUCCESS;
     254             : }
     255             : 
     256           0 : VMDPLUGIN_API int VMDPLUGIN_fini(void) { return VMDPLUGIN_SUCCESS; }
     257             : 
     258             : }
     259             : }
     260             : 
     261             : #endif
     262             : 

Generated by: LCOV version 1.15