CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Community Contributions

[PyFoam] Parse log file while running.

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 14, 2014, 17:18
Default Parse log file while running.
  #1
Member
 
Ripudaman Manchanda
Join Date: May 2013
Posts: 55
Rep Power: 12
ripudaman is on a distinguished road
Hi,

I am relatively inexperienced in pyFoam. I am using bits and pieces of information that I have gathered from the various pyFoam talks and presentations over the years.

I am having a problem in being able to run a case, generate a specific log file and read the log file for a regExp to modify the output on the screen all in one go. I am able to do all this but I am having to run the solver twice. I want to find a way to do this without having to run the solver twice. Also another tips on this will be very useful.

Here is the snippet of code that I am using
Code:
run=AnalyzedRunner(Analyzer(),silent=True,argv=["convergeFracWidthFoam","-case",work.name],logname='CFWF')
run.start()

CONVERGED=LogAnalyzer("CONVERGED")
analyzer=LogAnalyzerApplication(CONVERGED)
analyzer.run(path.join(target,"CFWF.logfile"))
Here is the logAnalyzer.py file that I use to parse the regular expressions in my created log file
Code:
import re,sys
from PyFoam.LogAnalysis.FoamLogAnalyzer import FoamLogAnalyzer
from PyFoam.LogAnalysis.LogAnalyzerApplication import LogAnalyzerApplication
from PyFoam.LogAnalysis.LogLineAnalyzer import LogLineAnalyzer
from PyFoam.LogAnalysis.BoundingLogAnalyzer import BoundingLogAnalyzer
class LogAnalyzer(FoamLogAnalyzer):
    def __init__(self,name):
    super(LogAnalyzer,self).__init__()
    self.addAnalyzer(name,LineAnalyzer())
    def obtainWidth(self,name):
    return self.getAnalyzer(name).obtainWidth()
    def obtainHaltRatio(self,name):
    return self.getAnalyzer(name).obtainHaltRatio()

class LineAnalyzer(LogLineAnalyzer):
    def __init__(self):
        LogLineAnalyzer.__init__(self)
 
        self.told=""
        self.exp1=re.compile("^(.+):  Solving for (.+), Initial residual = (.+), Final residual = (.+), No Iterations (.+)$")
     self.exp2=re.compile("Previous Fracture Width = (.+)  Current Fracture Width = (.+)$")
    self.exp3=re.compile("Current Halt Ratio = (.+)  Desired Halt Ratio = (.+)$")
    self.currentwidth=0
    self.haltratio=1

    def doAnalysis(self,line):
        m=self.exp1.match(line)
        if m!=None:
            name=m.group(2)
            resid=m.group(3)
        iters=m.group(5)
            time=self.getTime()
            if time!=self.told:
                self.told=time
                print "\n t = %3g: " % ( float(time) ),
            print "%3s: %3e %5s: %1g" % (name,float(resid),"NIter",float(iters)),
            #sys.stdout.flush()

     m=self.exp2.match(line)
    if m!=None:
        previous=m.group(1)
        current=m.group(2)
        print "\t %5s: %6e" % ("FracWidth",float(current)),
        self.currentwidth=float(current)
        sys.stdout.flush()

     m=self.exp3.match(line)
    if m!=None:
        current=m.group(1)
        target=m.group(2)
        self.haltratio=float(current)

    def obtainWidth(self):
    return self.currentwidth

    def obtainHaltRatio(self):
    return self.haltratio

class Analyzer(BoundingLogAnalyzer):
    def __init__(self):
        BoundingLogAnalyzer.__init__(self)
        self.addAnalyzer("Analyzer",LineAnalyzer())
I apologize for any redundancy here since I do not fully understand the way pyFoam libraries are set and am not very familiar with the class system in python.

Any help here is appreciated.

Thank you.
Regards,
Ripu
ripudaman is offline   Reply With Quote

Old   March 19, 2014, 17:57
Default
  #2
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by ripudaman View Post
Hi,

I am relatively inexperienced in pyFoam. I am using bits and pieces of information that I have gathered from the various pyFoam talks and presentations over the years.

I am having a problem in being able to run a case, generate a specific log file and read the log file for a regExp to modify the output on the screen all in one go. I am able to do all this but I am having to run the solver twice. I want to find a way to do this without having to run the solver twice. Also another tips on this will be very useful.

Here is the snippet of code that I am using
Code:
run=AnalyzedRunner(Analyzer(),silent=True,argv=["convergeFracWidthFoam","-case",work.name],logname='CFWF')
run.start()

CONVERGED=LogAnalyzer("CONVERGED")
analyzer=LogAnalyzerApplication(CONVERGED)
analyzer.run(path.join(target,"CFWF.logfile"))
Here is the logAnalyzer.py file that I use to parse the regular expressions in my created log file
Code:
import re,sys
from PyFoam.LogAnalysis.FoamLogAnalyzer import FoamLogAnalyzer
from PyFoam.LogAnalysis.LogAnalyzerApplication import LogAnalyzerApplication
from PyFoam.LogAnalysis.LogLineAnalyzer import LogLineAnalyzer
from PyFoam.LogAnalysis.BoundingLogAnalyzer import BoundingLogAnalyzer
class LogAnalyzer(FoamLogAnalyzer):
    def __init__(self,name):
    super(LogAnalyzer,self).__init__()
    self.addAnalyzer(name,LineAnalyzer())
    def obtainWidth(self,name):
    return self.getAnalyzer(name).obtainWidth()
    def obtainHaltRatio(self,name):
    return self.getAnalyzer(name).obtainHaltRatio()

class LineAnalyzer(LogLineAnalyzer):
    def __init__(self):
        LogLineAnalyzer.__init__(self)
 
        self.told=""
        self.exp1=re.compile("^(.+):  Solving for (.+), Initial residual = (.+), Final residual = (.+), No Iterations (.+)$")
     self.exp2=re.compile("Previous Fracture Width = (.+)  Current Fracture Width = (.+)$")
    self.exp3=re.compile("Current Halt Ratio = (.+)  Desired Halt Ratio = (.+)$")
    self.currentwidth=0
    self.haltratio=1

    def doAnalysis(self,line):
        m=self.exp1.match(line)
        if m!=None:
            name=m.group(2)
            resid=m.group(3)
        iters=m.group(5)
            time=self.getTime()
            if time!=self.told:
                self.told=time
                print "\n t = %3g: " % ( float(time) ),
            print "%3s: %3e %5s: %1g" % (name,float(resid),"NIter",float(iters)),
            #sys.stdout.flush()

     m=self.exp2.match(line)
    if m!=None:
        previous=m.group(1)
        current=m.group(2)
        print "\t %5s: %6e" % ("FracWidth",float(current)),
        self.currentwidth=float(current)
        sys.stdout.flush()

     m=self.exp3.match(line)
    if m!=None:
        current=m.group(1)
        target=m.group(2)
        self.haltratio=float(current)

    def obtainWidth(self):
    return self.currentwidth

    def obtainHaltRatio(self):
    return self.haltratio

class Analyzer(BoundingLogAnalyzer):
    def __init__(self):
        BoundingLogAnalyzer.__init__(self)
        self.addAnalyzer("Analyzer",LineAnalyzer())
I apologize for any redundancy here since I do not fully understand the way pyFoam libraries are set and am not very familiar with the class system in python.

Any help here is appreciated.

Thank you.
Regards,
Ripu
If I understand you correctly your problem is that your first run the solver and then analyze the log again. BTW: have you looked at http://web.student.chalmers.se/group...amAdvanced.pdf Starting at slide 66 something similar is done. Especially slide 75 with the lines 8, 10 and 16 demonstrate what you want to do: generate a LogAnalyzer-object. Pass it to the AnalyzedRunner. In the end use the two obtain-methods to get the information you want.

Basically (but you seem to have guessed so far) the division of tasks is: the LineAnalyzer looks at one line at a time and reacts to it. The Analyzer collects LineAnalyzer and feeds all of them the same lines. The AnalyzedRunner executes the solver and hands each line of the output to the Analyzer. What is important is that if you want to access data the Analyzer might have is to create the analyzer as a separate variable, pass the variable to the runner. After the Analyzer has the data. You've just got to ask for it
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   March 19, 2014, 23:51
Default
  #3
Member
 
Ripudaman Manchanda
Join Date: May 2013
Posts: 55
Rep Power: 12
ripudaman is on a distinguished road
Thank you for your response Bernhard. I have seen the presentation you suggested and have incorporated parts of it in my code. However, I did not fully understand the implementation in the presentation. I think I need to spend some time understanding the nuances of the code written in the presentation. The algorithmic guide you provide in the steps of execution should help in aiding my understanding.

I hope a more careful re-look at what is going on will help. Is there any way I can access the code explained in the slides in one file? That would help tremendously. For example, where can I find AbscheideAnalyzer.py?

Thanks again.
Ripu
ripudaman is offline   Reply With Quote

Old   March 20, 2014, 13:27
Default
  #4
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by ripudaman View Post
Thank you for your response Bernhard. I have seen the presentation you suggested and have incorporated parts of it in my code. However, I did not fully understand the implementation in the presentation. I think I need to spend some time understanding the nuances of the code written in the presentation. The algorithmic guide you provide in the steps of execution should help in aiding my understanding.

I hope a more careful re-look at what is going on will help. Is there any way I can access the code explained in the slides in one file? That would help tremendously. For example, where can I find AbscheideAnalyzer.py?

Thanks again.
Ripu
The data for the presentation is found at https://sourceforge.net/projects/ope...nced_Training/ (I'll add a link on the Wiki)

I only glanced at your code and I think the necessary change is what I described in the last post: create an Analyzer-object. Pass that to the Runner. Afterwards query the Analyzer-object. Basically you've got everything there. Only the order does not fit
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   March 30, 2014, 22:41
Default
  #5
Member
 
Ripudaman Manchanda
Join Date: May 2013
Posts: 55
Rep Power: 12
ripudaman is on a distinguished road
You were absolutely right. The rearrangement of the statements and following the logic you recommended solved my problem. Thank you very much. Now my code will run in half the time
ripudaman is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
OpenFoam "Permission denied" and "command not found" problems. iyidaniel@yahoo.co.uk OpenFOAM Running, Solving & CFD 11 January 2, 2018 06:47
[OpenFOAM.org] Error creating ParaView-4.1.0 OpenFOAM 2.3.0 tlcoons OpenFOAM Installation 13 April 20, 2016 17:34
[foam-extend.org] problem when installing foam-extend-1.6 Thomas pan OpenFOAM Installation 7 September 9, 2015 21:53
[OpenFOAM.org] Compile OF 2.3 on Mac OS X .... the patch gschaider OpenFOAM Installation 225 August 25, 2015 19:43
[swak4Foam] Error bulding swak4Foam sfigato OpenFOAM Community Contributions 18 August 22, 2013 12:41


All times are GMT -4. The time now is 18:25.