PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
Loading...
Searching...
No Matches
convert_grid_from_legacy_to_picgrid.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""
3Convert LES-Flat grid format to PICGRID format
4Input: Coordinates stored separately by dimension
5Output: PICGRID format with all coordinates per point
6"""
7
8import numpy as np
9
10def read_les_flat_grid(filename):
11 """Read grid file in LES-Flat format"""
12 with open(filename, 'r') as f:
13 lines = f.readlines()
14
15 # First line: number of blocks
16 nblocks = int(lines[0].strip())
17
18 # Second line: grid dimensions
19 ni, nj, nk = map(int, lines[1].strip().split())
20
21 print(f"Grid dimensions: {ni} x {nj} x {nk}")
22
23 # Read X coordinates (lines 2 to 2+ni)
24 x_coords = np.zeros(ni)
25 for i in range(ni):
26 vals = list(map(float, lines[2 + i].strip().split()))
27 x_coords[i] = vals[0] # X is in first column
28
29 # Read Y coordinates (lines 2+ni to 2+ni+nj)
30 y_coords = np.zeros(nj)
31 for j in range(nj):
32 vals = list(map(float, lines[2 + ni + j].strip().split()))
33 y_coords[j] = vals[1] # Y is in second column
34
35 # Read Z coordinates (lines 2+ni+nj to 2+ni+nj+nk)
36 z_coords = np.zeros(nk)
37 for k in range(nk):
38 vals = list(map(float, lines[2 + ni + nj + k].strip().split()))
39 z_coords[k] = vals[2] # Z is in third column
40
41 return ni, nj, nk, x_coords, y_coords, z_coords
42
43def write_picgrid_format(filename, ni, nj, nk, x_coords, y_coords, z_coords):
44 """Write grid file in PICGRID format"""
45
46 total_points = ni * nj * nk
47 print(f"Writing {total_points} grid points to {filename}")
48
49 with open(filename, 'w') as f:
50 # Write header
51 f.write("PICGRID\n")
52 f.write("1\n")
53 f.write(f"{ni} {nj} {nk}\n")
54
55 # Write coordinates
56 # The ordering appears to be: for i, for k, for j (based on cpipe_coarse.grid)
57 # This means i varies fastest, then j, then k
58 for k in range(ni):
59 for j in range(nk):
60 for i in range(nj):
61 x = x_coords[i] + min(x_coords)
62 y = y_coords[j] + min(y_coords)
63 z = z_coords[k] + min(z_coords)
64 f.write(f"{x:.8e} {y:.8e} {z:.8e}\n")
65
66 print(f"Successfully wrote {filename}")
67
68def main():
69 input_file = "/mnt/user-data/uploads/LES-Flat_grid.dat"
70 output_file = "/mnt/user-data/outputs/LES-Flat_PICGRID.grid"
71
72 print(f"Reading {input_file}...")
73 ni, nj, nk, x_coords, y_coords, z_coords = read_les_flat_grid(input_file)
74
75 print(f"\nCoordinate ranges:")
76 print(f" X: [{x_coords.min():.6f}, {x_coords.max():.6f}]")
77 print(f" Y: [{y_coords.min():.6f}, {y_coords.max():.6f}]")
78 print(f" Z: [{z_coords.min():.6f}, {z_coords.max():.6f}]")
79
80 print(f"\nWriting to {output_file}...")
81 write_picgrid_format(output_file, ni, nj, nk, x_coords, y_coords, z_coords)
82
83 print("\nConversion complete!")
84
85if __name__ == "__main__":
86 main()
write_picgrid_format(filename, ni, nj, nk, x_coords, y_coords, z_coords)
Head of a generic C-style linked list.
Definition variables.h:372