NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/opnml/MEX/findelemex5.c

Revision 495 (checked in by cbc, 12 years ago)

Initial import of Stark code.

Line 
1 #include <math.h>
2 #include <stdio.h>
3 #include "mex.h"
4 #include "opnml_mex5_allocs.c"
5
6 /************************************************************
7
8   ####     ##     #####  ######  #    #    ##     #   #
9  #    #   #  #      #    #       #    #   #  #     # #
10  #       #    #     #    #####   #    #  #    #     #
11  #  ###  ######     #    #       # ## #  ######     #
12  #    #  #    #     #    #       ##  ##  #    #     #
13   ####   #    #     #    ######  #    #  #    #     #
14
15 ************************************************************/
16
17 /* ---- AA,BB,TT are defined to perform the following array
18         element extractions     AA(i,j,m) AA[i+m*j] ---------------- */
19 #define AA(i,j,m) A[i+m*j]
20 #define BB(i,j,m) B[i+m*j]
21 #define TT(i,j,m) T[i+m*j]
22
23 void mexFunction(int            nlhs,
24                  mxArray       *plhs[],
25                  int            nrhs,
26                  const mxArray *prhs[])
27 {
28
29 /* ---- findelemex will be called as :
30         j_el=findelemex(xp,yp,AR,A,B,T); ---------------------------- */
31 /* ---- xp,yp are NOT nodal coordinates; they are the points we are
32         finding elements for.  Nodal coordinates have already been
33         accounted for in A,B,T                                      ----- */
34
35    int ip,j,np,nl,nh,ne;
36    double *xp, *yp;
37    double *AR,*A,*B,*T;
38    double *fnd;
39    double NaN=mxGetNaN();
40    double fac,S1,S2,S3,ONE,ZERO;
41    double tol=1.e-4;
42
43      
44 /* ---- check I/O arguments ----------------------------------------- */
45    if (nrhs != 6)
46       mexErrMsgTxt("findelemex requires 6 input arguments.");
47    else if (nlhs != 1)
48       mexErrMsgTxt("findelemex requires 1 output arguments.");
49
50 /* ---- dereference input arrays ------------------------------------ */
51    xp  =mxGetPr(prhs[0]);
52    yp  =mxGetPr(prhs[1]);
53    AR  =mxGetPr(prhs[2]);
54    A   =mxGetPr(prhs[3]);
55    B   =mxGetPr(prhs[4]);
56    T   =mxGetPr(prhs[5]);
57    
58    np=mxGetM(prhs[0]);
59    ne=mxGetM(prhs[2]);   
60    
61 /* ---- allocate space for list containing element numbers following NRC
62         allocation style
63         double *mxDvector(int nl,int nh)
64         fnd= (double *) mxDvector(0,np); ---------------------------- */
65    fnd= (double *) mxDvector(0,np);
66    for (ip=0;ip<np;ip++)fnd[ip]=-1.;
67    ONE=1.+tol;
68    ZERO=0.-tol;
69    for (j=0;j<ne;j++){
70       for (ip=0;ip<np;ip++){ 
71          if(fnd[ip]<(double)0){
72             fac=.5/AR[j];         
73             S1=(TT(j,0,ne)+BB(j,0,ne)*xp[ip]+AA(j,0,ne)*yp[ip])*fac;
74             if (S1>ONE|S1<ZERO)goto l20;
75             S2=(TT(j,1,ne)+BB(j,1,ne)*xp[ip]+AA(j,1,ne)*yp[ip])*fac;
76             if (S2>ONE|S2<ZERO)goto l20;
77             S3=(TT(j,2,ne)+BB(j,2,ne)*xp[ip]+AA(j,2,ne)*yp[ip])*fac;
78             if (S3>ONE|S3<ZERO)goto l20;         
79             fnd[ip]=(double)(j+1);           
80          }
81        l20: continue;
82        }
83     }
84     for (ip=0;ip<np;ip++) if(fnd[ip]<(double)0)fnd[ip]=NaN;
85                
86 /* ---- Set elements of return matrix, pointed to by plhs[0] -------- */
87    plhs[0]=mxCreateDoubleMatrix(np,1,mxREAL);
88    mxSetPr(plhs[0],fnd);
89
90 /* ---- No need to free memory allocated with "mxCalloc"; MATLAB
91    does this automatically.  The CMEX allocation functions in
92    "opnml_allocs.c" use mxCalloc. ----------------------------------- */
93    return;   
94 }
Note: See TracBrowser for help on using the browser.