File size: 27,440 Bytes
f614769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
import csv
import os
import re
import sys
import uuid
from datetime import datetime
from typing import List, Tuple
from warnings import filterwarnings

import h5py
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import torch
from ase import io
from ase.atoms import Atoms
from ase.geometry.analysis import Analysis
from joblib import Parallel, delayed
from sklearn.metrics import r2_score
from tqdm import tqdm

# Suppress warnings
filterwarnings('ignore')


# Function to find missing CSV files
def find_missing_csv_files_v8(root_folder, model_name, results_folder):
    """
    Combines all Data.csv files from .cif subdirectories under the given root folder.
    Generates a combined CSV and reports missing or unreadable files.

    Args:
        root_folder (str): The root directory containing subdirectories to scan.
        model_name (str): The name to use for the output files.

    Returns:
        Tuple: Combined DataFrame, missing CSV directories, and unreadable CSV directories.
    """
    df_list = []
    missing_csv_dirs = []  # Directories where Data.csv is missing
    unreadable_csv_dirs = []  # Directories with unreadable Data.csv
    successfully_read_csvs = []  # Paths of CSVs successfully read
    no_cif_folders = []  # Folders without .cif subdirectories
    hidden_folders = []  # Hidden folders
    all_cif_paths = []  # Track all .cif folders found

    for subdir in os.listdir(root_folder):
        subdir_path = os.path.join(root_folder, subdir)
        subdir_path = os.path.abspath(subdir_path)

        if subdir.startswith('.'):
            hidden_folders.append(subdir_path)
            continue

        if os.path.isdir(subdir_path):
            version_0_path = os.path.join(subdir_path, 'version_0')

            if os.path.isdir(version_0_path):
                cif_folders = [
                    f for f in os.listdir(version_0_path)
                    if f.endswith('.cif') and os.path.isdir(os.path.join(version_0_path, f))
                ]

                if cif_folders:
                    for cif_folder in cif_folders:
                        cif_folder_path = os.path.join(version_0_path, cif_folder)
                        all_cif_paths.append(cif_folder_path)

                        csv_path = os.path.join(cif_folder_path, 'Data.csv')

                        if os.path.isfile(csv_path):
                            try:
                                df = pd.read_csv(csv_path)
                                successfully_read_csvs.append(csv_path)
                                df_list.append(df)
                            except Exception as e:
                                unreadable_csv_dirs.append(cif_folder_path)
                        else:
                            missing_csv_dirs.append(cif_folder_path)
                else:
                    no_cif_folders.append(version_0_path)
            else:
                no_cif_folders.append(subdir_path)

    print("\n=== Detailed Path Analysis ===")
    print(f"Total .cif folders found: {len(all_cif_paths)}")
    print(f"Successfully read Data.csv: {len(successfully_read_csvs)}")
    print(f"Missing Data.csv: {len(missing_csv_dirs)}")
    print(f"Unreadable Data.csv: {len(unreadable_csv_dirs)}")

    if df_list:
        combined_df = pd.concat(df_list, ignore_index=True)
        combined_df.to_csv(f"{results_folder}/{model_name}/{model_name}.csv", index=False)

        with open(f"{results_folder}/{model_name}/fraction_complete_{model_name}.txt", 'w') as file:
            file.write(f"{results_folder}/{model_name}\t Total .cif folders found: {len(all_cif_paths)}" +
                       f"\t\tSuccessfully read Data.csv: {len(successfully_read_csvs)}")

        return combined_df, missing_csv_dirs, unreadable_csv_dirs

    print("\nNo Data.csv files found in the specified folders.")
    return None, missing_csv_dirs, unreadable_csv_dirs

# Function for plotting parity plots
def plot_scatter(ax, mask, actual, predicted, parameter_name, color, marker, model_name, r2_dict):
    """
    Creates a scatter plot comparing actual vs predicted values with an optional mask.

    Args:
        ax (matplotlib.axes.Axes): The axis to plot on.
        mask (np.ndarray): Mask to filter data.
        actual (np.ndarray): Actual values.
        predicted (np.ndarray): Predicted values.
        parameter_name (str): Name of the parameter being plotted.
        color (str): Color of the points.
        marker (str): Marker style.
        model_name (str): Name of the model.
        r2_dict (dict): Dictionary to store R2 values.

    Returns:
        Tuple: R2 score and indices of removed points.
    """
    filtered_actual = actual[mask]
    filtered_predicted = predicted[mask]

    r2 = r2_score(filtered_predicted, filtered_actual) if len(filtered_actual) > 0 else np.nan
    r2_dict[parameter_name] = r2

    ax.scatter(filtered_actual, filtered_predicted, label=f'{parameter_name}', color=color, marker=marker)
    max_val = max(max(filtered_actual), max(filtered_predicted)) if len(filtered_actual) > 0 else 1
    ax.plot([0, max_val], [0, max_val], color='black', linestyle='--')

    return r2, np.where(mask == False)

# Class for handling file operations
class FileHandler:
    """
    Handles file searching and reading operations.
    """

    def __init__(self, root_folder: str, incoming_uuid: str):
        self.root_folder = root_folder
        self.uuid = incoming_uuid

    def find_xyz_files(self) -> List[Tuple[str, str]]:
        xyz_files = []
        log_files = []

        for subdir in os.listdir(self.root_folder):
            subdir_path = os.path.join(self.root_folder, subdir)
            if not os.path.isdir(subdir_path) or subdir.startswith("."):
                continue

            version_0_path = os.path.join(subdir_path, "version_0")
            if not os.path.isdir(version_0_path):
                continue

            for folder in os.listdir(version_0_path):
                folder_path = os.path.join(version_0_path, folder)
                if not os.path.isdir(folder_path) or not folder.endswith(".cif"):
                    continue

                for file in os.listdir(folder_path):
                    if file.endswith(".xyz") and not file.startswith("._"):
                        system_name = folder.replace(".cif", "").replace(" ", "_")
                        xyz_files.append((system_name, os.path.join(folder_path, file)))
                    if file.endswith(".log") and not file.startswith("._"):
                        system_name = folder.replace(".cif", "").replace(" ", "_")
                        log_files.append((system_name, os.path.join(folder_path, file)))

        return xyz_files, log_files

    def safe_read_xyz(self, file_path: str) -> List[Atoms]:
        """
        Safely reads an XYZ file.

        Args:
            file_path (str): Path to the XYZ file.

        Returns:
            List[Atoms]: List of atomic structures.
        """
        try:
            # with open(file_path, "rb") as f:
            #     content = f.read()
            # decoded_content = content.decode("utf-8", errors="ignore")
            # temp_file_name = f"./temp_file_dir/temporary_{self.uuid}.xyz"
            # with open(temp_file_name, "w") as temp_file:
            #     temp_file.write(decoded_content)
            atoms = io.read(file_path, index=":")
            # os.remove(temp_file_name) 
            return atoms
        except Exception as e:
            print(f"Error reading {file_path}: {e}")
            return []

    @staticmethod
    def read_log_for_temperature(log_path: str) -> List[Tuple[float, float, float, float, float]]:
        """
        Reads a log file to extract temperature and energy data.

        Args:
            log_path (str): Path to the log file.

        Returns:
            List[Tuple]: Extracted data.
        """
        data = []
        try:
            with open(log_path, 'r') as log_file:
                for line in log_file:
                    match = re.match(r"(\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(\d+\.\d+)", line)
                    if match:
                        data.append(tuple(map(float, match.groups())))
        except Exception as e:
            print(f"Error reading log file {log_path}: {e}")
        return data

class PropertyCalculator:
    """Class for performing property calculations on atomic structures."""

    @staticmethod
    def calculate_density(atoms: Atoms) -> float:
        """
        Calculate the density of an atomic structure in g/cm³.
        
        Args:
            atoms (Atoms): The atomic structure to calculate the density for.
        
        Returns:
            float: The density in g/cm³.
        """
        # Conversion constants
        amu_to_grams = 1.66053906660e-24  # Atomic mass unit to grams
        angstrom_to_cm = 1e-8  # Angstrom to centimeters

        # Calculate mass in grams
        mass_amu = atoms.get_masses().sum()
        mass_g = mass_amu * amu_to_grams

        # Calculate volume in cm³
        volume_A3 = atoms.get_volume()
        volume_cm3 = volume_A3 * (angstrom_to_cm**3)

        # Return density
        return mass_g / volume_cm3
    
class XYZAnalyzer:
    """
    Class for analyzing and processing XYZ and log files related to atomic structures.
    Generates plots for various properties like density, temperature, and energy.

    Attributes:
        root_folder (str): The root folder containing the input files.
        output_dir (str): The directory to save output plots and analysis results.
        file_handler (FileHandler): An instance of the FileHandler class to read files.
        calculator (PropertyCalculator): An instance of the PropertyCalculator class to calculate properties.
    """

    def __init__(self, root_folder: str, output_dir: str):
        """
        Initializes the XYZAnalyzer with the root folder and output directory.
        
        Args:
            root_folder (str): Path to the root folder containing XYZ and log files.
            output_dir (str): Path to the directory where results and plots will be saved.
        """
        self.root_folder = root_folder
        self.output_dir = output_dir

        # Ensure the output directory exists
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        # Initialize FileHandler and PropertyCalculator
        self.file_handler = FileHandler(root_folder)
        self.calculator = PropertyCalculator()

    def process_file(self, system_name: str, xyz_file_path: str, log_file_path: str) -> Tuple[List[float], List[np.ndarray], List[Tuple[float, float, float, float, float]]]:
        """
        Process a single XYZ and log file to extract properties.
        
        Args:
            system_name (str): Name of the system being processed.
            xyz_file_path (str): Path to the XYZ file for the atomic structure.
            log_file_path (str): Path to the log file containing simulation data.
        
        Returns:
            Tuple:
                - List[float]: List of calculated densities.
                - List[np.ndarray]: List of lattice parameters for each structure.
                - List[Tuple[float, float, float, float, float]]: List of tuples containing time, temperature, total energy, potential energy, and kinetic energy.
        """
        densities = []
        lattice_params = []
        time_temp_data = []

        # Read the XYZ file and compute densities
        structures = self.file_handler.safe_read_xyz(xyz_file_path)
        # print(len(structures))

        for structure in structures:
            try:
                # Calculate density for each structure
                densities.append(self.calculator.calculate_density(structure))
                # Get lattice parameters
                lattice_params.append(structure.get_cell_lengths_and_angles())
            except Exception as e:
                print(f"Error processing structure in {system_name}: {e}")

        # Read the log file and extract time, temperature, and energy data
        time_temp_data = self.file_handler.read_log_for_temperature(log_file_path)

        return densities, lattice_params, time_temp_data

    def analyze_and_plot(self):
        """
        Analyze all XYZ and log files in the root folder and generate plots.
        
        - Generates plots for density evolution, temperature, and energies (Etot, Epot, Ekin) over time.
        - Skips systems where the temperature exceeds 3000K.
        """
        # Find XYZ and log files
        xyz_files, log_files = self.file_handler.find_xyz_files()
        print(f"Found {len(xyz_files)} XYZ files and {len(log_files)} log files.")

        if not xyz_files or not log_files:
            print("No XYZ or Log files found!")
            return

        # Create subplots for density, temperature, and energies
        fig, (ax_density, ax_temp, ax_etot, ax_epot, ax_ekin) = plt.subplots(5, 1, figsize=(12, 30))

        # Process each XYZ and log file pair
        for (system_name, xyz_file_path), (_, log_file_path) in zip(xyz_files, log_files):
            densities, _, time_temp_data = self.process_file(system_name, xyz_file_path, log_file_path)

            # Skip systems with temperature exceeding 3000K
            if any(temp_k > 3000 for _, temp_k, _, _, _ in time_temp_data):
                print(f"Skipping {system_name} due to temperature exceeding 3000K")
                continue

            # Extract time, temperature, and energy data
            time_steps, temperatures, etot_values, epot_values, ekin_values = zip(*time_temp_data)

            # Plot density evolution
            if densities:
                ax_density.plot(range(len(densities)), densities, label=system_name)

            # Plot temperature evolution
            ax_temp.plot(time_steps, temperatures, label=system_name)

            # Plot total energy (Etot) vs time
            ax_etot.plot(time_steps, etot_values, label=system_name)

            # Plot potential energy (Epot) vs time
            ax_epot.plot(time_steps, epot_values, label=system_name)

            # Plot kinetic energy (Ekin) vs time
            ax_ekin.plot(time_steps, ekin_values, label=system_name)

        # Set labels and titles for the subplots
        ax_density.set_xlabel("Timesteps")
        ax_density.set_ylabel("Density (g/cm³)")
        ax_density.set_title("Density Evolution")

        ax_temp.set_xlabel("Time (ps)")
        ax_temp.set_ylabel("Temperature (K)")
        ax_temp.set_title("Temperature Evolution")

        ax_etot.set_xlabel("Time (ps)")
        ax_etot.set_ylabel("Etot (eV)")
        ax_etot.set_title("Etot Evolution")

        ax_epot.set_xlabel("Time (ps)")
        ax_epot.set_ylabel("Epot (eV)")
        ax_epot.set_title("Epot Evolution")

        ax_ekin.set_xlabel("Time (ps)")
        ax_ekin.set_ylabel("Ekin (eV)")
        ax_ekin.set_title("Ekin Evolution")

        # Adjust layout and save the figure
        fig.tight_layout()
        fig.savefig(f"{self.output_dir}/energy_and_temperature.png", bbox_inches='tight')
        plt.show()
        
        
def symmetricize_replicate(curr_atoms, max_atoms, box_lengths):
    """
    Determine the replication factors needed to increase the number of atoms in the system
    to at least half of the target max_atoms while maintaining the symmetry of the cell.
    """
    replication = [1, 1, 1]  # Initial replication factors for each direction
    atom_count = curr_atoms
    lengths = box_lengths
    while atom_count < (max_atoms // 2):
        direction = np.argmin(box_lengths)  # Choose the smallest direction to replicate
        replication[direction] += 1  # Increase replication factor for that direction
        lengths[direction] = box_lengths[direction] * replication[direction]
        atom_count = curr_atoms * replication[0] * replication[1] * replication[2]
    return replication, atom_count

def replicate_system(atoms, replicate_factors):
    """
    Replicates the given ASE Atoms object according to the specified replication factors
    (nx, ny, nz).
    """
    nx, ny, nz = replicate_factors
    original_cell = atoms.get_cell()  # Original cell parameters
    original_positions = atoms.get_positions() #@ original_cell  # Convert positions to cartesian
    original_numbers = atoms.get_atomic_numbers()  # Atomic numbers of the atoms
    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]
                
    # Replicate positions along each direction
    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 dimensions after replication
    new_cell = [nx * original_cell[0], ny * original_cell[1], nz * original_cell[2]]
    
    # Create a new Atoms object with the replicated structure
    new_atoms = Atoms(
        numbers=np.concatenate(new_numbers),
        positions=pos_after_z,
        cell=new_cell,
        pbc=atoms.get_pbc(),
    )
    return new_atoms

def min_height(cell_matrix):
    """
    Calculate the perpendicular heights in three directions given a 3x3 cell matrix.
    The minimum height corresponds to the shortest height along the principal axes.
    """
    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 for each pair of axes
    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):
    """
    Perturb the atomic positions of the input ASE Atoms object by a Gaussian random displacement
    to simulate thermal fluctuations.
    """
    positions = atoms.get_positions()
    displacements = np.random.normal(scale=displacement_std, size=positions.shape)  # Gaussian noise
    new_positions = positions + displacements  # Apply perturbations to positions
    
    # Create a copy of the atoms object with the perturbed positions
    new_perturbed_atoms = atoms.copy()
    new_perturbed_atoms.set_positions(new_positions)
    return new_perturbed_atoms

def get_pairs(atoms):
    """
    Generate all unique pairs of atomic types present in the system.
    """
    Atom_types = np.unique(atoms.get_chemical_symbols())  # List unique atomic species
    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):
    """
    Find the position of the first peak in the RDF (Radial Distribution Function) within
    a specified range (r_max).
    """
    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])  # Index of the first peak
    return Peak_index, r[Peak_index]  # Return peak index and corresponding bond length

def get_partial_rdfs(Traj, r_max=6.0, dr=0.01):
    """
    Compute the partial radial distribution functions (RDFs) for all pairs of elements in the trajectory.
    """
    rmax = min(r_max, min_height(Traj[0].get_cell()) / 2.7)
    analysis = Analysis(Traj)  # Initialize RDF analysis
    dr = dr
    nbins = int(rmax / dr)  # Number of bins in the RDF calculation
    pairs_list = get_pairs(Traj[0])  # List of element pairs to compute RDFs
    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
):
    """
    Compute smoothened partial RDFs by perturbing the input atoms and replicating the system to a target size.
    """
    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)
    
    # Perturb the system multiple times to smoothen the RDF
    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
):
    """
    Get the bond lengths by computing the first peak in the RDF after perturbing the system.
    """
    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]  # Get bond length at the first peak
    return Bond_lengths, Pair_rdfs

def get_bond_lengths_TrajAvg(Traj, r_max=6.0, dr=0.01):
    """
    Compute the bond lengths from the average RDF of a trajectory.
    """
    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,
):
    """
    Compute the initial RDF for a given input atoms object with optional perturbations and replication.
    """
    atoms = inp_atoms.copy()
    
    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)  # Ensure r_max doesn't exceed the cell height
    analysis = Analysis([perturb_config(atoms, noise_std) for k in range(perturb)])
    
    dr = dr
    nbins = int(rmax / dr)  # Number of bins for RDF calculation
    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):
    """
    Compute the radial distribution function (RDF) for a given trajectory.
    """
    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


def process_file(file_handler, calculator, system_name: str, xyz_file_path: str, log_file_path: str):
    """Process a single XYZ and log file to extract properties."""
    densities = []
    lattice_params = []
    temperature = []
    rdf_error = []  # Will store RDF values for each window
    bond_error = dict()
    time_temp_data = []

    # Read the XYZ file and compute densities
    structures = file_handler.safe_read_xyz(xyz_file_path)
    if not structures:
        return [], [], [], [], {}, []

    # Calculate initial RDF
    _, initial_rdf = get_initial_rdf(
        structures[0], perturb=20, noise_std=0.05, max_atoms=200, replicate=True
    )

    # Calculate initial partial_rdf
    initial_bond_lengths, Initial_Pair_rdfs = get_bond_lengths_noise(
        structures[0], perturb=20, noise_std=0.05, max_atoms=200, r_max=6.0
    )

    counter = 0
    window_size = 100  # Size of the trajectory window for RDF calculation

    for structure in structures:
        try:
            densities.append(calculator.calculate_density(structure))
            lattice_params.append(structure.get_cell_lengths_and_angles())
            temperature.append(structure.get_temperature())

            if (counter + 1) % window_size == 0:
                window_start = max(0, counter - window_size + 1)
                window_structures = structures[window_start:counter + 1]

                r, current_rdf = get_rdf(window_structures, r_max=6.0)
                min_len = min(len(current_rdf), len(initial_rdf))
                current_rdf = current_rdf[:min_len]
                initial_rdf_ = initial_rdf[:min_len]
                error_rdf = (
                    100
                    * (((current_rdf - initial_rdf_) ** 2).sum())
                    / (((initial_rdf_) ** 2).sum())
                )
                rdf_error.append(error_rdf)

                curr_bond_lengths, Pair_rdfs = get_bond_lengths_TrajAvg(
                    window_structures, r_max=6.0
                )

                for key in curr_bond_lengths.keys():
                    if key in Initial_Pair_rdfs:
                        r, initial_rdf = Initial_Pair_rdfs[key]
                        r, rdf = Pair_rdfs[key]
                        RDF_len = min(len(rdf), len(initial_rdf))
                        r = r[:RDF_len]
                        rdf = rdf[:RDF_len]
                        initial_rdf_ = initial_rdf[:RDF_len]

                        pair_error = (
                            100
                            * (((rdf - initial_rdf_) ** 2).sum())
                            / (((initial_rdf_) ** 2).sum())
                        )

                        if key not in bond_error:
                            bond_error[key] = []
                        bond_error[key].append(pair_error)

        except Exception as e:
            print(f"Error processing structure {counter} in {system_name}: {e}")

        counter += 1

    time_temp_data = file_handler.read_log_for_temperature(log_file_path)

    return densities, lattice_params, temperature, rdf_error, time_temp_data, bond_error



def save_bond_errors_to_txt(file_name, bond_error):
    """Save bond errors to a text file."""
    with open(file_name, mode='w') as file:
        for bond, errors in bond_error.items():
            file.write(f"Bond: {bond}\n")
            file.write("Errors:\n")
            file.write(", ".join(map(str, errors)) + "\n")
            file.write("\n")


def save_to_csv(file_name, data):
    """Save data to a CSV file."""
    with open(file_name, mode='w', newline='') as file:
        writer = csv.writer(file)
        if isinstance(data[0], (list, tuple)):
            writer.writerow([f"Column {i+1}" for i in range(len(data[0]))])
        writer.writerows(data)