Hi, I am absolutely novice at this in general. My background is in mechanical engineering so only have had self-learning and an intro to C course. I am parsing a file of text that I get from my simulations and I am having trouble saving the all of the values. Instead I only have the final value after solving in a for loop. I’ve implemented it in so many ways from reading on my own and it has to be a simple fix.
First I tried doing the following :
readdir()
f = open("Cruciform/Post-Processing/hellothere.txt")
storelines = readlines(f)
close(f)
iter = 220
storage = []
for iter in 220:230
iter += 1
initialRes = []
if findnext("Solving for p", storelines[iter], 1) != nothing
sst = storelines[iter]
findVal = findfirst(r"\d\.\d\d\d\d\d", sst)
initialRes = sst[findVal];
println("$initialRes");
end # empty as in do nothing
end
# SAMPLE OF TEXT FILE FOR REFERENCE
forces forceCoeffs1:
Not including porosity effects
forceCoeffs forceCoeffs1:
Not including porosity effects
Courant Number mean: 0.0226649 max: 11.448
Time = 0.001
PIMPLE: Iteration 1
smoothSolver: Solving for Ux, Initial residual = 0.999998, Final residual = 0.0479853, No Iterations 2
GAMG: Solving for p, Initial residual = 1, Final residual = 0.0580078, No Iterations 7
# END FILE EXCERPT OF LINES
The value was extracted from the last line extracted, but the for loop will not save all of the instances of “Solving for p” in the document, only the final iteration in the loop (I provided one instance in the sample text file but the line will appear 1000s of times). I decided let me trouble shoot the simpler method with just one specific line of the text file, say, for example line 225. So I re-wrote my code into this function and called a single line succesfully while catching errors
Cleaner attempt #2
function parseNumbers(inputLog,dataLot)
storelines = readlines(inputLog) # Take the file input and read
storage = [ ] # temp storage
# create condition to read pressure or break if error
if findnext("Solving for p", storelines[dataLot], 1) != nothing
sst = storelines[dataLot] # save lines to variable
findVal = findfirst(r"\d\.\d\d\d\d\d\d\d", sst) # filter lines
initialRes = sst[findVal]; # combine the two methods
store = parse(Float64, initialRes); # create parse to convert strings
return push!(storage, copy(store)) # push the value to storage
else
initialRes = 0.0 # if theres an error just give 0
return push!(storage, initialRes)
end
close(inputLog) # Close the original file
end
``
d = open("Cruciform/Post-Processing/hellothere.txt") # open the text file
dataUse = 225 # line number in the text file to try on
testVar = parseNumbers(d,dataUse) # use our function
→ Then Julia provided me with the value of 0.050078. Great so now I figured I would write a simple for loop to do each entry from line 220 to 230 as a small step.
Implementation of my function in a For Loop
limits = 230
newstore = Vector{testVari}(undef, limits)
#newstore = [ ]
count = 0
for i in 220:limits
dataStart = 220
count += 1
value = parseNumbers(d,dataStart)
newstore[count] = testVari(count, value)
#push!(newstore, testVari)
i += 1
end