GridMap.cpp
上传用户:chinafayin
上传日期:2022-04-05
资源大小:153k
文件大小:7k
源码类别:

并行计算

开发平台:

Visual C++

  1. /*
  2.     FastGrid (formerly AutoGrid)
  3.     Copyright (C) 2009 The Scripps Research Institute. All rights reserved.
  4.     Copyright (C) 2009 Masaryk University. All rights reserved.
  5.     AutoGrid is a Trade Mark of The Scripps Research Institute.
  6.     This program is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU General Public License
  8.     as published by the Free Software Foundation; either version 2
  9.     of the License, or (at your option) any later version.
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17. */
  18. #include <new>
  19. #include <cstring>
  20. #include "GridMap.h"
  21. #include "Utils.h"
  22. GridMap::GridMap()
  23. {
  24.     memset(this, 0, sizeof(*this));
  25. }
  26. ///////////////////////////////////////////////////////////////////////////////
  27. GridMapList::GridMapList(LogFile *logFile): numMaps(0), numAtomMaps(0), elecIndex(0), desolvIndex(0), gridmaps(0),
  28.     logFile(logFile), floatingGridMins(0), useFloatingGrid(false)
  29. {}
  30. GridMapList::~GridMapList()
  31. {
  32.     for (int i = 0; i < numMaps; i++)
  33.         if (gridmaps[i].filename[0])
  34.             delete [] gridmaps[i].energies;
  35.     if (useFloatingGrid)
  36.         delete [] floatingGridMins;
  37.     if (gridmaps)
  38.         delete [] gridmaps;
  39. }
  40. void GridMapList::setNumMaps(int numMaps)
  41. {
  42.     if (gridmaps)
  43.         delete [] gridmaps;
  44.     numAtomMaps = numMaps - 2;
  45.     elecIndex = numMaps - 2;
  46.     desolvIndex = numMaps - 1;
  47.     this->numMaps = numMaps;
  48.     gridmaps = new(std::nothrow) GridMap[numMaps];
  49.     if (!gridmaps)
  50.     {
  51.         logFile->printError(ERROR, "Could not allocate memory to create the GridMap "gridmaps".n");
  52.         logFile->printError(FATAL_ERROR, "Unsuccessful completion.nn");
  53.     }
  54. }
  55. void GridMapList::logSummary()
  56. {
  57.     // Print a summary of extrema-values from the atomic-affinity and
  58.     // electrostatics grid-maps,
  59.     logFile->print("nGridtAtomtMinimum   tMaximumn"
  60.                   "Map tTypetEnergy    tEnergy n"
  61.                   "tt(kcal/mol)t(kcal/mol)n"
  62.                   "____t____t_____________t_____________n");
  63.     for (int i = 0; i < numMaps; i++)
  64.     {
  65.         double energyMin, energyMax;
  66.         calculateEnergyMinMax(i, energyMin, energyMax);
  67.         logFile->printFormatted(" %dt %st  %6.2lft%9.2len", i + 1, gridmaps[i].type, energyMin, energyMax);
  68.     }
  69.     double energyMinElec, energyMaxElec;
  70.     double energyMinDesolv, energyMaxDesolv;
  71.     calculateEnergyMinMax(elecIndex, energyMinElec, energyMaxElec);
  72.     calculateEnergyMinMax(desolvIndex, energyMinDesolv, energyMaxDesolv);
  73.     logFile->printFormatted(" %dt %ct  %6.2lft%9.2letElectrostatic Potentialn"
  74.                            " %dt %ct  %6.2lft%9.2letDesolvation Potentialn"
  75.                            "nn * Note:  Every pairwise-atomic interaction was clamped at %.2fnnn",
  76.                            getElectrostaticMapIndex() + 1, 'e', energyMinElec, energyMaxElec,
  77.                            getDesolvationMapIndex() + 1, 'd', energyMinDesolv, energyMaxDesolv,
  78.                            EINTCLAMP);
  79.     fprintf(stderr, "n%s: Successful Completion.n", logFile->getProgramName());
  80.     logFile->printTitled("Successful Completion.n");
  81. }
  82. void GridMapList::enableFloatingGrid()
  83. {
  84.     useFloatingGrid = true;
  85. }
  86. int GridMapList::getNumMapsInclFloatingGrid() const
  87. {
  88.     return numMaps + (useFloatingGrid ? 1 : 0);
  89. }
  90. void GridMapList::prepareGridmaps(int numGridPointsPerMap)
  91. {
  92.     this->numGridPointsPerMap = numGridPointsPerMap;
  93.     for (int i = 0; i < numMaps; i++)
  94.         if (gridmaps[i].filename[0])
  95.         {
  96.             gridmaps[i].energies = new double[numGridPointsPerMap];
  97.             memset(gridmaps[i].energies, 0, numGridPointsPerMap * sizeof(double));
  98.         }
  99.     if (useFloatingGrid)
  100.         floatingGridMins = new float[numGridPointsPerMap];
  101. }
  102. void GridMapList::initFileHeader(const InputData *input, const char *gridParameterFilename)
  103. {
  104.     // Write out the correct grid data '.fld' filename at the head of each map file,
  105.     // to avoid centering errors in subsequent dockings...
  106.     // AutoDock can then check to see if the center of each map matches that
  107.     // specified in its parameter file...
  108.     // The header of all files
  109.     fileHeaderLength = snprintf(fileHeader, 1<<14,
  110.         "GRID_PARAMETER_FILE %sn"
  111.         "GRID_DATA_FILE %sn"
  112.         "MACROMOLECULE %sn"
  113.         "SPACING %.3lfn"
  114.         "NELEMENTS %d %d %dn"
  115.         "CENTER %.3lf %.3lf %.3lfn",
  116.         gridParameterFilename, input->fldFilenameAVS, input->receptorFilename, input->gridSpacing,
  117.         input->numGridPoints.x-1, input->numGridPoints.y-1, input->numGridPoints.z-1,
  118.         input->gridCenter.x, input->gridCenter.y, input->gridCenter.z);
  119. }
  120. void GridMapList::saveElectrostaticMap() const
  121. {
  122.     saveGridMap(elecIndex);
  123. }
  124. void GridMapList::saveAtomMapsAndDesolvMap() const
  125. {
  126.     for (int i = 0; i < numAtomMaps; i++)
  127.         saveGridMap(i);
  128.     
  129.     saveGridMap(desolvIndex);
  130. }
  131. void GridMapList::saveFloatingGrid(const char *floatingGridFilename) const
  132. {
  133.     // Floating grid
  134.     if (useFloatingGrid)
  135.     {
  136.         // Open the file
  137.         FILE *file = 0;
  138.         if ((file = boincOpenFile(floatingGridFilename, "w")) == 0)
  139.         {
  140.             logFile->printErrorFormatted(ERROR, "can't open grid map "%s" for writing.n", floatingGridFilename);
  141.             logFile->printError(FATAL_ERROR, "Unsuccessful completion.nn");
  142.         }
  143.         if (!fwrite(fileHeader, fileHeaderLength, 1, file))
  144.             logFile->printError(FATAL_ERROR, "Not enough disk space.");
  145.         // Save the floating grid
  146.         for (int j = 0; j < numGridPointsPerMap; j++)
  147.             fprintf(file, "%.3fn", floatingGridMins[j]);
  148.         fclose(file);
  149.     }
  150. }
  151. void GridMapList::saveGridMap(int index) const
  152. {
  153.     if (gridmaps[index].filename[0])
  154.     {
  155.         // Open the file
  156.         FILE *file;
  157.         if ((file = boincOpenFile(gridmaps[index].filename, "w")) == 0)
  158.         {
  159.             logFile->printErrorFormatted(ERROR, "Cannot open grid map "%s" for writing.", gridmaps[index].filename);
  160.             logFile->printError(FATAL_ERROR, "Unsuccessful completion.nn");
  161.         }
  162.         if (!fwrite(fileHeader, fileHeaderLength, 1, file))
  163.             logFile->printError(FATAL_ERROR, "Not enough disk space.");
  164.         // Save energies
  165.         for (int j = 0; j < numGridPointsPerMap; j++)
  166.         {
  167.             double f = gridmaps[index].energies[j];
  168.             if (f == 0)
  169.             {
  170.                 if (!fwrite("0n", 2, 1, file))
  171.                     logFile->printError(FATAL_ERROR, "Not enough disk space.");
  172.             }
  173.             else
  174.                 fprintf(file, "%.3fn", f);
  175.         }
  176.         fclose(file);
  177.     }
  178. }
  179. void GridMapList::calculateEnergyMinMax(int map, double &energyMin, double &energyMax)
  180. {
  181.     energyMax = -BIG;
  182.     energyMin = BIG;
  183.     for (int j = 0; j < numGridPointsPerMap; j++)
  184.     {
  185.         energyMax = Mathd::Max(energyMax, gridmaps[map].energies[j]);
  186.         energyMin = Mathd::Min(energyMin, gridmaps[map].energies[j]);
  187.     }
  188. }