/[escript]/branches/3.4.1/modellib/py_src/probe.py
ViewVC logotype

Contents of /branches/3.4.1/modellib/py_src/probe.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4587 - (show annotations)
Wed Dec 11 06:17:09 2013 UTC (5 years, 3 months ago) by jfenwick
File MIME type: text/x-python
File size: 5002 byte(s)
Preparation begins

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