1 |
#include <math.h> |
---|
2 |
#include <stdio.h> |
---|
3 |
#include "mex.h" |
---|
4 |
#include "opnml_mex5_allocs.c" |
---|
5 |
|
---|
6 |
/* PROTOTYPES */ |
---|
7 |
void bandmsolve_c(int, |
---|
8 |
double **, |
---|
9 |
double *, |
---|
10 |
int, |
---|
11 |
int); |
---|
12 |
void curl(int nn, |
---|
13 |
int ne, |
---|
14 |
double *x, |
---|
15 |
double *y, |
---|
16 |
int *ele, |
---|
17 |
double *u, |
---|
18 |
double *v, |
---|
19 |
double *dv); |
---|
20 |
|
---|
21 |
/************************************************************ |
---|
22 |
|
---|
23 |
#### ## ##### ###### # # ## # # |
---|
24 |
# # # # # # # # # # # # |
---|
25 |
# # # # ##### # # # # # |
---|
26 |
# ### ###### # # # ## # ###### # |
---|
27 |
# # # # # # ## ## # # # |
---|
28 |
#### # # # ###### # # # # # |
---|
29 |
|
---|
30 |
************************************************************/ |
---|
31 |
|
---|
32 |
|
---|
33 |
void mexFunction(int nlhs, |
---|
34 |
mxArray *plhs[], |
---|
35 |
int nrhs, |
---|
36 |
const mxArray *prhs[]) |
---|
37 |
{ |
---|
38 |
int cnt,*ele,i,j,nn,ne; |
---|
39 |
double *x, *y, *u, *v; |
---|
40 |
double *dele; |
---|
41 |
double *vt; |
---|
42 |
|
---|
43 |
/* ---- curlmex will be called as : |
---|
44 |
vt=curlmex(x,y,ele,u,v); |
---|
45 |
------------------------------- */ |
---|
46 |
|
---|
47 |
/* ---- check I/O arguments ----------------------------------------- */ |
---|
48 |
if (nrhs != 5) |
---|
49 |
mexErrMsgTxt("curlmex5 requires 5 input arguments."); |
---|
50 |
else if (nlhs != 1) |
---|
51 |
mexErrMsgTxt("curlmex5 requires 1 output arguments."); |
---|
52 |
|
---|
53 |
/* ---- dereference input arrays ------------------------------------ */ |
---|
54 |
x=mxGetPr(prhs[0]); |
---|
55 |
y=mxGetPr(prhs[1]); |
---|
56 |
dele=mxGetPr(prhs[2]); |
---|
57 |
u=mxGetPr(prhs[3]); |
---|
58 |
v=mxGetPr(prhs[4]); |
---|
59 |
nn=mxGetM(prhs[0]); |
---|
60 |
ne=mxGetM(prhs[2]); |
---|
61 |
|
---|
62 |
/* ---- allocate space for int representation of dele & |
---|
63 |
convert double element representation to int & |
---|
64 |
shift node numbers toward 0 by 1 for proper indexing -------- */ |
---|
65 |
ele=(int *)mxIvector(0,3*ne); |
---|
66 |
for (i=0;i<3*ne;i++){ |
---|
67 |
ele[i]=(int)dele[i]; |
---|
68 |
ele[i]=ele[i]-1; |
---|
69 |
} |
---|
70 |
|
---|
71 |
/* ---- allocate space for divergence list dv following |
---|
72 |
NRC allocation style ------------- */ |
---|
73 |
vt = (double *) mxDvector(0,nn-1); |
---|
74 |
|
---|
75 |
/* ---- call curl routine ------------------------------------ */ |
---|
76 |
curl(nn,ne,x,y,ele,u,v,vt); |
---|
77 |
|
---|
78 |
/* ---- Set elements of return matrix, pointed to by plhs[0] -------- */ |
---|
79 |
plhs[0]=mxCreateDoubleMatrix(nn,1,mxREAL); |
---|
80 |
mxSetPr(plhs[0],vt); |
---|
81 |
|
---|
82 |
/* --- No need to free memory allocated with "mxCalloc"; MATLAB |
---|
83 |
does this automatically. The CMEX allocation functions in |
---|
84 |
"opnml_allocs.c" use mxCalloc. ----------------------------------- */ |
---|
85 |
|
---|
86 |
return; |
---|
87 |
} |
---|
88 |
|
---|
89 |
/*---------------------------------------------------------------------- |
---|
90 |
|
---|
91 |
----------------------------------------------------------------------*/ |
---|
92 |
void curl(int nn,int ne, |
---|
93 |
double *x, |
---|
94 |
double *y, |
---|
95 |
int *ele, |
---|
96 |
double *u, |
---|
97 |
double *v, |
---|
98 |
double *vt) |
---|
99 |
#define ELE(i,j,m) ele[i+m*j] |
---|
100 |
|
---|
101 |
{ |
---|
102 |
double *ar,**av,**dx,**dy; |
---|
103 |
double area6,area12; |
---|
104 |
int i,j,l,m,n; |
---|
105 |
int nbw=0, nh=0, nhm=0, n0 ,n1, n2, l01, l02, l12; |
---|
106 |
|
---|
107 |
/* ---- Compute half bandwidth -------------------------------------- */ |
---|
108 |
for (i=0;i<ne;i++){ |
---|
109 |
l01=abs(ELE(i,0,ne)-ELE(i,1,ne)); |
---|
110 |
l02=abs(ELE(i,0,ne)-ELE(i,2,ne)); |
---|
111 |
l12=abs(ELE(i,1,ne)-ELE(i,2,ne)); |
---|
112 |
nhm=IMAX(l01,IMAX(l02,l12)); |
---|
113 |
nh=IMAX(nh,nhm); |
---|
114 |
} |
---|
115 |
nbw=2*nh+1; |
---|
116 |
fprintf(stderr,"BW = %d\n",nbw); |
---|
117 |
|
---|
118 |
/* ---- ALLOCATE space for dx, dy, ar, av and dv -------------------- */ |
---|
119 |
ar = (double *) mxDvector(0,ne-1); |
---|
120 |
av = (double **) mxDmatrix(0,nbw-1,0,nn-1); |
---|
121 |
dx = (double **) mxDmatrix(0,2,0,ne-1); |
---|
122 |
dy = (double **) mxDmatrix(0,2,0,ne-1); |
---|
123 |
puts("Local Arrays Allocated"); |
---|
124 |
|
---|
125 |
/* ---- Compute dx,dy and element areas ----------------------------- */ |
---|
126 |
for(l=0;l<ne;l++){ |
---|
127 |
dx[0][l]=x[ELE(l,1,ne)]-x[ELE(l,2,ne)]; |
---|
128 |
dx[1][l]=x[ELE(l,2,ne)]-x[ELE(l,0,ne)]; |
---|
129 |
dx[2][l]=x[ELE(l,0,ne)]-x[ELE(l,1,ne)]; |
---|
130 |
dy[0][l]=y[ELE(l,1,ne)]-y[ELE(l,2,ne)]; |
---|
131 |
dy[1][l]=y[ELE(l,2,ne)]-y[ELE(l,0,ne)]; |
---|
132 |
dy[2][l]=y[ELE(l,0,ne)]-y[ELE(l,1,ne)]; |
---|
133 |
ar[l]=.5*(x[ELE(l,0,ne)]*dy[0][l]+ |
---|
134 |
x[ELE(l,1,ne)]*dy[1][l]+ |
---|
135 |
x[ELE(l,2,ne)]*dy[2][l]); |
---|
136 |
} |
---|
137 |
puts("Element areas and edge lengths computed"); |
---|
138 |
|
---|
139 |
/* ---- Assemble LHS (av) ------------------------------------------- */ |
---|
140 |
for(l=0;l<ne;l++){ |
---|
141 |
area6=ar[l]/6.; |
---|
142 |
area12=ar[l]/12.; |
---|
143 |
for(j=0;j<3;j++){ |
---|
144 |
m=ELE(l,j,ne); |
---|
145 |
for(i=0;i<3;i++){ |
---|
146 |
n=nh+ELE(l,i,ne)-ELE(l,j,ne); |
---|
147 |
if(i==j) av[n][m]+=area6; |
---|
148 |
else av[n][m]+=area12; |
---|
149 |
} |
---|
150 |
} |
---|
151 |
} |
---|
152 |
puts("LHS (av) assembled"); |
---|
153 |
|
---|
154 |
/* ---- Assemble RHS (vt) ------------------------------------------- */ |
---|
155 |
for(l=0;l<ne;l++){ |
---|
156 |
for(j=0;j<3;j++){ |
---|
157 |
m=ELE(l,j,ne); |
---|
158 |
for(i=0;i<3;i++) |
---|
159 |
vt[m]+=v[ELE(l,i,ne)]*dy[i][l]/6.+u[ELE(l,i,ne)]*dx[i][l]/6.; |
---|
160 |
} |
---|
161 |
} |
---|
162 |
puts("RHS (vt) assembled"); |
---|
163 |
|
---|
164 |
/* ---- Call banded Matrix solver to triangularize av --------------- */ |
---|
165 |
bandmsolve_c(0,av,vt,nn,nh); |
---|
166 |
puts("Left side triangularized"); |
---|
167 |
|
---|
168 |
/* ---- Call banded Matrix solver to solve for dv ------------------- */ |
---|
169 |
bandmsolve_c(1,av,vt,nn,nh); |
---|
170 |
puts("Curl computed"); |
---|
171 |
|
---|
172 |
return; |
---|
173 |
} |
---|
174 |
|
---|