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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import matplotlib.pyplot as plt
import numpy as np
import torch
from ase import Atoms
from ase.calculators.calculator import Calculator, all_changes
from ase.geometry.analysis import Analysis
from ase.optimize import FIRE
from matsciml.datasets.trajectory_lmdb import data_list_collater
from matsciml.datasets.transforms import (
    FrameAveraging,
    PeriodicPropertiesTransform,
    PointCloudToGraphTransform,
)
from matsciml.preprocessing.atoms_to_graphs import AtomsToGraphs

a2g = AtomsToGraphs(
    max_neigh=200,
    radius=6,
    r_energy=False,
    r_forces=False,
    r_distances=False,
    r_edges=True,
    r_fixed=True,
)
f_avg = FrameAveraging(frame_averaging="3D", fa_method="stochastic")
PBCTransform = PeriodicPropertiesTransform(cutoff_radius=6.0, adaptive_cutoff=True)
GTransform = PointCloudToGraphTransform(
    "dgl",
    node_keys=["pos", "atomic_numbers"],
)


def convAtomstoBatchmace(atoms):
    data_obj = a2g.convert(atoms)
    Reformatted_batch = {
        "cell": data_obj.cell,
        "natoms": torch.Tensor([data_obj.natoms]).unsqueeze(0),
        "edge_index": [data_obj.edge_index.shape],
        "cell_offsets": data_obj.cell_offsets,
        "atomic_numbers": data_obj.atomic_numbers,
        "pos": data_obj.pos,
        "y": None,
        "force": None,
        "fixed": [data_obj.fixed],
        "tags": None,
        "sid": None,
        "fid": None,
        "dataset": "S2EFDataset",
        "graph": data_list_collater([data_obj]),
    }

    Reformatted_batch = PBCTransform(Reformatted_batch)

    return Reformatted_batch


def convAtomstoBatchtensornet(atoms):
    data_obj = a2g.convert(atoms)
    Reformatted_batch = {
        "cell": data_obj.cell,
        "natoms": torch.Tensor([data_obj.natoms]).unsqueeze(0),
        "edge_index": [data_obj.edge_index.shape],
        "cell_offsets": data_obj.cell_offsets,
        "atomic_numbers": data_obj.atomic_numbers,
        "pos": data_obj.pos,
        "y": None,
        "force": None,
        "fixed": [data_obj.fixed],
        "tags": None,
        "sid": None,
        "fid": None,
        "dataset": "S2EFDataset",
        # 'graph' : data_list_collater([data_obj]),
    }
    Reformatted_batch = PBCTransform(Reformatted_batch)
    Reformatted_batch = GTransform(Reformatted_batch)
    return Reformatted_batch


def convAtomstoBatchfaenet(atoms):
    data_obj = a2g.convert(atoms)
    Reformatted_batch = {
        "cell": data_obj.cell,
        "natoms": torch.Tensor([data_obj.natoms]).unsqueeze(0),
        "edge_index": [data_obj.edge_index.shape],
        "cell_offsets": data_obj.cell_offsets,
        "atomic_numbers": data_obj.atomic_numbers,
        "pos": data_obj.pos,
        "y": None,
        "force": None,
        "fixed": [data_obj.fixed],
        "tags": None,
        "sid": None,
        "fid": None,
        "dataset": "S2EFDataset",
        "graph": data_list_collater([data_obj]),
    }
    Reformatted_batch = f_avg(Reformatted_batch)
    Reformatted_batch = PBCTransform(Reformatted_batch)

    return Reformatted_batch


def convBatchtoAtoms(batch):
    # data_obj=a2g.convert(atoms)
    curr_atoms = Atoms(
        positions=batch["graph"].pos,
        cell=batch["cell"][0],
        numbers=batch["graph"].atomic_numbers,
        pbc=True,
    )  # True or false

    return curr_atoms


def minimize_structure(atoms, fmax=0.05, steps=50):
    """

    Perform energy minimization on the given ASE Atoms object using the FIRE optimizer.



    Parameters:

    atoms (ase.Atoms): The Atoms object to be minimized.

    fmax (float): The maximum force tolerance for the optimization (default: 0.01 eV/Å).

    steps (int): The maximum number of optimization steps (default: 1000).



    Returns:

    ase.Atoms: The minimized Atoms object.

    """
    dyn = FIRE(atoms, trajectory=None)
    dyn.run(fmax=fmax, steps=steps)
    return atoms


def to(data, device):
    """Simple utility function to move things to correct device"""
    new_dict = {}
    for key, value in data.items():
        if hasattr(value, "to"):
            new_dict[key] = value.to(device)
        else:
            new_dict[key] = value
    return new_dict


def min_height(cell_matrix):
    """

    Calculate the perpendicular heights in three directions given a 3x3 cell matrix.

    """
    a, b, c = cell_matrix[:, 0], cell_matrix[:, 1], cell_matrix[:, 2]
    volume = abs(np.dot(a, np.cross(b, c)))
    # Calculate the cross products
    a_cross_b, b_cross_c, c_cross_a = (
        np.linalg.norm(np.cross(a, b)),
        np.linalg.norm(np.cross(b, c)),
        np.linalg.norm(np.cross(c, a)),
    )
    # Calculate the perpendicular heights
    height_a, height_b, height_c = (
        abs(volume / a_cross_b),
        abs(volume / b_cross_c),
        abs(volume / c_cross_a),
    )
    return min(height_a, height_b, height_c)


def perturb_config(atoms, displacement_std=0.01):
    # Create a new Atoms object with the perturbed positions
    positions = atoms.get_positions()
    displacements = np.random.normal(scale=displacement_std, size=positions.shape)
    new_positions = positions + displacements
    new_perturbed_atoms = atoms.copy()
    new_perturbed_atoms.set_positions(new_positions)
    return new_perturbed_atoms


def plot_pair_rdfs(Pair_rdfs, shift=0):
    counter = 0
    plt.figure()
    for key in Pair_rdfs.keys():
        plt.plot(Pair_rdfs[key][0], Pair_rdfs[key][1] + shift * counter, label=key)
        counter += 1
    plt.legend(loc=(1.2, 0))
    plt.xlabel("r (Angstrom)")
    plt.ylabel("g(r)")
    plt.show()


def replicate_system(atoms, replicate_factors):
    """

    Replicates the given ASE Atoms object according to the specified replication factors.

    """
    nx, ny, nz = replicate_factors
    original_cell = atoms.get_cell()
    original_positions = atoms.get_positions() #@ original_cell  # Scaled or Unscaled ?
    original_numbers = atoms.get_atomic_numbers()
    x_cell, y_cell, z_cell = original_cell[0], original_cell[1], original_cell[2]
    new_numbers = []
    for i in range(nx):
        for j in range(ny):
            for k in range(nz):
                new_numbers += [original_numbers]
    pos_after_x = np.concatenate([original_positions + i * x_cell for i in range(nx)])
    pos_after_y = np.concatenate([pos_after_x + i * y_cell for i in range(ny)])
    pos_after_z = np.concatenate([pos_after_y + i * z_cell for i in range(nz)])
    new_cell = [nx * original_cell[0], ny * original_cell[1], nz * original_cell[2]]
    new_atoms = Atoms(
        numbers=np.concatenate(new_numbers),
        positions=pos_after_z,
        cell=new_cell,
        pbc=atoms.get_pbc(),
    )
    return new_atoms


def write_xyz(Filepath, atoms):
    """Writes ovito xyz file"""
    R = atoms.get_position()
    species = atoms.get_atomic_numbers()
    cell = atoms.get_cell()
    f = open(Filepath, "w")
    f.write(str(R.shape[0]) + "\n")
    flat_cell = cell.flatten()
    f.write(
        f'Lattice="{flat_cell[0]} {flat_cell[1]} {flat_cell[2]} {flat_cell[3]} {flat_cell[4]} {flat_cell[5]} {flat_cell[6]} {flat_cell[7]} {flat_cell[8]}" Properties=species:S:1:pos:R:3 Time=0.0'
    )
    for i in range(R.shape[0]):
        f.write(
            "\n"
            + str(species[i])
            + "\t"
            + str(R[i, 0])
            + "\t"
            + str(R[i, 1])
            + "\t"
            + str(R[i, 2])
        )


def symmetricize_replicate(curr_atoms, max_atoms, box_lengths):
    replication = [1, 1, 1]
    atom_count = curr_atoms
    lengths = box_lengths
    while atom_count < (max_atoms // 2):
        direction = np.argmin(box_lengths)
        replication[direction] += 1
        lengths[direction] = box_lengths[direction] * replication[direction]
        atom_count = curr_atoms * replication[0] * replication[1] * replication[2]
    return replication, atom_count


def get_pairs(atoms):
    Atom_types = np.unique(atoms.get_chemical_symbols())
    Pairs = []
    for i in range(len(Atom_types)):
        for j in range(i, len(Atom_types)):
            Pairs += [[Atom_types[i], Atom_types[j]]]
    return Pairs


def getfirstpeaklength(r, rdf, r_max=6.0):
    bin_size = (r[-1] - r[0]) / len(r)
    cut_index = int(r_max / bin_size)
    cut_index = min(cut_index, len(r))
    Peak_index = np.argmax(rdf[:cut_index])
    # Returns : Peak index and Bond length
    return Peak_index, r[Peak_index]


def get_partial_rdfs(Traj, r_max=6.0, dr=0.01):
    rmax = min(r_max, min_height(Traj[0].get_cell()) / 2.7)
    analysis = Analysis(Traj)
    dr = dr
    nbins = int(rmax / dr)
    pairs_list = get_pairs(Traj[0])
    Pair_rdfs = dict()
    for pair in pairs_list:
        rdf = analysis.get_rdf(
            rmax=rmax, nbins=nbins, imageIdx=None, elements=pair, return_dists=True
        )
        x = rdf[0][1]
        y = np.array([rdf[k][0] for k in range(len(rdf))]).mean(axis=0)
        Pair_rdfs["-".join(pair)] = [x, y]
    return Pair_rdfs


def get_partial_rdfs_smoothened(

    inp_atoms, perturb=10, noise_std=0.01, max_atoms=300, r_max=6.0, dr=0.01

):
    atoms = inp_atoms.copy()
    replication_factors, _ = symmetricize_replicate(
        len(atoms),
        max_atoms=max_atoms,
        box_lengths=atoms.get_cell_lengths_and_angles()[:3],
    )
    atoms = replicate_system(atoms, replication_factors)
    Traj = [perturb_config(atoms, noise_std) for k in range(perturb)]
    return get_partial_rdfs(Traj, r_max=r_max, dr=dr)


def get_bond_lengths_noise(

    inp_atoms, perturb=10, noise_std=0.01, max_atoms=300, r_max=6.0, dr=0.01

):
    Pair_rdfs = get_partial_rdfs_smoothened(
        inp_atoms,
        perturb=perturb,
        noise_std=noise_std,
        max_atoms=max_atoms,
        r_max=r_max,
        dr=dr,
    )
    Bond_lengths = dict()
    for key in Pair_rdfs:
        r, rdf = Pair_rdfs[key]
        Bond_lengths[key] = getfirstpeaklength(r, rdf)[1]
    return Bond_lengths, Pair_rdfs


def get_bond_lengths_TrajAvg(Traj, r_max=6.0, dr=0.01):
    Pair_rdfs = get_partial_rdfs(Traj, r_max=r_max, dr=dr)
    Bond_lengths = dict()
    for key in Pair_rdfs:
        r, rdf = Pair_rdfs[key]
        Bond_lengths[key] = getfirstpeaklength(r, rdf)[1]
    return Bond_lengths, Pair_rdfs


def get_initial_rdf(

    inp_atoms,

    perturb=10,

    noise_std=0.01,

    max_atoms=300,

    replicate=False,

    Structid=0,

    r_max=6.0,

    dr=0.01,

):
    atoms = inp_atoms.copy()
    # write_xyz(f"StabilityXYZData2/{Structid}.xyz",atoms.get_positions(),atoms.get_chemical_symbols(),atoms.get_cell())
    if replicate:
        replication_factors, size = symmetricize_replicate(
            len(atoms),
            max_atoms=max_atoms,
            box_lengths=atoms.get_cell_lengths_and_angles()[:3],
        )
        atoms = replicate_system(atoms, replication_factors)
    rmax = min(r_max, min_height(atoms.get_cell()) / 2.7)
    # atoms.rattle(0.01)
    analysis = Analysis([perturb_config(atoms, noise_std) for k in range(perturb)])
    # write_xyz(f"StabilityXYZDataReplicated2/{Structid}.xyz",atoms.get_positions(),atoms.get_chemical_symbols(),atoms.get_cell())
    dr = dr
    nbins = int(rmax / dr)
    rdf = analysis.get_rdf(
        rmax=rmax, nbins=nbins, imageIdx=None, elements=None, return_dists=True
    )
    x = rdf[0][1]
    y = np.array([rdf[k][0] for k in range(len(rdf))]).mean(axis=0)
    return x, y


def get_rdf(Traj, r_max=6.0, dr=0.01):
    rmax = min(r_max, min_height(Traj[0].get_cell()) / 2.7)
    analysis = Analysis(Traj)
    dr = dr
    nbins = int(rmax / dr)
    rdf = analysis.get_rdf(
        rmax=rmax, nbins=nbins, imageIdx=None, elements=None, return_dists=True
    )
    x = rdf[0][1]
    y = np.array([rdf[k][0] for k in range(len(rdf))]).mean(axis=0)
    return x, y


## MAce Calculator
class ASEcalculator(Calculator):
    """Simulation ASE Calculator"""

    implemented_properties = ["energy", "forces", "stress"]

    def __init__(self, model, model_name, **kwargs):
        Calculator.__init__(self, **kwargs)
        self.results = {}

        self.model = model
        if model_name == "mace":
            self.convAtomstoBatch = convAtomstoBatchmace

        elif model_name == "faenet":
            self.convAtomstoBatch = convAtomstoBatchfaenet

        elif model_name == "tensornet":
            self.convAtomstoBatch = convAtomstoBatchtensornet

        else:
            print("Wrong Model Name")

    # pylint: disable=dangerous-default-value
    def calculate(self, atoms=None, properties=None, system_changes=all_changes):
        """

        Calculate properties.

        :param atoms: ase.Atoms object

        :param properties: [str], properties to be computed, used by ASE internally

        :param system_changes: [str], system changes since last calculation, used by ASE internally

        :return:

        """
        # call to base-class to set atoms attribute
        Calculator.calculate(self, atoms)

        # prepare data
        batch = self.convAtomstoBatch(atoms)

        # predict + extract data
        out = self.model.predict(to(batch,self.model.device))
        #out = self.model.forward(batch)
        energy = out['energy'].detach().cpu().item()
        forces = out["force"].detach().cpu().numpy()
        stress = out["stress"].squeeze(0).detach().cpu().numpy()
        # store results
        E = energy
        stress = np.array(
            [
                stress[0, 0],
                stress[1, 1],
                stress[2, 2],
                stress[1, 2],
                stress[0, 2],
                stress[0, 1],
            ]
        )
        self.results = {
            "energy": E,
            # force has units eng / len:
            "forces": forces,
            "stress": stress,
        }