/[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 155 - (show annotations)
Wed Nov 9 02:02:19 2005 UTC (13 years, 10 months ago) by jgs
File MIME type: text/x-python
File size: 4156 byte(s)
move all directories from trunk/esys2 into trunk and remove esys2

1 # $Id$
2
3 from esys.escript.modelframe import Model,ParameterSet
4 from esys.escript.escript import Data
5 from esys.escript.util import *
6
7
8 class EvaluateExpression(ParameterSet):
9 """
10 Return the evaluation of an expression at current time t and
11 locations in the domain
12
13 @warning: this class use python's eval function!!!!!
14 Please use input.InterpolateOverBox is possible!!!!
15 @ivar expression (in): expression or list of expressions defining
16 expression value
17 @ivar out (callable): current value of the expression
18 """
19
20 def __init__(self,debug=False):
21 """
22 Set up parameters
23 """
24 ParameterSet.__init__(self,debug=debug)
25 self.declareParameter(domain=None, \
26 t=0., \
27 expression="x[0]")
28
29 def out(self):
30 x=self.domain.getX()
31 t=self.t
32 if isinstance(self.expression,str):
33 out=eval(self.expression)
34 else:
35 out=Data(0,(len(self.expression),),x.getFunctionSpace())
36 for i in range(len(self.expression)): out[i]=eval(self.expression[i])
37 return out
38
39 class Probe(Model):
40 """
41 Tests values against a expression which may depend on time and spatial
42 coordinates.
43
44 It prints out the relative error in each time step and the maximum
45 relative error over all time steps at the end.
46
47 @warning: this class use python's eval function!!!!!
48 @ivar value (in): values to be tested
49 @ivar expression (in): expressions defining expression values to test against. If None only value is reported.
50 @ivar line_tag (in): tag to be used when printing error
51 @ivar t (in): current time
52 @ivar max_error (out): maximum error
53 @ivar t_max (out): time of maximum error
54
55 """
56
57 def __init__(self,debug=False):
58 """
59 Set up parameters
60 """
61 Model.__init__(self,debug=debug)
62 self.declareParameter(expression=None, \
63 value=0., \
64 t=0., \
65 line_tag="PROBE")
66
67 def doInitialization(self):
68 """
69 Initializes values
70 """
71 self.t_max=None
72 self.max_error=0.
73
74 def doStepPostprocessing(self,dt):
75 t=self.t
76 print "%s : time %e"%(self.line_tag,t)
77 v=self.value
78 x=v.getFunctionSpace().getX()
79 if v.getRank()==0:
80 if self.expression==None:
81 print "%s : true (min,max)= (%e,%e)"%(self.line_tag,inf(v),sub(v))
82 else:
83 ref=eval(self.expression)
84 err=Lsup(v-ref)/Lsup(ref)
85 print "%s : true (min,max)= (%e,%e)"%(self.line_tag,inf(ref),sup(ref))
86 print "%s : (min,max,rel error)= (%e,%e,%e)"%(self.line_tag,inf(v),sup(v),err)
87 else:
88 err=0
89 for i in range(v.getRank()):
90 vi=v[i]
91 if self.expression==None:
92 print "%s : component %d: true (min,max)= (%e,%e)"%(self.line_tag,i,inf(vi))
93 else:
94 refi=eval(self.expression[i])
95 erri=Lsup(vi-refi)/Lsup(refi)
96 print "%s : component %d true (min,max)= (%e,%e)"%(self.line_tag,i,inf(refi),sup(refi))
97 print "%s : component %d (min,max,rel error)= (%e,%e,%e,%e,%e)"%(self.line_tag,i,inf(vi),sup(vi),erri)
98 err=max(err,erri)
99 if not self.expression==None: print "%s : maximum error %e",err
100
101 if not self.expression==None:
102 if err>self.max_error:
103 self.t_max=t
104 self.max_error=err
105
106 def doFinalization(self):
107 """
108 Print out the maximum error.
109 """
110 if not self.t_max==None: print "%s : == maximum error %e at time %e == "%(self.line_tag,self.max_error,self.t_max)
111
112 # 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