View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2023.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.algorithms.thermodynamics;
39  
40  import static java.lang.Boolean.parseBoolean;
41  import static java.lang.Double.parseDouble;
42  import static java.lang.Integer.parseInt;
43  import static java.lang.String.format;
44  
45  import ffx.algorithms.thermodynamics.OrthogonalSpaceTempering.Histogram;
46  
47  import javax.annotation.Nullable;
48  import java.io.BufferedReader;
49  import java.io.IOException;
50  import java.io.Reader;
51  import java.util.logging.Level;
52  import java.util.logging.Logger;
53  
54  /**
55   * Read in the Histogram.
56   *
57   * @author Michael J. Schnieders
58   * @since 1.0
59   */
60  public class HistogramReader extends BufferedReader {
61  
62    private static final Logger logger = Logger.getLogger(HistogramReader.class.getName());
63    /** Private reference to the Histogram instance, if any. */
64    private final Histogram histogram;
65  
66    private double temperature;
67    private double thetaMass;
68    private double thetaFriction;
69    private double biasMag;
70    private int biasCutoff;
71    private int countInterval;
72    private int lambdaBins;
73    private int dUdLBins;
74    private double mindUdL;
75    private double dUdLBinWidth;
76    private double[][] counts;
77    private boolean metaDynamics = false;
78  
79    public HistogramReader(Reader reader) {
80      this(null, reader);
81    }
82  
83    /**
84     * Constructor.
85     *
86     * @param histogram The Histogram instance.
87     * @param reader The Reader to use.
88     */
89    public HistogramReader(@Nullable Histogram histogram, Reader reader) {
90      super(reader);
91      this.histogram = histogram;
92    }
93  
94    public int getBiasCutoff() {
95      return biasCutoff;
96    }
97  
98    public double getBiasMag() {
99      return biasMag;
100   }
101 
102   public int getCountInterval() {
103     return countInterval;
104   }
105 
106   public double getdUdLBinWidth() {
107     return dUdLBinWidth;
108   }
109 
110   public int getLambdaBins() {
111     return lambdaBins;
112   }
113 
114   public double getTemperature() {
115     return temperature;
116   }
117 
118   public double getThetaFriction() {
119     return thetaFriction;
120   }
121 
122   public double getThetaMass() {
123     return thetaMass;
124   }
125 
126   /** Read the histogram file. */
127   public void readHistogramFile() {
128     try {
129       temperature = parseDouble(readLine().split(" +")[1]);
130       thetaMass = parseDouble(readLine().split(" +")[1]);
131       thetaFriction = parseDouble(readLine().split(" +")[1]);
132       biasMag = parseDouble(readLine().split(" +")[1]);
133       biasCutoff = parseInt(readLine().split(" +")[1]);
134       countInterval = parseInt(readLine().split(" +")[1]);
135       lambdaBins = parseInt(readLine().split(" +")[1]);
136       dUdLBins = parseInt(readLine().split(" +")[1]);
137       mindUdL = parseDouble(readLine().split(" +")[1]);
138       dUdLBinWidth = parseDouble(readLine().split(" +")[1]);
139       counts = new double[lambdaBins][dUdLBins];
140       int i = 0;
141       String[] toks = readLine().split(" +");
142       if (toks.length == 2) {
143         // Read the flag for the metadynamics.
144         metaDynamics = parseBoolean(toks[1]);
145       } else {
146         // Read the first line of the histogram.
147         for (int j = 0; j < dUdLBins; j++) {
148           counts[i][j] = parseDouble(toks[j]);
149         }
150         i++;
151       }
152 
153       for (; i < lambdaBins; i++) {
154         String[] countToks = readLine().split(" +");
155         for (int j = 0; j < dUdLBins; j++) {
156           counts[i][j] = parseDouble(countToks[j]);
157         }
158       }
159 
160       if (histogram != null) {
161         histogram.dUdLBins = dUdLBins;
162         histogram.mindUdL = mindUdL;
163         // Allocate memory for the recursion kernel.
164         histogram.allocateRecursionKernel();
165         // Set the recursion kernel.
166         histogram.setRecursionKernel(counts);
167         histogram.setMetaDynamics(metaDynamics);
168       }
169 
170     } catch (Exception e) {
171       String message = " Invalid OST Histogram file.";
172       logger.log(Level.SEVERE, message, e);
173     }
174     try {
175       close();
176     } catch (IOException ioe) {
177       String histoName = histogram == null ? "unknown file" : histogram.toString();
178       logger.warning(format(" Failed to close histogram reader for %s", histoName));
179     }
180   }
181 
182 }