forked from ATOMSLab/LeanLJ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnergyLRC_calculation
More file actions
163 lines (134 loc) · 4.45 KB
/
EnergyLRC_calculation
File metadata and controls
163 lines (134 loc) · 4.45 KB
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
import Lean
/-!
## 1. Float Parsing
A small parser that converts a string to a Float.
-/
def parseFloat? (s : String) : Option Float := Id.run do
let s := s.trim
if s.isEmpty then
return none
-- Check for a leading negative sign
let isNeg := s.startsWith "-"
let s := if isNeg then s.drop 1 else s
if s.isEmpty then
return none
let parts := s.splitOn "."
match parts with
| [intStr] =>
match intStr.toNat? with
| some intVal =>
let val := intVal.toFloat
return if isNeg then some (-val) else some val
| none => return none
| [intStr, fracStr] =>
if intStr.isEmpty && fracStr.isEmpty then
return none
let intVal := (intStr.toNat?).getD 0
let fracVal := (fracStr.toNat?).getD 0
let fracPow := 10.0 ^ (fracStr.length.toFloat)
let val := intVal.toFloat + (fracVal.toFloat / fracPow)
return if isNeg then some (-val) else some val
| _ => return none
/-- Convert a string to a Float, returning 0.0 if parse fails. -/
def stringToFloat (s : String) : Float :=
match parseFloat? s with
| some f => f
| none => 0.0
/-!
## 2. CSV Parsing for (Fin 3 → Float)
-/
/-- Parse one line of CSV (format: "x,y,z") into a (Fin 3 → Float). -/
def parseLineToFin3 (line : String) : Option (Fin 3 → Float) :=
let parts := line.splitOn ","
if parts.length < 3 then
none
else
let xVal := stringToFloat parts[0]!
let yVal := stringToFloat parts[1]!
let zVal := stringToFloat parts[2]!
some (fun i =>
match i.val with
| 0 => xVal
| 1 => yVal
| 2 => zVal
| _ => 0.0)
/-- Parse an array of CSV lines into a list of (Fin 3 → Float). -/
def parseCSVToFin3 (lines : Array String) : List (Fin 3 → Float) :=
let parsed := lines.foldl (fun acc line =>
match parseLineToFin3 line with
| some vec => vec :: acc
| none => acc
) []
parsed.reverse
/-!
## 3. NIST Long-Range Correction
U_LRC^* = (8π/3) ρ^* [ 1/(3 r_c^9) - 1/(r_c^3) ]
where ρ^* = N / (Lx^* Ly^* Lz^*)
-/
def pi : Float := 3.141592653589793
/-- Compute the dimensionless tail correction U_LRC* from NIST. -/
def computeULRCStar
(positions : List (Fin 3 → Float))
(boxLength : Fin 3 → Float)
(cutoffStar : Float)
: Float :=
let n := positions.length.toFloat
let volume := (boxLength ⟨0, by decide⟩)
* (boxLength ⟨1, by decide⟩)
* (boxLength ⟨2, by decide⟩)
let rhoStar := n / volume
(8.0 / 3.0) * pi * rhoStar *
( (1.0 / (3.0 * (cutoffStar ^ 9))) - (1.0 / (cutoffStar ^ 3)) )
/-!
## 4. Complete `main` Function
Reads:
- CSV path
- dimensionless cutoff r_c^*
- dimensionless box lengths
Then computes U_LRC^* and prints it.
-/
def main : IO Unit := do
let stdout ← IO.getStdout
let stdin ← IO.getStdin
-- Prompt for CSV path
stdout.putStrLn "Enter the path to the CSV file containing *dimensionless* positions:"
let filePath ← stdin.getLine
let filePath := filePath.trim
-- Read file and parse positions
let input ← IO.FS.readFile filePath
let lines := input.splitOn "\n" |>.toArray
let positions := parseCSVToFin3 lines
if positions.isEmpty then
stdout.putStrLn "Error: No valid positions found in the CSV file."
return ()
let n := positions.length
stdout.putStrLn s!"Number of positions parsed: {n}"
-- Prompt for dimensionless cutoff
stdout.putStrLn "Enter the dimensionless cutoff r_c^* (e.g., 2.5):"
let cutoffStr := (← stdin.getLine).trim
let cutoffStar := stringToFloat cutoffStr
-- Prompt for dimensionless box lengths
stdout.putStrLn "Enter dimensionless box lengths Lx^*,Ly^*,Lz^* (comma-separated, e.g. 8.0,8.0,8.0):"
let boxStr := (← stdin.getLine).splitOn ","
if boxStr.length < 3 then
stdout.putStrLn "Error: Not enough box-length entries!"
return ()
let Lx := stringToFloat boxStr[0]!
let Ly := stringToFloat boxStr[1]!
let Lz := stringToFloat boxStr[2]!
-- Construct box length function
let boxLength : Fin 3 → Float := fun i =>
match i.val with
| 0 => Lx
| 1 => Ly
| 2 => Lz
| _ => 0.0
-- Compute U_LRC^*
let n := positions.length.toFloat
let ULRCStar := n * computeULRCStar positions boxLength cutoffStar
-- Output
stdout.putStrLn ""
stdout.putStrLn "==========================================="
stdout.putStrLn s!"Dimensionless Lennard-Jones Tail Correction"
stdout.putStrLn s!"U_LRC^* = {ULRCStar}"
stdout.putStrLn "==========================================="