/[escript]/trunk/modellib/py_src/probe.py
ViewVC logotype

Contents of /trunk/modellib/py_src/probe.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2548 - (show annotations)
Mon Jul 20 06:20:06 2009 UTC (10 years, 2 months ago) by jfenwick
File MIME type: text/x-python
File size: 4925 byte(s)
Updating copyright notices
1
2 ########################################################
3 #
4 # Copyright (c) 2003-2009 by University of Queensland
5 # Earth Systems Science Computational Center (ESSCC)
6 # http://www.uq.edu.au/esscc
7 #
8 # Primary Business: Queensland, Australia
9 # Licensed under the Open Software License version 3.0
10 # http://www.opensource.org/licenses/osl-3.0.php
11 #
12 ########################################################
13
14 __copyright__="""Copyright (c) 2003-2008 by University of Queensland
15 Earth Systems Science Computational Center (ESSCC)
16 http://www.uq.edu.au/esscc
17 Primary Business: Queensland, Australia"""
18 __license__="""Licensed under the Open Software License version 3.0
19 http://www.opensource.org/licenses/osl-3.0.php"""
20 __url__="https://launchpad.net/escript-finley"
21
22 from esys.escript.modelframe import Model,ParameterSet
23 from esys.escript.escript import Data
24 from esys.escript.util import *
25
26
27 class EvaluateExpression(ParameterSet):
28 """
29 Return the evaluation of an expression at current time t and
30 locations in the domain
31
32 @warning: this class use python's eval function!!!!!
33 Please use input.InterpolateOverBox is possible!!!!
34 @ivar expression: expression or list of expressions defining
35 expression value (in)
36 @ivar out: current value of the expression (callable)
37 """
38
39 def __init__(self,**kwargs):
40 """
41 Set up parameters
42 """
43 super(EvaluateExpression, self).__init__(**kwargs)
44 self.declareParameter(domain=None, \
45 t=0., \
46 expression="x[0]")
47
48 def out(self):
49 x=self.domain.getX()
50 t=self.t
51 if isinstance(self.expression,str):
52 out=eval(self.expression)
53 else:
54 out=Data(0,(len(self.expression),),x.getFunctionSpace())
55 for i in range(len(self.expression)): out[i]=eval(self.expression[i])
56 return out
57
58 class Probe(Model):
59 """
60 Tests values against a expression which may depend on time and spatial
61 coordinates.
62
63 It prints out the relative error in each time step and the maximum
64 relative error over all time steps at the end.
65
66 @warning: this class uses python's eval function!!!!!
67 @ivar value: values to be tested (in)
68 @ivar expression: expressions defining expression values to test against. If None only value is reported. (in)
69 @ivar line_tag: tag to be used when printing error. (in)
70 @ivar t: current time (in)
71 @ivar max_error: maximum error (out)
72 @ivar t_max: time of maximum error (out)
73
74 """
75
76 def __init__(self,**kwargs):
77 """
78 Set up parameters
79 """
80 super(Probe, self).__init__(**kwargs)
81 self.declareParameter(expression=None, \
82 value=0., \
83 t=0., \
84 line_tag="PROBE")
85
86 def doInitialization(self):
87 """
88 Initializes values
89 """
90 self.t_max=None
91 self.max_error=0.
92
93 def doStepPostprocessing(self,dt):
94 t=self.t
95 print "%s : time %e"%(self.line_tag,t)
96 v=self.value
97 x=v.getFunctionSpace().getX()
98 if v.getRank()==0:
99 if self.expression==None:
100 print "%s : true (min,max)= (%e,%e)"%(self.line_tag,inf(v),sub(v))
101 else:
102 ref=eval(self.expression)
103 err=Lsup(v-ref)/Lsup(ref)
104 print "%s : true (min,max)= (%e,%e)"%(self.line_tag,inf(ref),sup(ref))
105 print "%s : (min,max,rel error)= (%e,%e,%e)"%(self.line_tag,inf(v),sup(v),err)
106 else:
107 err=0
108 for i in range(v.getRank()):
109 vi=v[i]
110 if self.expression==None:
111 print "%s : component %d: true (min,max)= (%e,%e)"%(self.line_tag,i,inf(vi))
112 else:
113 refi=eval(self.expression[i])
114 erri=Lsup(vi-refi)/Lsup(refi)
115 print "%s : component %d true (min,max)= (%e,%e)"%(self.line_tag,i,inf(refi),sup(refi))
116 print "%s : component %d (min,max,rel error)= (%e,%e,%e,%e,%e)"%(self.line_tag,i,inf(vi),sup(vi),erri)
117 err=max(err,erri)
118 if not self.expression==None: print "%s : maximum error %e",err
119
120 if not self.expression==None:
121 if err>self.max_error:
122 self.t_max=t
123 self.max_error=err
124
125 def doFinalization(self):
126 """
127 Print out the maximum error.
128 """
129 if not self.t_max==None: print "%s : == maximum error %e at time %e == "%(self.line_tag,self.max_error,self.t_max)
130
131 # vim: expandtab shiftwidth=4:

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26