-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpyigalib.f95
423 lines (362 loc) · 9.79 KB
/
pyigalib.f95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
!
! NURBS library for implementation of Isogeometric Analysis
!
subroutine find_span(p,n,m,uvec,u,mid)
!
! Determine the knot span idex
! Algorithm A2.1 in The NURBS book.
!
! Input:
! p - degree of the curve,
! n - number of control points,
! m - the highest index of knot vector,
! u - control parameter,
! uvec - a knot vector,
! Output: The knot span index while (u < uvec(mid) || u >= uvec(mid+1));- that is 'i' if u <= u < u
! i i+1
!
implicit none
integer, intent(in) :: p
integer, intent(in) :: n
integer, intent(in) :: m
double precision, dimension(0:m), intent(in) :: Uvec
double precision, intent(in) :: u
integer, intent(out) :: mid
!
! Locals
!
integer :: low, high
if ( u >= Uvec(n+1)) then
mid = n
return
endif
if ( u<= Uvec(p)) then
mid = p
return
endif
low = p
high = n+1
mid = (low + high)/2
do while (u < Uvec(mid) .or. u >= Uvec(mid+1))
if ( u < Uvec(mid) ) then
high = mid
else
low = mid
endif
mid = (low + high)/2
enddo
return
end
subroutine basis_funs(p,n,m,uvec,u,nbasis)
!
! Computes the nonvanishing basis functions.
! Based on Eq. 2.5 and Cox-deBoor-Mansfield reccurence algorithm.
! Algorithm A2.2 of The NURBS book, pp70.
!
! Input: span - specifies the span at which basis function to compute,
! u - the parametric value,
! p - degree of the curve,
! U - the knot vector
! Output: N - the non-zero basis functions in the array N(0),...N(p) of length p+1.
! This array has to be allocated in the calling function.
!
! * Dopunjeno je tako da se unutar funkcije izracunava span
!
implicit none
integer, intent(in) :: p
integer, intent(in) :: n
integer, intent(in) :: m
double precision, dimension(0:m), intent(in) :: uvec
double precision, intent(in) :: u
double precision, dimension(0:p), intent(out) :: nbasis
!
! Locals
!
integer :: i,j,r,span
double precision :: temp, saved
double precision, dimension(0:2*(p+1)-1), target :: left
double precision, dimension(:), pointer :: right
right => left(p+1:2*(p+1)-1)
call find_span(p,n,m,uvec,u,span)
i = span
nbasis(0) = 1.0d0
do j=1,p
left(j) = u-Uvec(i+1-j)
right(j) = Uvec(i+j)-u
saved = 0.0d0
do r=0,j-1
temp = nbasis(r)/(right(r+1)+left(j-r))
nbasis(r) = saved+right(r+1) * temp
saved = left(j-r) * temp
end do
nbasis(j) = saved
end do
return
end
subroutine basis_funs_and_derivatives(p,n,m,uvec,u,ders,d)
!
! Compute the basis functions and their derivatives at 'u' of the NURBS curve.
! Based on Eq. 2.10 and Algorithm A2.3 in The NURBS book, pp 72,
!
! The result is stored in the ders matrix, where ders is of
! size (d+1,p+1) and the derivative
! N'_i(u) = ders(1,i=span-p+j) where j = 0...p+1.
!
!
! Input:
! d - the degree of the derivation
! u - the parametric value
! p - degree of the curve
! span - the span for the basis functions
! Uvec - the knot vector on which the Basis functions must be computed
!
! Output:
! ders A (d+1,p+1) matrix containing the basis functions and derivatives up to order d.
!
implicit none
integer, intent(in) :: p
integer, intent(in) :: n
integer, intent(in) :: m
double precision, dimension(0:m), intent(in) :: uvec
double precision, intent(in) :: u
double precision, dimension(0:d,0:p), intent(out) :: ders
integer, intent(in) :: d
!
! Locals
!
double precision, dimension(0:p) :: left
double precision, dimension(0:p) :: right
double precision, dimension(0:p,0:p) :: ndu
double precision, dimension(0:1,0:p) :: a
double precision :: saved,temp,dd
integer :: j,r,span,s1,s2,rk,pk,j1,j2,k
call find_span(p,n,m,uvec,u,span)
ndu(0,0) = 1.0;
do j=1,p
left(j) = u-Uvec(span+1-j)
right(j) = Uvec(span+j)-u
saved = 0.0
do r=0,j-1
! Lower triangle
ndu(j,r) = right(r+1)+left(j-r)
temp = ndu(r,j-1)/ndu(j,r)
! Upper triangle
ndu(r,j) = saved+right(r+1) * temp
saved = left(j-r) * temp
end do
ndu(j,j) = saved
end do
ders(0,:) = ndu(:,p) ! Load the basis functions
! Compute the derivatives Eq. 2.10 in The NURBS book
do r=0,p
s1 = 0
s2 = 1 ! alternate rows in array a
a(0,0) = 1.0
! Compute the kth derivative
do k=1,d
dd = 0.0d0
rk = r-k
pk = p-k
if(r>=k) then
a(s2,0) = a(s1,0)/ndu(pk+1,rk)
dd = a(s2,0)*ndu(rk,pk)
endif
if(rk>=-1) then
j1 = 1
else
j1 = -rk
endif
if(r-1 <= pk) then
j2 = k-1
else
j2 = p-r
endif
do j=j1,j2
a(s2,j) = (a(s1,j)-a(s1,j-1))/ndu(pk+1,rk+j)
dd= dd + a(s2,j)*ndu(rk+j,pk)
end do
if(r<=pk) then
a(s2,k) = -a(s1,k-1)/ndu(pk+1,r)
dd = dd + a(s2,k)*ndu(r,pk)
endif
ders(k,r) = dd
j = s1
s1 = s2
s2 = j ! Switch rows
end do
end do
! Multiply through by the correct factors
r = p
do k=1,d
do j=0,p
ders(k,j) = ders(k,j) * r
end do
r = r * p-k
end do
! Strange scaling I have to do - check what bug:
ders(2,:) = ders(2,:)*p/(p+1.0d0)
return
end
subroutine basis_funs_cpt(p,n,m,uvec,ncpt,ucpt,nb_cpt)
!
! Computes the nonvanishing basis functions.
! Based on Eq. 2.5 and Cox-deBoor-Mansfield reccurence algorithm.
! Algorithm A2.2 of The NURBS book, pp70.
!
! Input: span - specifies the span at which basis function to compute,
! u - the parametric value,
! p - degree of the curve,
! U - the knot vector
! Output: N - the non-zero basis functions in the array N(0),...N(p) of length p+1.
! This array has to be allocated in the calling function.
!
! * Dopunjeno je tako da se unutar funkcije izracunava span
!
implicit none
integer, intent(in) :: p
integer, intent(in) :: n
integer, intent(in) :: m
double precision, dimension(0:m), intent(in) :: uvec
integer, intent(in) :: ncpt
double precision, dimension(ncpt), intent(in) :: ucpt
double precision, dimension(ncpt,0:n), intent(out) :: nb_cpt
!
! Locals
!
integer :: i,j,r,span,ic
double precision :: temp, saved
double precision, dimension(0:p) :: nbasis
double precision, dimension(0:2*(p+1)-1), target :: left
double precision, dimension(:), pointer :: right
right => left(p+1:2*(p+1)-1)
nb_cpt = 0.0d0
do ic=1,ncpt
call find_span(p,n,m,uvec,ucpt(ic),span)
i = span
nbasis(0) = 1.0d0
do j=1,p
left(j) = ucpt(ic)-Uvec(i+1-j)
right(j) = Uvec(i+j)-ucpt(ic)
saved = 0.0d0
do r=0,j-1
temp = nbasis(r)/(right(r+1)+left(j-r))
nbasis(r) = saved+right(r+1) * temp
saved = left(j-r) * temp
end do
nbasis(j) = saved
end do
nb_cpt(ic,i-p:i) = nbasis(:)
enddo
return
end
subroutine basis_funs_and_derivatives_cpt(p,n,m,uvec,ncpt,ucpt,ders_cpt,d)
!
! Compute the basis functions and their derivatives at 'u' of the NURBS curve.
! Based on Eq. 2.10 and Algorithm A2.3 in The NURBS book, pp 72,
!
! The result is stored in the ders matrix, where ders is of
! size (d+1,p+1) and the derivative
! N'_i(u) = ders(1,i=span-p+j) where j = 0...p+1.
!
!
! Input:
! d - the degree of the derivation
! u - the parametric value
! p - degree of the curve
! span - the span for the basis functions
! Uvec - the knot vector on which the Basis functions must be computed
!
! Output:
! ders A (d+1,p+1) matrix containing the basis functions and derivatives up to order d.
!
implicit none
integer, intent(in) :: p
integer, intent(in) :: n
integer, intent(in) :: m
double precision, dimension(0:m), intent(in) :: uvec
integer, intent(in) :: ncpt
double precision, dimension(ncpt), intent(in) :: ucpt
double precision, dimension(ncpt,0:d,0:n), intent(out) :: ders_cpt
integer, intent(in) :: d
!
! Locals
!
double precision, dimension(0:d,0:p) :: ders
double precision, dimension(0:p) :: left
double precision, dimension(0:p) :: right
double precision, dimension(0:p,0:p) :: ndu
double precision, dimension(0:1,0:p) :: a
double precision :: saved,temp,dd
integer :: j,r,span,s1,s2,rk,pk,j1,j2,k,ic
ders_cpt = 0.0d0
do ic=1,ncpt
call find_span(p,n,m,uvec,ucpt(ic),span)
ndu(0,0) = 1.0;
do j=1,p
left(j) = ucpt(ic)-Uvec(span+1-j)
right(j) = Uvec(span+j)-ucpt(ic)
saved = 0.0
do r=0,j-1
! Lower triangle
ndu(j,r) = right(r+1)+left(j-r)
temp = ndu(r,j-1)/ndu(j,r)
! Upper triangle
ndu(r,j) = saved+right(r+1) * temp
saved = left(j-r) * temp
end do
ndu(j,j) = saved
end do
ders(0,:) = ndu(:,p) ! Load the basis functions
! Compute the derivatives Eq. 2.10 in The NURBS book
do r=0,p
s1 = 0
s2 = 1 ! alternate rows in array a
a(0,0) = 1.0
! Compute the kth derivative
do k=1,d
dd = 0.0d0
rk = r-k
pk = p-k
if(r>=k) then
a(s2,0) = a(s1,0)/ndu(pk+1,rk)
dd = a(s2,0)*ndu(rk,pk)
endif
if(rk>=-1) then
j1 = 1
else
j1 = -rk
endif
if(r-1 <= pk) then
j2 = k-1
else
j2 = p-r
endif
do j=j1,j2
a(s2,j) = (a(s1,j)-a(s1,j-1))/ndu(pk+1,rk+j)
dd= dd + a(s2,j)*ndu(rk+j,pk)
end do
if(r<=pk) then
a(s2,k) = -a(s1,k-1)/ndu(pk+1,r)
dd = dd + a(s2,k)*ndu(r,pk)
endif
ders(k,r) = dd
j = s1
s1 = s2
s2 = j ! Switch rows
end do
end do
! Multiply through by the correct factors
r = p
do k=1,d
do j=0,p
ders(k,j) = ders(k,j) * r
end do
r = r * p-k
end do
! Strange scaling I have to do - check what bug:
ders(2,:) = ders(2,:)*p/(p+1.0d0)
! Store everything in an array
ders_cpt(ic,:,span-p:span) = ders(:,:)
end do
return
end