1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//! For operating on frcmod files, which describe Amber force fields for small molecules.
use std::{
fs::File,
io::{self, ErrorKind, Read, Write},
path::Path,
};
use crate::md_params::{
AngleBendingParams, BondStretchingParams, DihedralParams, ForceFieldParamsVec, MassParams,
};
#[derive(Debug, PartialEq)]
enum Section {
Remark,
Mass,
Bond,
Angle,
Dihedral,
Improper,
Nonbond,
}
impl ForceFieldParamsVec {
/// From a string of a FRCMOD text file.
pub fn from_frcmod(text: &str) -> io::Result<Self> {
let mut result = Self::default();
let lines: Vec<&str> = text.lines().collect();
let mut section = Section::Remark;
for line in lines {
let line = line.trim();
if line.is_empty() {
continue;
}
match line {
"MASS" => {
section = Section::Mass;
continue;
}
"BOND" => {
section = Section::Bond;
continue;
}
"ANGLE" | "ANGL" => {
section = Section::Angle;
continue;
}
"DIHE" | "DIHEDRAL" => {
section = Section::Dihedral;
continue;
}
"IMPROPER" | "IMPR" => {
section = Section::Improper;
continue;
}
"NONBON" | "NONB" | "NONBOND" => {
section = Section::Nonbond;
continue;
}
_ => {}
}
match section {
Section::Remark => {
result.remarks.push(line.to_owned());
}
Section::Mass => {
result.mass.push(MassParams::from_line(line)?);
}
Section::Bond => {
result.bond.push(BondStretchingParams::from_line(line)?);
}
Section::Angle => {
result.angle.push(AngleBendingParams::from_line(line)?);
}
Section::Dihedral => {
result.dihedral.push(DihedralParams::from_line(line)?.0);
}
Section::Improper => {
result.improper.push(DihedralParams::from_line(line)?.0);
}
Section::Nonbond => { /* skip or extend */ }
}
}
// for r in &result.dihedral {
// println!("Dihe: {:?}", r);
// }
// println!("\n\n\n");
// for r in &result.improper {
// println!("Imp: {:?}", r);
// }
Ok(result)
}
/// Write to file
pub fn save_frcmod(&self, path: &Path) -> io::Result<()> {
let mut f = File::create(path)?;
for r in &self.remarks {
writeln!(f, "{r}")?;
}
writeln!(f)?;
writeln!(f, "MASS")?;
for m in &self.mass {
if let Some(c) = &m.comment {
writeln!(f, "{} {:>10.4} ! {}", m.atom_type, m.mass, c)?;
} else {
writeln!(f, "{} {:>10.4}", m.atom_type, m.mass)?;
}
}
writeln!(f)?;
writeln!(f, "BOND")?;
for b in &self.bond {
if let Some(c) = &b.comment {
writeln!(
f,
"{}-{} {:>8.3} {:>8.3} {}",
b.atom_types.0, b.atom_types.1, b.k_b, b.r_0, c
)?;
} else {
writeln!(
f,
"{}-{} {:>8.3} {:>8.3}",
b.atom_types.0, b.atom_types.1, b.k_b, b.r_0
)?;
}
}
writeln!(f)?;
writeln!(f, "ANGLE")?;
for a in &self.angle {
if let Some(c) = &a.comment {
writeln!(
f,
"{}-{}-{} {:>8.3} {:>8.3} {}",
a.atom_types.0,
a.atom_types.1,
a.atom_types.2,
a.k,
a.theta_0.to_degrees(),
c
)?;
} else {
writeln!(
f,
"{}-{}-{} {:>8.3} {:>8.3}",
a.atom_types.0,
a.atom_types.1,
a.atom_types.2,
a.k,
a.theta_0.to_degrees()
)?;
}
}
writeln!(f)?;
writeln!(f, "DIHE")?;
for d in &self.dihedral {
let names = format!(
"{}-{}-{}-{}",
d.atom_types.0, d.atom_types.1, d.atom_types.2, d.atom_types.3
);
let mut line = format!(
"{} {:>3} {:>8.3} {:>8.3} {:>8.3}",
names,
d.divider,
d.barrier_height,
d.phase.to_degrees(),
d.periodicity
);
if let Some(n) = &d.comment {
line.push_str(&format!(" {n}"));
}
writeln!(f, "{line}")?;
}
writeln!(f)?;
writeln!(f, "IMPROPER")?;
for imp in &self.improper {
let names = format!(
"{}-{}-{}-{}",
imp.atom_types.0, imp.atom_types.1, imp.atom_types.2, imp.atom_types.3
);
if let Some(c) = &imp.comment {
writeln!(
f,
"{} {:>8.3} {:>8.3} {:>8.3} {}",
names,
imp.barrier_height,
imp.phase.to_degrees(),
imp.periodicity,
c
)?;
} else {
writeln!(
f,
"{} {:>8.3} {:>8.3} {:>8.3}",
names,
imp.barrier_height,
imp.phase.to_degrees(),
imp.periodicity
)?;
}
}
writeln!(f)?;
// todo: Placeholder. A/R.
writeln!(f, "NONBON")?;
Ok(())
}
/// todo: Sort out the syntax for loading from different sources.
pub fn load_frcmod(path: &Path) -> io::Result<Self> {
let mut file = File::open(path)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
let data_str: String = String::from_utf8(buffer)
.map_err(|_| io::Error::new(ErrorKind::InvalidData, "Invalid UTF8"))?;
Self::from_frcmod(&data_str)
}
}