-
Notifications
You must be signed in to change notification settings - Fork 10
/
fit.jl
289 lines (254 loc) · 10 KB
/
fit.jl
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# ------------------------------------------
# ACE Fitting
import ACE1
import ACEpotentials
import ACEfit
import ExtXYZ
using Base
#export fit_params, fit_ace, make_ace_db, db_params, fit_ace_db, save_fit
"""
`fit_ace(params::Dict) -> IP, lsqinfo`
Function to set up and fit the least-squares
problem of "atoms' positions" -> "energy, forces, virials". Takes in a
dictionary with all the parameters. See `?fit_params` for details.
"""
function fit_ace(params::Dict)
basis = [ACEpotentials.generate_basis(basis_params) for (basis_name, basis_params) in params["basis"]]
basis = JuLIP.MLIPs.IPSuperBasis(basis);
Vref = OneBody(Dict(Symbol(k) => v for (k,v) in params["e0"]))
energy_key = params["data"]["energy_key"]
force_key = params["data"]["force_key"]
virial_key = params["data"]["virial_key"]
weight_key = params["data"]["weight_key"]
weights = params["weights"]
dataset = JuLIP.read_extxyz(params["data"]["fname"])
data = AtomsData[]
for atoms in dataset
d = AtomsData(atoms; energy_key=energy_key, force_key=force_key, virial_key=virial_key, weight_key=weight_key, weights=weights, v_ref=Vref)
push!(data, d)
end
assess_dataset(data)
# create solver
solver = eval(Meta.parse("ACEfit." * uppercase(params["solver"]["type"])))
solver_kwargs = Dict(Symbol(k)=>v for (k,v) in params["solver"] if k!="type")
solver = solver(;solver_kwargs...)
if isnothing(params["P"])
P = nothing
else
P = ACEpotentials.generate_regularizer(basis, params["P"])
end
fit = ACEfit.linear_fit(data, basis, solver; P)
C = fit["C"]
IP = JuLIP.MLIPs.combine(basis, C)
(Vref != nothing) && (IP = JuLIP.MLIPs.SumIP(Vref, IP))
errors = linear_errors(data, IP)
results = Dict{String,Any}("IP" => IP, "errors" => errors)
save_fit(params["ACE_fname"], IP, Dict("errors" => errors, "params" => params))
if haskey(fit, "committee")
IP_com = ACE1.committee_potential(basis, C, fit["committee"])
(Vref != nothing) && (IP_com = JuLIP.MLIPs.SumIP(Vref, IP_com))
results["IP_com"] = IP_com
if !(params["ACE_fname"] == "") && !isnothing(params["ACE_fname"])
save_fit(chop(params["ACE_fname"],tail=5)*"-committee.json", IP_com, Dict("errors" => errors, "params" => params))
end
end
return results
end
"""
`fit_params(; kwargs...)`
Returns a dictionary containing all of the
parameters needed to make an ACE potential. All parameters are passed
as keyword argumts.
### Parameters
* `data` : data parameters, see `?data_params` for details (mandatory)
* `basis` : dictionary containing dictionaries that specify the basis used in fitting.
For example
```julia
basis = Dict(
"pair_short" => Dict( "type" => "pair", ...),
"pair_long" => Dict("type" => "pair", ...),
"manybody" => Dict("type" => "ace", ...),
"nospecies" => Dict("type" => "ace", species = ["X",], ...)
```
keys of `basis` are ignored, so that multiple basis with different specifications
(e.g. smaller and larger cutoffs) can be combined. See `?basis_params` for more detail.
* `solver` : dictionary containing parameters that specify the solver for
least squares problem (mandatory). See `?solver_params`.
* `e0` : `Dict{String, Float}` containing reference values for isolated atoms'
energies (mandatory).
* `weights` : dictionary of `Dict("config_type" => Dict("E" => Float, "F => Float))``
entries specifying fitting weights. "default" is set to `1.0` for all of "E", "F",
and "V" weights.
* `P` : regularizer parameters (optional), see `?regularizer_params`.
* `ACE_fname = "ACE_fit.json"` : filename to save ACE to. Potential & info
do not get saved if `ACE_fname` isnothing() or is set to `""`. Files already _parse_entry
are renamed and not overwritten.
* `LSQ_DB_fname_stem = ""` : stem to save LsqDB to. Doesn't get saved if set to an empty
string (""). If the file is already present, but `fit_from_LSQ_DB` is set to false,
the old database is renamed, a new one constructed and saved under the given name.
* `fit_from_LSQ_DB = false`: whether to fit from a least squares database specified with
`LSQ_DB_fname_stem`. If `LSQ_DB_fname_stem * "_kron.h5"` file is not present, LsqDB is
constructed from scratch and saved.
"""
function fit_params(;
data = nothing,
basis = nothing,
solver = nothing,
e0 = nothing,
weights = Dict("default"=>Dict("E"=>1.0, "F"=>1.0, "V"=>1.0)),
P = nothing,
ACE_fname = "ACE_fit.json",
LSQ_DB_fname_stem = "",
fit_from_LSQ_DB = false)
@assert !isnothing(data) "`data` is mandatory"
@assert !isnothing(basis) "`basis` is mandatory"
@assert !isnothing(solver) "`solver` is mandatory"
@assert !isnothing(e0) "`e0` is mandatory"
return Dict(
"data" => data,
"basis" => basis,
"solver" => solver,
"e0" => e0,
"weights" => weights,
"P" => P,
"ACE_fname" => ACE_fname,
"LSQ_DB_fname_stem" => LSQ_DB_fname_stem,
"fit_from_LSQ_DB" => fit_from_LSQ_DB)
end
"""
`save_fit(fname, IP, lsqinfo)`
Saves Dict("IP" => IP, "info" => lsqinfo) to fname.
If `fname` is already present, it is renamed and dictionary saved to `fname`.
"""
function save_fit(fname, IP, lsqinfo)
# ENH: save to yace option
if fname == "" || isnothing(fname)
return
end
if isfile(fname)
stem = replace(fname, ".json" => "")
fnew = stem * "." * String(rand('a':'z', 5)) * ".json"
@warn("The file $fname already exists. It will be renamed to $fnew to avoid overwriting.")
mv(fname, fnew)
end
@info("Saving ace fit to $(fname)")
save_dict(fname, Dict("IP" => write_dict(IP), "info" => lsqinfo))
end
#"""
#`fit_ace_db(params::Dict)` : fits LsqDB with `params["LSQ_DB_fname_stem"], which must be already present.
#`params["fit_from_LSQ_DB"]` must be set to true. See `?fit_params` for `params` specification,
#of which `data` and `basis` aren't needed (are ignored).
#"""
#function fit_ace_db(params::Dict)
# @assert params["fit_from_LSQ_DB"]
# db = LsqDB(params["LSQ_DB_fname_stem"])
# IP, lsqinfo = fit_ace_db(db, params)
# return IP, lsqinfo
#end
#"""
#`fit_ace_db(db::IPFitting.LsqDB, params::Dict)` : fits the given LsqDB. See `?fit_params` for
#`params` specification, of which `data` and `basis` aren't needed (are ignored).
#"""
#function fit_ace_db(db::IPFitting.LsqDB, params::Dict)
# solver = ACEpotentials.generate_solver(params["solver"])
#
# if !isnothing(params["P"])
# solver["P"] = ACEpotentials.generate_regularizer(db.basis, params["P"])
# end
#
# if typeof(params["e0"]) == Dict{Any, Any}
# # sometimes gets read in (from yaml?) as Dict{Any, Any}
# # which gives StackOverflowError somewhere in OneBody
# params["e0"] = convert(Dict{String, Any}, params["e0"])
# end
#
# Vref = OneBody(params["e0"])
# weights = params["weights"]
#
# vals, solve_time, bytes, _, _ = @timed IPFitting.Lsq.lsqfit(
# db,
# Vref=Vref,
# solver=solver,
# weights=weights,
# error_table=true
# );
#
# IP = vals[1]
# lsqinfo = vals[2]
# solve_time = canonicalize(Dates.CompoundPeriod(Second(trunc(Int, solve_time))))
# bytes = Base.format_bytes(bytes)
# lsqinfo["solve_time"] = solve_time
# lsqinfo["solve_mem"] = bytes
# lsqinfo["fit_params"] = params
#
# save_fit(params["ACE_fname"], IP, lsqinfo)
#
# @info("Fitting errors")
# rmse_table(lsqinfo["errors"])
# @info("LsqDB solve time: $(solve_time)")
# @info("LsqDB memory: $(bytes)")
#
# return IP, lsqinfo
#end
"""
`make_ace_db(params::Dict)` -> LsqDB
Makes a LsqDB from given parameters' dictionary. For `params` see
`?db_params`; parameters from `fit_params` also work, except
unnecessary entries will be ignored. Returns `IPFitting.LsqDB`
"""
function make_ace_db(params::Dict)
data = ACEpotentials.read_data(params["data"])
basis = [ACEpotentials.generate_basis(basis_params) for (basis_name, basis_params) in params["basis"]]
basis = JuLIP.MLIPs.IPSuperBasis(basis);
db = LsqDB(params["LSQ_DB_fname_stem"], basis, data)
return db
end
"""
db_params(; kwargs...)` : returns a dictionary containing all of the
parameters needed for making a LsqDB. All parameters are passed
as keyword argumts.
### Parameters
* `data` : data parameters, see `?data_params` for details (mandatory)
* `basis` : dictionary containing dictionaries that specify the basis used in fitting.
For example
```julia
basis = Dict(
"pair_short" => Dict( "type" => "pair", ...),
"pair_long" => Dict("type" => "pair", ...),
"manybody" => Dict("type" => "ace", ...),
"nospecies" => Dict("type" => "ace", species = ["X",], ...)
```
keys of `basis` are ignored, so that multiple basis with different specifications
(e.g. smaller and larger cutoffs) can be combined. See `?basis_params` for more detail.
* `LSQ_DB_fname_stem = ""` : stem to save LsqDB to. Doesn't get saved if set to an empty
string (""). If `LSQ_DB_fname_stem * "_kron.h5"` file is not present it gets renamed,
a new LsqDB is constructed and saved.
"""
function db_params(;
data = nothing,
basis = nothing,
LSQ_DB_fname_stem = "")
@assert !isnothing(data) "`data` is mandatory"
@assert !isnothing(basis) "`basis` is mandatory"
return Dict(
"data" => data,
"basis" => basis,
"LSQ_DB_fname_stem" => LSQ_DB_fname_stem)
end
function _decide_how_to_get_db(params::Dict)
if params["LSQ_DB_fname_stem"] == ""
return make_ace_db(params)
else
db_fname = params["LSQ_DB_fname_stem"] * "_kron.h5"
if ~isfile(db_fname)
return make_ace_db(params)
else
if params["fit_from_LSQ_DB"]
return LsqDB(params["LSQ_DB_fname_stem"])
else
@warn("Found $(db_fname), but wasn't asked to fit it. Making a new one and saving to $db_fname).")
return make_ace_db(params)
end
end
end
end