1 | from scipy import sin, cos, sqrt, pi, dot |
---|
2 | from gaussPivot import * |
---|
3 | from parameter import * |
---|
4 | |
---|
5 | def root_g(a,b,t): |
---|
6 | dx = 0.01 |
---|
7 | def g(u): |
---|
8 | return u + 2.0*A*pi/T*sin(2.0*pi/T*(t+u)) |
---|
9 | while 1: |
---|
10 | x1,x2 = rootsearch(g,a,b,dx) |
---|
11 | if x1 != None: |
---|
12 | a = x2 |
---|
13 | root = bisect(g,x1,x2,1) |
---|
14 | else: |
---|
15 | break |
---|
16 | return root |
---|
17 | def shore(t): |
---|
18 | a = -1.0 |
---|
19 | b = 1.0 |
---|
20 | #dx = 0.01 |
---|
21 | u = root_g(a,b,t) |
---|
22 | xi = -0.5*u*u + A*cos(2.0*pi/T*(t+u)) #This is the displacement |
---|
23 | #position = 1.0 + xi |
---|
24 | return [xi, u]#[position, u] |
---|
25 | |
---|
26 | |
---|
27 | |
---|
28 | def prescribe(x,t): |
---|
29 | from Numeric import array, zeros, Float |
---|
30 | q_shore = shore(t) |
---|
31 | #q = zeros(2,Float) |
---|
32 | if x<1.0-abs(q_shore[0]): |
---|
33 | #q[0] = w_at_O(t) |
---|
34 | #q[1] = u_at_O(t) |
---|
35 | q = zeros(2,Float) |
---|
36 | else: |
---|
37 | q = q_shore |
---|
38 | def fun(q): #Here q=(w, u) |
---|
39 | f = zeros(2,Float) |
---|
40 | f[0] = q[0] + 0.5*q[1]**2.0 - A*j0(4.0*pi/T*(1.0+q[0]-x)**0.5)*cos(2.0*pi/T*(t+q[1])) |
---|
41 | f[1] = q[1] + A*j1(4.0*pi/T*(1.0+q[0]-x)**0.5)*sin(2.0*pi/T*(t+q[1]))/(1+q[0]-x)**0.5 |
---|
42 | return f |
---|
43 | def newtonRaphson2(f,q,tol=1.0e-13): |
---|
44 | #print "Initially q=",q |
---|
45 | for i in range(50): |
---|
46 | h = 1.0e-4 ##1.0e-4 may be too large. |
---|
47 | n = len(q) |
---|
48 | jac = zeros((n,n),Float) |
---|
49 | if 1.0+q[0]-x<0.0: |
---|
50 | #temp1 = 1.0+q[0]-x |
---|
51 | q[0] = x-1.0+0.01 |
---|
52 | #print "1.0+q[0]-x=",1.0+q[0]-x |
---|
53 | #SomethingWRONG |
---|
54 | #return q |
---|
55 | #q[0] =0.25 |
---|
56 | f0 = f(q) |
---|
57 | for i in range(n): |
---|
58 | temp = q[i] |
---|
59 | q[i] = temp + h |
---|
60 | f1 = f(q) |
---|
61 | q[i] = temp |
---|
62 | jac[:,i] = (f1 - f0)/h |
---|
63 | if sqrt(dot(f0,f0)/len(q)) < tol: return q |
---|
64 | dq = gaussPivot(jac,-f0) |
---|
65 | q = q + dq |
---|
66 | if sqrt(dot(dq,dq)) < tol*max(max(abs(q)),1.0): return q |
---|
67 | print 'Too many iterations' |
---|
68 | q = newtonRaphson2(fun,q) |
---|
69 | return q[0], q[1] |
---|