/[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 6651 - (show annotations)
Wed Feb 7 02:12:08 2018 UTC (21 months, 1 week ago) by jfenwick
File MIME type: text/x-python
File size: 5147 byte(s)
Make everyone sad by touching all the files

Copyright dates update

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