pythondata-extraction

Extract data from a txt


I have a log of a simulation looking like this:

Time = 1

smoothSolver:  Solving for Ux, Initial residual = 1, Final residual = 0.0289664, No Iterations 2
smoothSolver:  Solving for Uy, Initial residual = 1, Final residual = 0.028966, No Iterations 2
smoothSolver:  Solving for Uz, Initial residual = 1, Final residual = 0.0842607, No Iterations 1
GAMG:  Solving for p, Initial residual = 1, Final residual = 0.0471791, No Iterations 120
time step continuity errors : sum local = 0.000235896, global = -4.03834e-05, cumulative = -4.03834e-05
smoothSolver:  Solving for epsilon, Initial residual = 0.239416, Final residual = 0.0154473, No Iterations 1
smoothSolver:  Solving for k, Initial residual = 1, Final residual = 0.0534371, No Iterations 2
ExecutionTime = 9.27 s  ClockTime = 10 s

i want to use python to extract the initial residual values for Ux,Uy,Uz, p, epsilon and k and store them in an array. How can this be achieved in python?


Solution

  • You can use regular expressions, via standard package re

    import re
    
    log = """
    Time = 1
    
    smoothSolver:  Solving for Ux, Initial residual = 1, Final residual = 0.0289664, No Iterations 2
    smoothSolver:  Solving for Uy, Initial residual = 1, Final residual = 0.028966, No Iterations 2
    smoothSolver:  Solving for Uz, Initial residual = 1, Final residual = 0.0842607, No Iterations 1
    GAMG:  Solving for p, Initial residual = 1, Final residual = 0.0471791, No Iterations 120
    time step continuity errors : sum local = 0.000235896, global = -4.03834e-05, cumulative = -4.03834e-05
    smoothSolver:  Solving for epsilon, Initial residual = 0.239416, Final residual = 0.0154473, No Iterations 1
    smoothSolver:  Solving for k, Initial residual = 1, Final residual = 0.0534371, No Iterations 2
    ExecutionTime = 9.27 s  ClockTime = 10 s
    """
    
    
    pattern = re.compile(r'^.*Solving for (\w+), Initial residual = ([0-9.\-+eE]+), Final residual = ([0-9.\-+eE]+)')
    
    initial_residuals = {}
    final_residuals = {}
    
    for line in log.split('\n'):
      match = pattern.match(line)
      if match is not None:
        key = match.group(1)
        initial_residuals[key] = float(match.group(2))
        final_residuals[key] = float(match.group(3))
    
    print('Initial', initial_residuals)
    print('Final', final_residuals) 
    

    It would print dictionaries:

    Initial {'Ux': 1.0, 'Uy': 1.0, 'Uz': 1.0, 'p': 1.0, 'epsilon': 0.239416, 'k': 1.0}

    Final {'Ux': 0.0289664, 'Uy': 0.028966, 'Uz': 0.0842607, 'p': 0.0471791, 'epsilon': 0.0154473, 'k': 0.0534371}