1 // ******************************************************************************
2 //
3 // Title: Force Field X.
4 // Description: Force Field X - Software for Molecular Biophysics.
5 // Copyright: Copyright (c) Michael J. Schnieders 2001-2025.
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.xray.scatter;
39
40 import java.util.Arrays;
41 import java.util.HashMap;
42 import java.util.logging.Logger;
43
44 import static java.lang.String.format;
45
46 /**
47 * The XRayScatteringParameters class is a record used to store and access X-ray scattering parameters
48 * for specific atoms or elements. It provides methods to retrieve such parameters based
49 * on their atomic properties or a unique key. Instances of this class consist of an atom's
50 * name, a description, and its associated X-ray form factor data.
51 *
52 * @author Michael J. Schnieders
53 * @since 1.0
54 */
55 public record XRayScatteringParameters(String name,
56 int atomicNumber,
57 int charge,
58 int numberOfGaussians,
59 double[][] formFactor) {
60
61 private static final Logger logger = Logger.getLogger(XRayScatteringParameters.class.getName());
62
63 private static final HashMap<String, XRayScatteringParameters> formFactorsSuCoppens = new HashMap<>();
64 private static final HashMap<String, XRayScatteringParameters> formFactorsCCTBX = new HashMap<>();
65
66 /**
67 * Returns a string representation of the X-Ray scattering parameters.
68 * The string includes the atom name, description, the first form factor element,
69 * and the array of additional form factor parameters.
70 *
71 * @return A formatted string representation of the object.
72 */
73 @Override
74 public String toString() {
75 return format(" %s %d Charge: %d Gaussian Amplitudes (%d): %s",
76 name, atomicNumber, charge, numberOfGaussians, Arrays.toString(formFactor[1]));
77 }
78
79 /**
80 * Retrieves the scattering parameters based on the provided atomic parameters and preference for using 3 Gaussian parameters.
81 *
82 * @param charge the formal charge of the atom.
83 * @param atomicNumber the atomic number of the element.
84 * @param use3G a boolean flag indicating whether to prefer 3 Gaussian parameters if available.
85 * @return The X-ray scattering parameters, or null if no matching data is found.
86 */
87 public static XRayScatteringParameters getFormFactor(int atomicNumber, int charge, boolean use3G) {
88 XRayScatteringParameters parameters = null;
89 if (use3G) {
90 parameters = getFormFactorCCTBX(atomicNumber, charge);
91 }
92 // Su and Coppens parameters if use3G is false or CCTBX parameters were not found.
93 if (parameters == null) {
94 parameters = getFormFactorSuCoppens(atomicNumber, charge);
95 }
96 // No scattering parameters were found.
97 if (parameters == null) {
98 String message = format(" Parameters not found for %d with charge %d", atomicNumber, charge);
99 logger.severe(message);
100 }
101
102 return parameters;
103 }
104
105 /**
106 * Get the Su and Coppens scattering parameters.
107 *
108 * @param atomicNumber The atomic number.
109 * @param charge The formal charge of the atom.
110 * @return The scattering parameters.
111 */
112 public static XRayScatteringParameters getFormFactorSuCoppens(int atomicNumber, int charge) {
113 String key = Integer.toString(atomicNumber);
114 String keyWithCharge = key + "_" + charge;
115 if (formFactorsSuCoppens.containsKey(keyWithCharge)) {
116 return formFactorsSuCoppens.get(keyWithCharge);
117 } else return formFactorsSuCoppens.getOrDefault(key, null);
118 }
119
120 /**
121 * Get the CCTBX 3 Gaussian scattering parameters.
122 *
123 * @param atomicNumber The atomic number.
124 * @param charge The formal charge of the atom.
125 * @return The scattering parameters.
126 */
127 public static XRayScatteringParameters getFormFactorCCTBX(int atomicNumber, int charge) {
128 String key = Integer.toString(atomicNumber);
129 String keyWithCharge = key + "_" + charge;
130 if (formFactorsCCTBX.containsKey(keyWithCharge)) {
131 return formFactorsCCTBX.get(keyWithCharge);
132 } else return formFactorsCCTBX.getOrDefault(key, null);
133 }
134
135 static {
136 // Load Su & Coppens scattering parameters.
137 String[] atoms = XrayParametersSuCoppens.atoms;
138 String[] atomsi = XrayParametersSuCoppens.atomsi;
139 double[][][] ffactors = XrayParametersSuCoppens.ffactors;
140 for (int i = 0; i < atoms.length; i++) {
141 String environment = atomsi[i];
142 String[] descriptions = environment.split("_");
143 int atomicNumber = Integer.parseInt(descriptions[0]);
144 int charge = 0;
145 if (descriptions.length > 1) {
146 charge = Integer.parseInt(descriptions[1]);
147 }
148 int numberOfGaussians = ffactors[i][1].length;
149 XRayScatteringParameters factor = new XRayScatteringParameters(atoms[i],
150 atomicNumber, charge, numberOfGaussians, ffactors[i]);
151 formFactorsSuCoppens.put(atomsi[i], factor);
152 }
153
154 // Load CCTBX 3 Gaussian scattering parameters.
155 atoms = XrayParametersCCTBX.atoms;
156 atomsi = XrayParametersCCTBX.atomsi;
157 ffactors = XrayParametersCCTBX.ffactors;
158 for (int i = 0; i < atoms.length; i++) {
159 String environment = atomsi[i];
160 String[] descriptions = environment.split("_");
161 int atomicNumber = Integer.parseInt(descriptions[0]);
162 int charge = 0;
163 if (descriptions.length > 1) {
164 charge = Integer.parseInt(descriptions[1]);
165 }
166 int numberOfGaussians = ffactors[i][1].length;
167 XRayScatteringParameters factor = new XRayScatteringParameters(atoms[i],
168 atomicNumber, charge, numberOfGaussians, ffactors[i]);
169 formFactorsCCTBX.put(atomsi[i], factor);
170 }
171
172 }
173
174 }