/[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 4657 - (show annotations)
Thu Feb 6 06:12:20 2014 UTC (5 years, 8 months ago) by jfenwick
File MIME type: text/x-python
File size: 5070 byte(s)
I changed some files.
Updated copyright notices, added GeoComp.



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