From fe0839e73fedb73ed48d9934a8f0c0f8f53a2a3f Mon Sep 17 00:00:00 2001 From: Bart Verhaegh Date: Wed, 15 Apr 2020 08:25:59 +0200 Subject: [PATCH 1/3] Feat: improved graphwriter to allow larger outputs without losing standardized output format --- pyfem/io/GraphWriter.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pyfem/io/GraphWriter.py b/pyfem/io/GraphWriter.py index 18a03365..0d082adc 100644 --- a/pyfem/io/GraphWriter.py +++ b/pyfem/io/GraphWriter.py @@ -95,8 +95,8 @@ def run( self , props , globdat ): data = data * col.factor a.append(data) - - self.outfile.write(str(data)+' ',) + clean_line = clean_output_line(str(data)) + self.outfile.write(clean_line+' ',) self.outfile.flush() self.outfile.write('\n') @@ -109,3 +109,10 @@ def run( self , props , globdat ): if not globdat.active: self.outfile.close + + def clean_outputline( line ): + return str(data).replace(' ', ' ') + .replace(' ',' ') + .replace(' ]',']') + .replace('[ ', '[') + .replace('\n', '') \ No newline at end of file From 238fc12307d1e3177b0be0abcdcd108619cd3448 Mon Sep 17 00:00:00 2001 From: Bart Verhaegh Date: Wed, 15 Apr 2020 09:32:31 +0200 Subject: [PATCH 2/3] Fix: cleanline function --- pyfem/io/GraphWriter.py | 151 ++++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 74 deletions(-) diff --git a/pyfem/io/GraphWriter.py b/pyfem/io/GraphWriter.py index 0d082adc..26b68416 100644 --- a/pyfem/io/GraphWriter.py +++ b/pyfem/io/GraphWriter.py @@ -16,7 +16,7 @@ # # # The code is open source and intended for educational and scientific # # purposes only. If you use PyFEM in your research, the developers would # -# be grateful if you could cite the book. # +# be grateful if you could cite the book. # # # # Disclaimer: # # The authors reserve all rights but do not guarantee that the code is # @@ -25,94 +25,97 @@ ############################################################################ from pyfem.util.BaseModule import BaseModule from pylab import plot, show, xlabel, ylabel, draw, ion, figure, gcf -from numpy import ndarray,zeros +from numpy import ndarray, zeros -class GraphWriter( BaseModule ): - def __init__ ( self, props , globdat ): +class GraphWriter(BaseModule): - self.prefix = globdat.prefix - self.extension = ".out" - self.onScreen = False + def __init__(self, props, globdat): - BaseModule.__init__( self , props ) + self.prefix = globdat.prefix + self.extension = ".out" + self.onScreen = False - if not hasattr( self , "filename" ): - self.filename = self.prefix + self.extension - - self.columndata = [] + BaseModule.__init__(self, props) - for i,col in enumerate ( self.columns ): + if not hasattr(self, "filename"): + self.filename = self.prefix + self.extension - colProps = getattr( self , col ) - - if not hasattr( colProps , "factor" ): - colProps.factor = 1.0 + self.columndata = [] - self.columndata.append( colProps ) + for i, col in enumerate(self.columns): - if self.onScreen and hasattr( globdat , "onScreen" ): - self.onScreen = False - else: - globdat.onScreen = True + colProps = getattr(self, col) - self.fig = gcf() - self.fig.show() - self.fig.canvas.draw() + if not hasattr(colProps, "factor"): + colProps.factor = 1.0 - self.outfile = open( self.filename ,'w' ) + self.columndata.append(colProps) - if self.onScreen: - self.output = [] + if self.onScreen and hasattr(globdat, "onScreen"): + self.onScreen = False + else: + globdat.onScreen = True - xlabel(self.columns[0]) - ylabel(self.columns[1]) - - ion() + self.fig = gcf() + self.fig.show() + self.fig.canvas.draw() - self.run( props , globdat ) + self.outfile = open(self.filename, 'w') -#------------------------------------------------------------------------------ -# -#------------------------------------------------------------------------------ + if self.onScreen: + self.output = [] - def run( self , props , globdat ): + xlabel(self.columns[0]) + ylabel(self.columns[1]) - a = [] + ion() - for i,col in enumerate(self.columndata): - if col.type in globdat.outputNames: - data = globdat.getData( col.type , col.node ) - elif hasattr(globdat,col.type): - b = getattr( globdat , col.type ) - if type(b) is ndarray: - data = b[globdat.dofs.getForType(col.node,col.dof)] - else: - data = b - else: - data = globdat.getData( col.type , col.node ) - - data = data * col.factor - - a.append(data) - clean_line = clean_output_line(str(data)) - self.outfile.write(clean_line+' ',) - self.outfile.flush() - - self.outfile.write('\n') - - if self.onScreen: - self.output.append( a ) - - plot( [x[0] for x in self.output], [x[1] for x in self.output], 'ro-' ) - self.fig.canvas.draw() - - if not globdat.active: - self.outfile.close - - def clean_outputline( line ): - return str(data).replace(' ', ' ') - .replace(' ',' ') - .replace(' ]',']') - .replace('[ ', '[') - .replace('\n', '') \ No newline at end of file + self.run(props, globdat) + +# ------------------------------------------------------------------------------ +# +# ------------------------------------------------------------------------------ + + def run(self, props, globdat): + + a = [] + + for i, col in enumerate(self.columndata): + if col.type in globdat.outputNames: + data = globdat.getData(col.type, col.node) + elif hasattr(globdat, col.type): + b = getattr(globdat, col.type) + if type(b) is ndarray: + data = b[globdat.dofs.getForType(col.node, col.dof)] + else: + data = b + else: + data = globdat.getData(col.type, col.node) + + data = data * col.factor + + a.append(data) + clean_line = clean_output_line(str(data)) + self.outfile.write(clean_line+' ',) + self.outfile.flush() + + self.outfile.write('\n') + + if self.onScreen: + self.output.append(a) + + plot([x[0] for x in self.output], [x[1] + for x in self.output], 'ro-') + self.fig.canvas.draw() + + if not globdat.active: + self.outfile.close + + def clean_outputline(line): + return str(line) \ + .replace(' ', ' ') \ + .replace(' ', ' ') \ + .replace(' ]', ']') \ + .replace('[ ', '[') \ + .replace('\n', '') From 7cda2d7091d0f96f1f6944283338ad0e17dbf924 Mon Sep 17 00:00:00 2001 From: Bart Verhaegh Date: Wed, 15 Apr 2020 09:39:41 +0200 Subject: [PATCH 3/3] Fix: cleanline function --- pyfem/io/GraphWriter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyfem/io/GraphWriter.py b/pyfem/io/GraphWriter.py index 26b68416..7487ac7f 100644 --- a/pyfem/io/GraphWriter.py +++ b/pyfem/io/GraphWriter.py @@ -96,7 +96,7 @@ def run(self, props, globdat): data = data * col.factor a.append(data) - clean_line = clean_output_line(str(data)) + clean_line = self._clean_output_line(str(data)) self.outfile.write(clean_line+' ',) self.outfile.flush() @@ -112,7 +112,7 @@ def run(self, props, globdat): if not globdat.active: self.outfile.close - def clean_outputline(line): + def _clean_output_line(self, line): return str(line) \ .replace(' ', ' ') \ .replace(' ', ' ') \