1 |
# $Id$ |
2 |
|
3 |
import sys |
4 |
import os |
5 |
import unittest |
6 |
import time |
7 |
|
8 |
from esys.escript import * |
9 |
from esys.escript.linearPDEs import * |
10 |
from esys import finley |
11 |
|
12 |
starttime = time.clock() |
13 |
|
14 |
print "\nSimpleSolve.py" |
15 |
print "--------------" |
16 |
|
17 |
alpha=0.7 |
18 |
error_tol=1.e-5 |
19 |
|
20 |
# generate mesh |
21 |
|
22 |
# print "\nGenerate mesh: finley.Rectangle(9,12,1)=>" |
23 |
# mydomain=finley.Rectangle(140,140) |
24 |
|
25 |
# print "\nGenerate mesh: finley.Rectangle(4,4,1)=>" |
26 |
mydomain=finley.Rectangle(60,40,1) |
27 |
# mydomain=finley.Rectangle(250,250,1) |
28 |
mydomain=finley.Rectangle(100,100,1) |
29 |
|
30 |
print "\nGenerate mesh: finley.Rectangle(151,151,1)=>" |
31 |
# mydomain=finley.Rectangle(151,151,1) |
32 |
# mydomain=finley.Rectangle(128,128,1) |
33 |
|
34 |
print "\nSetup domain and functions" |
35 |
print "--------------------------" |
36 |
|
37 |
print "e=Function(mydomain):" |
38 |
e=Function(mydomain) |
39 |
|
40 |
print "n=ContinuousFunction(mydomain):" |
41 |
n=ContinuousFunction(mydomain) |
42 |
|
43 |
# get handles to nodes and elements 1 |
44 |
|
45 |
print "\nGet handles to nodes and elements(1)=>" |
46 |
print "--------------------------------------" |
47 |
|
48 |
print "u_ex=Scalar(1,n,True):" |
49 |
u_ex=Scalar(1,n,True) |
50 |
|
51 |
print "x=e.getX():" |
52 |
x=e.getX() |
53 |
|
54 |
print "norm_u_ex=Lsup(u_ex):" |
55 |
norm_u_ex=Lsup(u_ex) |
56 |
|
57 |
print "\nGenerate a test solution (1)" |
58 |
print "----------------------------" |
59 |
|
60 |
print "mypde=LinearPDE( A=[[1.,0.8],[0.4,1.]], D=alpha, Y=alpha, domain=mydomain)" |
61 |
mypde=LinearPDE(mydomain) |
62 |
mypde.setDebugOn() |
63 |
mypde.setValue(A=[[1.,-0.001],[-0.001,1.]],D=alpha,Y=alpha) |
64 |
|
65 |
print "mypde.checkSymmetry()" |
66 |
print mypde.checkSymmetry() |
67 |
|
68 |
print "\nIterative Solver (1)=>" |
69 |
# mypde.setSolverMethod(mypde.PRES20,preconditioner=mypde.ILU0) |
70 |
mypde.setSolverMethod(mypde.BICGSTAB,preconditioner=mypde.JACOBI) |
71 |
u_i=mypde.getSolution(verbose=True,iter_max=3000) |
72 |
|
73 |
print "\nDirect Solver (1)=>" |
74 |
mypde.setSolverMethod(mypde.DIRECT) |
75 |
u_d=mypde.getSolution(verbose=True) |
76 |
|
77 |
print "\n***************************************************************" |
78 |
error=u_ex-u_d |
79 |
error_norm=Lsup(error)/norm_u_ex |
80 |
print "norm of the error for direct solver is : ",error_norm |
81 |
if error_norm > error_tol: |
82 |
print "### error norm exceeded maximum tolerance ###" |
83 |
sys.exit(1) |
84 |
error=u_ex-u_i |
85 |
error_norm=Lsup(error)/norm_u_ex |
86 |
print "norm of the error for iterative solver is: ",error_norm |
87 |
if error_norm > error_tol: |
88 |
print "### error norm exceeded maximum tolerance ###" |
89 |
sys.exit(1) |
90 |
print "***************************************************************" |
91 |
del mypde |
92 |
print "***************************************************************" |
93 |
|
94 |
|
95 |
# get handles to nodes and elements 2 |
96 |
|
97 |
print "\nGet handles to nodes and elements(2)=>" |
98 |
print "--------------------------------------" |
99 |
|
100 |
print "x=n.getX():" |
101 |
x=n.getX() |
102 |
|
103 |
print " msk=whereZero(x[0])+whereZero(x[0]-1.)" |
104 |
msk=whereZero(x[0])+whereZero(x[0]-1.) |
105 |
|
106 |
print "mypde=LinearPDE(A=[[1.,0.],[0.,1.]],q=msk,r=u_ex)" |
107 |
mypde=LinearPDE(mydomain) |
108 |
mypde.setDebugOn() |
109 |
mypde.setValue(A=[[1.,0.0],[0.0,1.]],q=msk,r=u_ex) |
110 |
|
111 |
print "mypde.checkSymmetry()" |
112 |
print mypde.checkSymmetry() |
113 |
|
114 |
# generate a test solution 2 |
115 |
|
116 |
print "\nGenerate a test solution (2)" |
117 |
print "----------------------------" |
118 |
|
119 |
print "\nDirect Solver (2)=>" |
120 |
|
121 |
mypde.setSymmetryOn() |
122 |
mypde.setTolerance(1.e-13) |
123 |
|
124 |
# mypde.setSymmetryOn() : is not woking yet! |
125 |
mypde.setSolverMethod(mypde.DIRECT) |
126 |
u_d=mypde.getSolution(verbose=True) |
127 |
|
128 |
print "\nIterative Solver (2)=>" |
129 |
|
130 |
mypde.setSolverMethod(mypde.ITERATIVE) |
131 |
u_i=mypde.getSolution(verbose=True,iter_max=3000) |
132 |
|
133 |
print "\n******************************************************************" |
134 |
error=u_ex-u_d |
135 |
error_norm=Lsup(error)/norm_u_ex |
136 |
print "norm of the error for direct solver is : ",error_norm |
137 |
if error_norm > error_tol: |
138 |
print "### error norm exceeded maximum tolerance ###" |
139 |
sys.exit(1) |
140 |
error=u_ex-u_i |
141 |
error_norm=Lsup(error)/norm_u_ex |
142 |
print "norm of the error for iterative solver is: ",error_norm |
143 |
if error_norm > error_tol: |
144 |
print "### error norm exceeded maximum tolerance ###" |
145 |
sys.exit(1) |
146 |
print "******************************************************************" |
147 |
|
148 |
print "\n-----" |
149 |
print "Done." |
150 |
print "-----" |
151 |
|
152 |
stoptime = time.clock() |
153 |
elapsed = stoptime - starttime |
154 |
print "\nElapsed time: ", elapsed, "\n" |
155 |
|
156 |
sys.exit(0) |