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