This repository has been archived by the owner on May 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathlinear_equations.c
540 lines (425 loc) · 14.3 KB
/
linear_equations.c
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*************************************************************************************************************
The MIT License
Copyright (C) 2006-2019 David Dubbeldam, Sofia Calero, Thijs Vlugt, Donald E. Ellis, and Randall Q. Snurr.
D.Dubbeldam@uva.nl http://www.uva.nl/profiel/d/u/d.dubbeldam/d.dubbeldam.html
scaldia@upo.es http://www.upo.es/raspa/
t.j.h.vlugt@tudelft.nl http://homepage.tudelft.nl/v9k6y
don-ellis@northwestern.edu http://dvworld.northwestern.edu/
snurr@northwestern.edu http://zeolites.cqe.northwestern.edu/
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <float.h>
#include "linear_equations.h"
#define SQR(x) ((x)*(x))
int fzyfsy(int n,REAL * md,REAL *ud1, REAL *ud2,
REAL *rs,REAL *x,REAL *cmd,REAL *cld_1,
REAL *cld_2,REAL *cld_l2,REAL *cld_l1,
REAL *bud_1,REAL *bud_2,REAL *brs_2,REAL *brs_1);
int fzyfsz(int n,REAL *md,REAL *ud1,REAL *ud2,REAL *cmd,
REAL *cld_1,REAL *cld_2,REAL *cld_l2,REAL *cld_l1,
REAL *bud_1,REAL *bud_2,REAL *brs_2, REAL *brs_1);
int fzyfsl(int n,REAL *rs,REAL *x,REAL *cmd,REAL *cld_1,
REAL *cld_2,REAL *cld_l2,REAL *cld_l1,REAL *bud_1,
REAL *bud_2,REAL *brs_2,REAL *brs_1);
int diag5dec(int n,REAL *ld2, REAL *ld1,REAL *d,
REAL *ud1,REAL *ud2);
int diag5sol(int n,REAL *ld2,REAL *ld1,REAL *d,
REAL *ud1,REAL *ud2,REAL *b);
int diag5pddec(int n,REAL *d,REAL *ud1,REAL *ud2);
int diag5pdsol(int n,REAL *d,REAL *ud1,REAL *ud2,REAL *b);
int diag5pd(int mod,int n,REAL *d,REAL *ud1,
REAL *ud2,REAL *b);
int diag5(int mod,int n,REAL *ld2,REAL *ld1, REAL *d,
REAL *ud1,REAL *ud2,REAL *b);
// used lapack routines
int dgetrf_(int *m,int *n,REAL *a,int *lda,int *ipiv,int *info);
int dgetri_(int *n,REAL *a,int *lda,int *ipiv,REAL *work,int *lwork,int *info);
int dgesv_(int *n,int *nhrs,REAL *a,int *lda,int *ipiv,REAL *b,int *ldb,int *info);
int Inverse(REAL_MATRIX c)
{
int arg1,arg2;
int *ipiv;
REAL *workspace;
ipiv=(int*)calloc(c.n,sizeof(int));
workspace=(REAL*)calloc(8*c.n,sizeof(REAL));
arg1=c.n;
arg2=8*c.n;
//dgetrf_(&arg1,&arg1,c.element[0],&arg1,ipiv,&ok);
//dgetri_(&arg1,c.element[0],&arg1,ipiv,workspace,&arg2,&ok);
free(workspace);
free(ipiv);
return 0;
}
int SolveLinearSystem(REAL_FORTRAN_MATRIX *a,REAL_FORTRAN_MATRIX *b)
{
int *ipiv;
ipiv=(int*)calloc(a->n,sizeof(int));
//dgesv_(&a->n,&b->n,a->element,&a->m,ipiv,b->element,&b->m,&ok);
free(ipiv);
return 0;
}
// Tridiagonal linear systems
int trdiag(int n,REAL *lower,REAL *diag,REAL *upper,REAL *b,int rep)
{
int i;
if(n < 2) return (1); /* n at least 2 */
if (lower == NULL || diag == NULL || upper == NULL ||
b == NULL) return (1);
if (rep == 0) /* for rep = 0, determine*/
{ /* LU factorization */
for (i = 1; i < n; i++)
{
if (fabs(diag[i-1])<(REAL)DBL_EPSILON) /* if one diag[i] = 0 */
return (2); /* we have no LU fact. */
lower[i]/=diag[i-1];
diag[i]-=lower[i]*upper[i-1];
}
}
if (fabs(diag[n-1])<(REAL)DBL_EPSILON) return (2);
for (i = 1; i < n; i++) /* update b */
b[i] -= lower[i] * b[i-1];
b[n-1] /= diag[n-1]; /* backsubsitute */
for (i = n-2; i >= 0; i--)
b[i] = ( b[i] - upper[i] * b[i+1] ) / diag[i];
return (0);
}
int tzdiag(int n,REAL *lower,REAL *diag,REAL *upper,
REAL *lowrow,REAL *ricol,REAL *b,int rep)
{
REAL tmp;
int i;
if (n<3) return (1);
if (lower==NULL||diag==NULL||upper==NULL||
lowrow==NULL||ricol==NULL) return (1);
if(rep==0) /* if rep = 0, */
{ /* decompose matrix */
lower[0]=upper[n-1]=0.0;
if (fabs(diag[0])<(REAL)DBL_EPSILON) return (2);
/* If a diagonal entry is */
tmp=1.0/diag[0]; /* too close to zero, stop */
upper[0]*=tmp;
ricol[0]*=tmp;
for(i=1;i<n-2;i++)
{
diag[i]-=lower[i]*upper[i-1];
if(fabs(diag[i])<(REAL)DBL_EPSILON) return (2);
tmp=1.0/diag[i];
upper[i]*=tmp;
ricol[i]=-lower[i]*ricol[i-1]*tmp;
}
diag[n-2]-=lower[n-2]*upper[n-3];
if (fabs(diag[n-2])<(REAL)DBL_EPSILON) return (2);
for (i=1;i<n-2;i++)
lowrow[i]=-lowrow[i-1]*upper[i-1];
lower[n-1]-=lowrow[n-3]*upper[n-3];
upper[n-2]=(upper[n-2]-lower[n-2]*ricol[n-3])/diag[n-2];
for(tmp=0.0,i=0;i<n-2;i++)
tmp-=lowrow[i]*ricol[i];
diag[n-1]+=tmp-lower[n-1]*upper[n-2];
if (fabs(diag[n-1])<(REAL)DBL_EPSILON) return (2);
} /* end if ( rep == 0 ) */
b[0]/=diag[0]; /* update b */
for(i=1;i<n-1;i++)
b[i]=(b[i]-b[i-1]*lower[i])/diag[i];
for(tmp=0.0,i=0;i<n-2;i++)
tmp-=lowrow[i]*b[i];
b[n-1]=(b[n-1]+tmp-lower[n-1]*b[n-2])/diag[n-1];
b[n-2]-=b[n-1]*upper[n-2]; /* back substitute */
for(i=n-3;i>=0;i--)
b[i]-=upper[i]*b[i+1]+ricol[i]*b[n-1];
return (0);
}
int fzyfsy(int n,REAL * md,REAL *ud1, REAL *ud2,
REAL *rs,REAL *x,REAL *cmd,REAL *cld_1,
REAL *cld_2,REAL *cld_l2,REAL *cld_l1,
REAL *bud_1,REAL *bud_2,REAL *brs_2,REAL *brs_1)
{
int error;
if(n<6) return (2);
// Factor system matrix into C * B for triangular matrices C, B
error=fzyfsz(n,md,ud1,ud2,cmd,cld_1,cld_2,cld_l2,cld_l1,
bud_1,bud_2,brs_2,brs_1);
if (!error) /* factorization without error */
fzyfsl(n,rs,x,cmd,cld_1,cld_2,cld_l2,cld_l1,bud_1,bud_2,brs_2,brs_1);
return (0);
}
int fzyfsz(int n,REAL *md,REAL *ud1,REAL *ud2,REAL *cmd,
REAL *cld_1,REAL *cld_2,REAL *cld_l2,REAL *cld_l1,
REAL *bud_1,REAL *bud_2,REAL *brs_2, REAL *brs_1)
{
int i,j,k;
REAL h_var_1,h_var_2,h_var_3;
// Factor A = C * B.
cmd[1]=md[1];
if(cmd[1]<=4.0*DBL_EPSILON) return (1);
bud_1[1]=ud1[1]/cmd[1];
brs_2[1]=ud2[n-1]/cmd[1];
brs_1[1]=ud1[n]/cmd[1];
cld_1[2]=ud1[1];
cmd[2]=md[2]-cld_1[2]*bud_1[1];
if(cmd[2]<=4.0*DBL_EPSILON) return (1);
brs_2[2]=-(brs_2[1]*cld_1[2])/cmd[2];
brs_1[2]=(ud2[n]-cld_1[2]*brs_1[1])/cmd[2];
for(i=3;i<=n-2;++i)
{
j=i-2;
k=i-1;
cld_2[i]=ud2[i-2];
bud_2[j]=ud2[j]/cmd[j];
bud_1[k]=(ud1[k]-cld_1[k]*bud_2[j])/cmd[k];
cld_1[i]=ud1[i-1]-cld_2[i]*bud_1[j];
cmd[i]=md[i]-cld_1[i]*bud_1[k]-cld_2[i]*bud_2[j];
if(cmd[i]<=4.0*DBL_EPSILON) return (1);
}
for(i=3;i<=n-4;++i)
brs_2[i]=-(cld_2[i]*brs_2[i-2]+cld_1[i]*brs_2[i-1])/cmd[i];
for(i=3;i<=n-3;++i)
brs_1[i]=-(cld_2[i]*brs_1[i-2]+cld_1[i]*brs_1[i-1])/cmd[i];
bud_2[n-3]=(ud2[n-3]-cld_1[n-3]*brs_2[n-4]-cld_2[n-3]*brs_2[n-5])/cmd[n-3];
bud_2[n-2]=(ud2[n-2]-cld_1[n-2]*brs_1[n-3]-cld_2[n-2]*brs_1[n-4])/cmd[n-2];
bud_1[n-2]=(ud1[n-2]-cld_1[n-2]*bud_2[n-3]-cld_2[n-2]*brs_2[n-4])/cmd[n-2];
cld_l2[1]=ud2[n-1];
cld_l2[2]=-cld_l2[1]*bud_1[1];
for (i=3;i<=n-4;++i)
cld_l2[i]=-(cld_l2[i-2]*bud_2[i-2]+cld_l2[i-1]*bud_1[i-1]);
cld_l1[1]=ud1[n];
cld_l1[2]=ud2[n]-cld_l1[1]*bud_1[1];
for (i=3;i<=n-3;++i)
cld_l1[i]=-(cld_l1[i-2]*bud_2[i-2]+cld_l1[i-1]*bud_1[i-1]);
cld_2[n-1]=ud2[n-3]-(cld_l2[n-5]*bud_2[n-5]+cld_l2[n-4]*bud_1[n-4]);
cld_2[n]=ud2[n-2]-(cld_l1[n-4]*bud_2[n-4]+ cld_l1[n-3]*bud_1[n-3]);
cld_1[n-1]=ud1[n-2]-(cld_l2[n-4]*bud_2[n-4]+cld_2[n-1]*bud_1[n-3]);
h_var_1=h_var_2=h_var_3=0.0;
for(i=1;i<=n-4;++i)
{
h_var_1+=cld_l1[i]*brs_2[i];
h_var_2+=cld_l2[i]*brs_2[i];
h_var_3+=cld_l2[i]*brs_1[i];
}
cld_1[n]=ud1[n-1]-h_var_1-cld_l1[n-3]*bud_2[n-3]-cld_2[n]*bud_1[n-2];
cmd[n-1]=md[n-1]-h_var_2-cld_2[n-1]*bud_2[n-3]-cld_1[n-1]*bud_1[n-2];
if(cmd[n-1]<=4.0*DBL_EPSILON) return (1);
bud_1[n-1]=(ud1[n-1]-h_var_3-cld_2[n-1]*brs_1[n-3]
-cld_1[n-1]*bud_2[n-2])/cmd[n-1];
for(h_var_1=0.0,i=1;i<=n-3;++i)
h_var_1+=cld_l1[i]*brs_1[i];
cmd[n]=md[n]-h_var_1-cld_2[n]*bud_2[n-2]-cld_1[n]*bud_1[n-1];
if(cmd[n]<=4.0*DBL_EPSILON) return (1);
return (0);
}
int fzyfsl(int n,REAL *rs,REAL *x,REAL *cmd,REAL *cld_1,
REAL *cld_2,REAL *cld_l2,REAL *cld_l1,REAL *bud_1,
REAL *bud_2,REAL *brs_2,REAL *brs_1)
{
int i;
REAL h_var_1;
// Solve system by updating right hand side and backsubstitution
x[1]=rs[1]/cmd[1]; /* update right hand side */
x[2]=(rs[2]-x[1]*cld_1[2])/cmd[2];
for(i=3;i<=n-2;++i)
x[i]=(rs[i]-x[i-2]*cld_2[i]-x[i-1]*cld_1[i])/cmd[i];
for(h_var_1=0.0,i=1;i<=n-4;++i)
h_var_1+=x[i]*cld_l2[i];
x[n-1]=(rs[n-1]-h_var_1-x[n-3]*cld_2[n-1]
- x[n-2]*cld_1[n-1])/cmd[n-1];
for(h_var_1=0.0,i=1;i<=n-3;++i)
h_var_1+=x[i]*cld_l1[i];
x[n]=(rs[n]-h_var_1-x[n-2]*cld_2[n]-x[n-1]*cld_1[n])/cmd[n];
x[n-1]-=bud_1[n-1]*x[n]; /* back substitute */
x[n-2]-=(bud_1[n-2]*x[n-1]+bud_2[n-2]*x[n]);
x[n-3]-=(bud_1[n-3]*x[n-2]+bud_2[n-3]*x[n-1]+brs_1[n-3]*x[n]);
for(i=n-4;i>=1;--i)
x[i]-=(bud_1[i]*x[i+1]+bud_2[i]*x[i+2]+brs_2[i]*x[n-1]+brs_1[i]*x[n]);
return 0;
}
// 5 diagonal linear systems
int diag5(int mod,int n,REAL *ld2,REAL *ld1, REAL *d,
REAL *ud1,REAL *ud2,REAL *b)
{
int rc;
switch (mod)
{
case 0:
rc=diag5dec(n,ld2,ld1,d,ud1,ud2);
if (rc==0)
return (diag5sol(n,ld2,ld1,d,ud1,ud2,b));
else
return (rc);
case 1:
return (diag5dec(n,ld2,ld1,d,ud1,ud2));
case 2:
return (diag5sol(n,ld2,ld1,d,ud1,ud2,b));
}
return (3);
}
int diag5dec(int n,REAL *ld2, REAL *ld1,REAL *d,
REAL *ud1,REAL *ud2)
{
register int i;
REAL row,D,ud1i,ud2i;
if (n<3) return (1);
if (ld2==NULL||ld1==NULL||d==NULL||ud1==NULL||
ud2==NULL) return (1);
row=fabs(d[0])+fabs(ud1[0])+fabs(ud2[0]);
if (row==0.0) return 2;
D=1.0/row;
if (fabs(d[0])*D<=4.0*DBL_EPSILON) return 2;
ud1[0]/=d[0];
ud2[0]/=d[0];
row=fabs(ld1[1])+fabs(d[1])+fabs(ud1[1])+fabs(ud2[1]);
if (row==0.0) return 2;
D=1.0/row;
d[1]-=ld1[1]*ud1[0];
if (fabs(d[1])*D<=4.0*DBL_EPSILON) return 2;
ud1[1]=(ud1[1]-ld1[1]*ud2[0])/d[1];
ud2[1]/=d[1];
ud1i=ud1[2];
ud2i=ud2[2];
for(i=2;i<n;i++)
{
row=fabs(ld2[i])+fabs(ld1[i])+fabs(d[i])+fabs(ud1i)+fabs(ud2i);
if (row==0.0) return 2;
D=1.0/row;
ld1[i]-=ld2[i]*ud1[i-2];
d[i]-=(ld2[i]*ud2[i-2]+ld1[i]*ud1[i-1]);
if(fabs(d[i])*D<=4.0*DBL_EPSILON) return 2;
if (i<n-1)
ud1[i]=(ud1[i]-ld1[i]*ud2[i-1])/d[i];
if (i<n-2)
ud2[i]/=d[i],
ud1i=ud1[i+1];
else
ud1i=0.0;
if (i<n-3)
ud2i=ud2[i+1];
else
ud2i=0.0;
}
return (0);
}
int diag5sol(int n,REAL *ld2,REAL *ld1,REAL *d,
REAL *ud1,REAL *ud2,REAL *b)
{
register int i;
if (n<3) return (1);
if (ld2==NULL||ld1==NULL||d==NULL||ud1==NULL||ud2==NULL||b==NULL) return (1);
if (fabs(d[0])<(REAL)DBL_EPSILON) return (2);
b[0]/=d[0];
if (fabs(d[1])<(REAL)DBL_EPSILON) return (2);
b[1]=(b[1]-ld1[1]*b[0])/d[1];
for(i=2;i<n;i++)
{
if (fabs(d[i])<(REAL)DBL_EPSILON) return (2);
b[i]=(b[i]-ld2[i]*b[i-2]-ld1[i]*b[i-1])/d[i];
}
b[n-2]-=ud1[n-2]*b[n-1];
for(i=n-3;i>=0;i--)
b[i]-=(ud1[i]*b[i+1]+ud2[i]*b[i+2]);
return (0);
}
int diag5pd(int mod,int n,REAL *d,REAL *ud1,
REAL *ud2,REAL *b)
{
int rc;
if(n<3) return (1);
switch (mod)
{
case 0:
rc = diag5pddec (n, d, ud1, ud2);
if (rc == 0)
return (diag5pdsol (n, d, ud1, ud2, b));
else
return (rc);
case 1:
return (diag5pddec(n,d,ud1,ud2));
case 2:
return (diag5pdsol(n,d,ud1,ud2,b));
}
return (3);
}
int diag5pddec(int n,REAL *d,REAL *ud1,REAL *ud2)
{
register int i;
REAL e_1,e_2,tmp,sum;
if (n<3) return (1);
if (d==NULL||ud1==NULL||ud2==NULL) return (1);
ud1[n-1]=0.0;
ud2[n-1]=0.0;
ud2[n-2]=0.0;
sum=fabs(d[0])+fabs(ud1[0])+fabs(ud2[0]);
if(sum==0.0) return (3);
if(d[0]<sum*(REAL)DBL_EPSILON) return (2);
tmp=ud1[0];
ud1[0]/=d[0];
e_2=ud2[0];
ud2[0]/=d[0];
sum=fabs(tmp)+fabs(d[1])+fabs(ud1[1])+fabs(ud2[1]);
if(sum==0.0) return (2);
d[1]-=tmp*ud1[0];
if(fabs(d[1])<sum*(REAL)DBL_EPSILON) return (2);
tmp=ud1[1];
ud1[1]=(ud1[1]-e_2*ud1[0])/d[1];
e_1=ud2[1];
ud2[1]/=d[1];
for(i=2;i<n;i++)
{
sum=fabs(e_2)+fabs(tmp)+fabs(d[i])+fabs(ud1[i])+fabs(ud2[i]);
if(sum==0.0) return (2);
d[i]-=e_2*ud2[i-2]+d[i-1]*SQR(ud1[i-1]);
if(fabs(d[i])<sum*(REAL)DBL_EPSILON) return (2);
if (i<n-1)
{
tmp=ud1[i];
ud1[i]=(ud1[i]-e_1*ud1[i-1])/d[i];
}
if(i<n-2)
{
e_2=e_1;
e_1=ud2[i];
ud2[i]/=d[i];
}
}
return (0);
}
int diag5pdsol(int n,REAL *d,REAL *ud1,REAL *ud2,REAL *b)
{
register int i;
if(n<3) return (1);
if(d==NULL||ud1==NULL||ud2==NULL||b==NULL) return (1);
if((REAL)fabs(d[1])<(REAL)DBL_EPSILON) return (2);
b[1]-=ud1[0]*b[0];
for(i=2;i<n;i++)
b[i]-=ud1[i-1]*b[i-1]+ud2[i-2]*b[i-2];
for(i=0;i<n;i++)
{
if(fabs(d[i])<(REAL)DBL_EPSILON) return (2);
b[i]/=d[i];
}
b[n-2]-=ud1[n-2]*b[n-1];
for(i=n-3;i>=0;i--)
b[i]-=(ud1[i]*b[i+1]+ud2[i]*b[i+2]);
return (0);
}