-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_acpflow.jl
91 lines (82 loc) · 3.25 KB
/
run_acpflow.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
using Sparlectra # Import Sparlectra module for power flow analysis
using BenchmarkTools # Import BenchmarkTools module for benchmarking
"""
Function to perform AC power flow analysis.
Parameters:
- max_ite: Int, the maximum number of iterations for the power flow algorithm (default: 10).
- tol: Float64, tolerance for convergence criterion (default: 1e-6).
- casefile: String, the name of the case file to load.
- path: Union{Nothing,String}, the path to the case file (default: nothing).
- verbose: Int, verbosity level for output (default: 0).
- printResultToFile: Bool, flag to print results to a file (default: false).
- printResultAnyCase: Bool, flag to print results even if the power flow fails (default: false).
Returns:
- Net, the network object.
"""
function run_acpflow(; max_ite::Int = 10, tol::Float64 = 1e-6, casefile::String, path::Union{Nothing,String} = nothing, verbose::Int = 0, printResultToFile::Bool = false, printResultAnyCase::Bool = false)::Net
ext = splitext(casefile)[2] # Get the file extension
myNet = nothing # Initialize myNet variable
in_path = nothing
out_path = nothing
if ext == ".m"
# Read network data from Matpower .m file
if path === nothing
in_path = joinpath(pwd(), "data", "mpower", strip(casefile))
out_path = joinpath(pwd(), "data", "mpower")
else
in_path = joinpath(path, strip(casefile))
out_path = joinpath(path)
end
if !isfile(in_path)
println("Error: File $(in_path) not found")
return
end
myNet = createNetFromMatPowerFile(in_path, (verbose > 0))
else
println("Error: File extension $(ext) not supported!")
return
end
# Run power flow
ite = 0
etime = @elapsed begin
ite, erg = runpf!(myNet, max_ite, tol, verbose)
end
if erg == 0 || printResultAnyCase
# Calculate network losses and print results
calcNetLosses!(myNet)
jpath = printResultToFile ? out_path : ""
printACPFlowResults(myNet, etime, ite, tol, printResultToFile, jpath)
elseif erg == 1
println("Newton-Raphson did not converge")
else
@error "Errors during calculation of Newton-Raphson"
end
return myNet
end
"""
Function to perform AC power flow analysis.
Parameters:
- net: Net, the network object.
- max_ite: Int, the maximum number of iterations for the power flow algorithm (default: 10).
- tol: Float64, tolerance for convergence criterion (default: 1e-6).
- verbose: Int, verbosity level for output (default: 0).
- printResultToFile: Bool, flag to print results to a file (default: false).
- printResultAnyCase: Bool, flag to print results even if the power flow fails (default: false).
"""
function run_net_acpflow(; net::Net, max_ite::Int = 10, tol::Float64 = 1e-6, verbose::Int = 0, printResultToFile::Bool = false, printResultAnyCase::Bool = false)
# Run power flow
ite = 0
etime = @elapsed begin
ite, erg = runpf!(net, max_ite, tol, verbose)
end
if erg == 0 || printResultAnyCase
# Calculate network losses and print results
calcNetLosses!(net)
jpath = printResultToFile ? out_path : ""
printACPFlowResults(net, etime, ite, tol, printResultToFile, jpath)
elseif erg == 1
println("Newton-Raphson did not converge")
else
@error "Errors during calculation of Newton-Raphson"
end
end