| 1 | /* \file WriteData.c:
|
|---|
| 2 | * \brief: general interface for writing data
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | #ifdef HAVE_CONFIG_H
|
|---|
| 6 | #include <config.h>
|
|---|
| 7 | #else
|
|---|
| 8 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
|---|
| 9 | #endif
|
|---|
| 10 |
|
|---|
| 11 | #include <mex.h>
|
|---|
| 12 | #include "../../include/include.h"
|
|---|
| 13 | #include "../../shared/shared.h"
|
|---|
| 14 | #include "./matlabio.h"
|
|---|
| 15 |
|
|---|
| 16 | /*Primitive data types*/
|
|---|
| 17 | /*FUNCTION WriteData(mxArray** pdataref,double* matrix, int M,int N){{{*/
|
|---|
| 18 | void WriteData(mxArray** pdataref,double* matrix, int M,int N){
|
|---|
| 19 |
|
|---|
| 20 | mxArray *dataref = NULL;
|
|---|
| 21 | double *tmatrix = NULL;
|
|---|
| 22 |
|
|---|
| 23 | if(matrix){
|
|---|
| 24 | /*create the matlab matrixwith Matlab's memory manager */
|
|---|
| 25 | tmatrix=(double*)mxMalloc(M*N*sizeof(double));
|
|---|
| 26 | for(int i=0;i<M;i++){
|
|---|
| 27 | for(int j=0;j<N;j++){
|
|---|
| 28 | tmatrix[j*M+i]=matrix[i*N+j];
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 32 | mxSetM(dataref,(mwSize)M);
|
|---|
| 33 | mxSetN(dataref,(mwSize)N);
|
|---|
| 34 | mxSetPr(dataref,(double*)tmatrix);
|
|---|
| 35 | }
|
|---|
| 36 | else{
|
|---|
| 37 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 38 | }
|
|---|
| 39 | *pdataref=dataref;
|
|---|
| 40 | }
|
|---|
| 41 | /*}}}*/
|
|---|
| 42 | /*FUNCTION WriteData(mxArray** pdataref,int* matrix, int M,int N){{{*/
|
|---|
| 43 | void WriteData(mxArray** pdataref,int* matrix, int M,int N){
|
|---|
| 44 |
|
|---|
| 45 | mxArray* dataref = NULL;
|
|---|
| 46 | double* tmatrix = NULL;
|
|---|
| 47 |
|
|---|
| 48 | if(matrix){
|
|---|
| 49 | /*convert to double matrix using Matlab's memory manager*/
|
|---|
| 50 | double* tmatrix=(double*)mxMalloc(M*N*sizeof(double));
|
|---|
| 51 | for(int i=0;i<M;i++){
|
|---|
| 52 | for(int j=0;j<N;j++){
|
|---|
| 53 | tmatrix[j*M+i]=(double)matrix[i*N+j];
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 57 | mxSetM(dataref,(mwSize)M);
|
|---|
| 58 | mxSetN(dataref,(mwSize)N);
|
|---|
| 59 | mxSetPr(dataref,(double*)tmatrix);
|
|---|
| 60 | }
|
|---|
| 61 | else{
|
|---|
| 62 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 63 | }
|
|---|
| 64 | *pdataref=dataref;
|
|---|
| 65 | }
|
|---|
| 66 | /*}}}*/
|
|---|
| 67 | /*FUNCTION WriteData(mxArray** pdataref,double* vector, int M){{{*/
|
|---|
| 68 | void WriteData(mxArray** pdataref,double* vector, int M){
|
|---|
| 69 |
|
|---|
| 70 | mxArray* dataref = NULL;
|
|---|
| 71 | double* vector_matlab = NULL;
|
|---|
| 72 |
|
|---|
| 73 | if(vector){
|
|---|
| 74 |
|
|---|
| 75 | /*create the matlab vector with Matlab's memory manager */
|
|---|
| 76 | vector_matlab=(double*)mxMalloc(M*sizeof(double));
|
|---|
| 77 | for(int i=0;i<M;i++) vector_matlab[i]=vector[i];
|
|---|
| 78 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 79 | mxSetM(dataref,(mwSize)M);
|
|---|
| 80 | mxSetN(dataref,(mwSize)1);
|
|---|
| 81 | mxSetPr(dataref,vector_matlab);
|
|---|
| 82 | }
|
|---|
| 83 | else{
|
|---|
| 84 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | *pdataref=dataref;
|
|---|
| 88 | }
|
|---|
| 89 | /*}}}*/
|
|---|
| 90 | /*FUNCTION WriteData(mxArray** pdataref,double scalar){{{*/
|
|---|
| 91 | void WriteData(mxArray** pdataref,double scalar){
|
|---|
| 92 |
|
|---|
| 93 | *pdataref=mxCreateDoubleScalar(scalar);
|
|---|
| 94 | }
|
|---|
| 95 | /*}}}*/
|
|---|
| 96 | /*FUNCTION WriteData(mxArray** pdataref,int integer){{{*/
|
|---|
| 97 | void WriteData(mxArray** pdataref,int integer){
|
|---|
| 98 |
|
|---|
| 99 | *pdataref=mxCreateDoubleScalar((double)integer);
|
|---|
| 100 |
|
|---|
| 101 | }
|
|---|
| 102 | /*}}}*/
|
|---|
| 103 | /*FUNCTION WriteData(mxArray** pdataref,int boolean){{{*/
|
|---|
| 104 | void WriteData(mxArray** pdataref,bool boolean){
|
|---|
| 105 |
|
|---|
| 106 | *pdataref=mxCreateDoubleScalar((double)boolean);
|
|---|
| 107 |
|
|---|
| 108 | }
|
|---|
| 109 | /*}}}*/
|
|---|
| 110 | /*FUNCTION WriteData(mxArray** pdataref,char* string){{{*/
|
|---|
| 111 | void WriteData(mxArray** pdataref,char* string){
|
|---|
| 112 |
|
|---|
| 113 | *pdataref=mxCreateString(string);
|
|---|
| 114 | }
|
|---|
| 115 | /*}}}*/
|
|---|
| 116 | /*FUNCTION WriteData(mxArray** pdataref){{{*/
|
|---|
| 117 | void WriteData(mxArray** pdataref){
|
|---|
| 118 |
|
|---|
| 119 | ;
|
|---|
| 120 |
|
|---|
| 121 | }
|
|---|
| 122 | /*}}}*/
|
|---|
| 123 |
|
|---|
| 124 | /*ISSM objects*/
|
|---|
| 125 | /*FUNCTION WriteData(mxArray** pdataref,BamgGeom* bamggeom){{{*/
|
|---|
| 126 | void WriteData(mxArray** pdataref,BamgGeom* bamggeom){
|
|---|
| 127 |
|
|---|
| 128 | /*Intermediary*/
|
|---|
| 129 | int i;
|
|---|
| 130 | mxArray *dataref = NULL;
|
|---|
| 131 | const int numfields = 8;
|
|---|
| 132 | const char *fnames[numfields];
|
|---|
| 133 | mwSize ndim = 2;
|
|---|
| 134 | mwSize dimensions[2] = {1,1};
|
|---|
| 135 |
|
|---|
| 136 | /*Initialize field names*/
|
|---|
| 137 | i=0;
|
|---|
| 138 | fnames[i++] = "Vertices";
|
|---|
| 139 | fnames[i++] = "Edges";
|
|---|
| 140 | fnames[i++] = "TangentAtEdges";
|
|---|
| 141 | fnames[i++] = "Corners";
|
|---|
| 142 | fnames[i++] = "RequiredVertices";
|
|---|
| 143 | fnames[i++] = "RequiredEdges";
|
|---|
| 144 | fnames[i++] = "CrackedEdges";
|
|---|
| 145 | fnames[i++] = "SubDomains";
|
|---|
| 146 | _assert_(i==numfields);
|
|---|
| 147 |
|
|---|
| 148 | /*Initialize Matlab structure*/
|
|---|
| 149 | dataref=mxCreateStructArray(ndim,dimensions,numfields,fnames);
|
|---|
| 150 |
|
|---|
| 151 | /*set each matlab each field*/
|
|---|
| 152 | i=0;
|
|---|
| 153 | i++; SetStructureField(dataref,"Vertices", bamggeom->VerticesSize[0], bamggeom->VerticesSize[1], bamggeom->Vertices);
|
|---|
| 154 | i++; SetStructureField(dataref,"Edges", bamggeom->EdgesSize[0], bamggeom->EdgesSize[1], bamggeom->Edges);
|
|---|
| 155 | i++; SetStructureField(dataref,"TangentAtEdges", bamggeom->TangentAtEdgesSize[0], bamggeom->TangentAtEdgesSize[1], bamggeom->TangentAtEdges);
|
|---|
| 156 | i++; SetStructureField(dataref,"Corners", bamggeom->CornersSize[0], bamggeom->CornersSize[1], bamggeom->Corners);
|
|---|
| 157 | i++; SetStructureField(dataref,"RequiredVertices",bamggeom->RequiredVerticesSize[0],bamggeom->RequiredVerticesSize[1],bamggeom->RequiredVertices);
|
|---|
| 158 | i++; SetStructureField(dataref,"RequiredEdges", bamggeom->RequiredEdgesSize[0], bamggeom->RequiredEdgesSize[1], bamggeom->RequiredEdges);
|
|---|
| 159 | i++; SetStructureField(dataref,"CrackedEdges", bamggeom->CrackedEdgesSize[0], bamggeom->CrackedEdgesSize[1], bamggeom->CrackedEdges);
|
|---|
| 160 | i++; SetStructureField(dataref,"SubDomains", bamggeom->SubDomainsSize[0], bamggeom->SubDomainsSize[1], bamggeom->SubDomains);
|
|---|
| 161 | _assert_(i==numfields);
|
|---|
| 162 |
|
|---|
| 163 | /*Assign output*/
|
|---|
| 164 | *pdataref=dataref;
|
|---|
| 165 | }
|
|---|
| 166 | /*}}}*/
|
|---|
| 167 | /*FUNCTION WriteData(mxArray** pdataref,BamgMesh* bamgmesh){{{*/
|
|---|
| 168 | void WriteData(mxArray** pdataref,BamgMesh* bamgmesh){
|
|---|
| 169 |
|
|---|
| 170 | /*Intermediary*/
|
|---|
| 171 | int i;
|
|---|
| 172 | mxArray *dataref = NULL;
|
|---|
| 173 | const int numfields = 16;
|
|---|
| 174 | const char *fnames[numfields];
|
|---|
| 175 | mwSize ndim = 2;
|
|---|
| 176 | mwSize dimensions[2] = {1,1};
|
|---|
| 177 |
|
|---|
| 178 | /*Initialize field names*/
|
|---|
| 179 | i=0;
|
|---|
| 180 | fnames[i++] = "Vertices";
|
|---|
| 181 | fnames[i++] = "Edges";
|
|---|
| 182 | fnames[i++] = "Triangles";
|
|---|
| 183 | fnames[i++] = "Quadrilaterals";
|
|---|
| 184 | fnames[i++] = "IssmEdges";
|
|---|
| 185 | fnames[i++] = "IssmSegments";
|
|---|
| 186 | fnames[i++] = "VerticesOnGeomVertex";
|
|---|
| 187 | fnames[i++] = "VerticesOnGeomEdge";
|
|---|
| 188 | fnames[i++] = "EdgesOnGeomEdge";
|
|---|
| 189 | fnames[i++] = "SubDomains";
|
|---|
| 190 | fnames[i++] = "SubDomainsFromGeom";
|
|---|
| 191 | fnames[i++] = "ElementConnectivity";
|
|---|
| 192 | fnames[i++] = "NodalConnectivity";
|
|---|
| 193 | fnames[i++] = "NodalElementConnectivity";
|
|---|
| 194 | fnames[i++] = "CrackedVertices";
|
|---|
| 195 | fnames[i++] = "CrackedEdges";
|
|---|
| 196 | _assert_(i==numfields);
|
|---|
| 197 |
|
|---|
| 198 | /*Initialize Matlab structure*/
|
|---|
| 199 | dataref=mxCreateStructArray(ndim,dimensions,numfields,fnames);
|
|---|
| 200 |
|
|---|
| 201 | /*set each matlab each field*/
|
|---|
| 202 | i=0;
|
|---|
| 203 | i++; SetStructureField(dataref,"Vertices",bamgmesh->VerticesSize[0], bamgmesh->VerticesSize[1],bamgmesh->Vertices);
|
|---|
| 204 | i++; SetStructureField(dataref,"Edges", bamgmesh->EdgesSize[0],bamgmesh->EdgesSize[1], bamgmesh->Edges);
|
|---|
| 205 | i++; SetStructureField(dataref,"Triangles", bamgmesh->TrianglesSize[0],bamgmesh->TrianglesSize[1], bamgmesh->Triangles);
|
|---|
| 206 | i++; SetStructureField(dataref,"Quadrilaterals",bamgmesh->QuadrilateralsSize[0], bamgmesh->QuadrilateralsSize[1],bamgmesh->Quadrilaterals);
|
|---|
| 207 | i++; SetStructureField(dataref,"IssmEdges", bamgmesh->IssmEdgesSize[0],bamgmesh->IssmEdgesSize[1], bamgmesh->IssmEdges);
|
|---|
| 208 | i++; SetStructureField(dataref,"IssmSegments",bamgmesh->IssmSegmentsSize[0], bamgmesh->IssmSegmentsSize[1],bamgmesh->IssmSegments);
|
|---|
| 209 | i++; SetStructureField(dataref,"VerticesOnGeomVertex",bamgmesh->VerticesOnGeomVertexSize[0],bamgmesh->VerticesOnGeomVertexSize[1], bamgmesh->VerticesOnGeomVertex);
|
|---|
| 210 | i++; SetStructureField(dataref,"VerticesOnGeomEdge",bamgmesh->VerticesOnGeomEdgeSize[0],bamgmesh->VerticesOnGeomEdgeSize[1], bamgmesh->VerticesOnGeomEdge);
|
|---|
| 211 | i++; SetStructureField(dataref,"EdgesOnGeomEdge", bamgmesh->EdgesOnGeomEdgeSize[0], bamgmesh->EdgesOnGeomEdgeSize[1],bamgmesh->EdgesOnGeomEdge);
|
|---|
| 212 | i++; SetStructureField(dataref,"SubDomains",bamgmesh->SubDomainsSize[0], bamgmesh->SubDomainsSize[1],bamgmesh->SubDomains);
|
|---|
| 213 | i++; SetStructureField(dataref,"SubDomainsFromGeom", bamgmesh->SubDomainsFromGeomSize[0], bamgmesh->SubDomainsFromGeomSize[1],bamgmesh->SubDomainsFromGeom);
|
|---|
| 214 | i++; SetStructureField(dataref,"ElementConnectivity",bamgmesh->ElementConnectivitySize[0],bamgmesh->ElementConnectivitySize[1], bamgmesh->ElementConnectivity);
|
|---|
| 215 | i++; SetStructureField(dataref,"NodalConnectivity",bamgmesh->NodalConnectivitySize[0],bamgmesh->NodalConnectivitySize[1], bamgmesh->NodalConnectivity);
|
|---|
| 216 | i++; SetStructureField(dataref,"NodalElementConnectivity", bamgmesh->NodalElementConnectivitySize[0], bamgmesh->NodalElementConnectivitySize[1],bamgmesh->NodalElementConnectivity);
|
|---|
| 217 | i++; SetStructureField(dataref,"CrackedVertices", bamgmesh->CrackedVerticesSize[0],bamgmesh->CrackedVerticesSize[1], bamgmesh->CrackedVertices);
|
|---|
| 218 | i++; SetStructureField(dataref,"CrackedEdges",bamgmesh->CrackedEdgesSize[0], bamgmesh->CrackedEdgesSize[1],bamgmesh->CrackedEdges);
|
|---|
| 219 | _assert_(i==numfields);
|
|---|
| 220 |
|
|---|
| 221 | /*Assign output*/
|
|---|
| 222 | *pdataref=dataref;
|
|---|
| 223 | }
|
|---|
| 224 | /*}}}*/
|
|---|
| 225 | /*FUNCTION WriteData(mxArray** pdataref,SeqMat<double>* matrix){{{*/
|
|---|
| 226 | void WriteData(mxArray** pdataref,SeqMat<double>* matrix){
|
|---|
| 227 |
|
|---|
| 228 | int i,j;
|
|---|
| 229 | int rows,cols;
|
|---|
| 230 | mxArray *dataref = NULL;
|
|---|
| 231 | double *matrix_ptr = NULL;
|
|---|
| 232 | double *tmatrix_ptr = NULL;
|
|---|
| 233 |
|
|---|
| 234 | if(matrix){
|
|---|
| 235 |
|
|---|
| 236 | matrix_ptr=matrix->ToSerial();
|
|---|
| 237 | matrix->GetSize(&rows,&cols);
|
|---|
| 238 |
|
|---|
| 239 | /*Now transpose the matrix and allocate with Matlab's memory manager: */
|
|---|
| 240 | tmatrix_ptr=(double*)mxMalloc(rows*cols*sizeof(double));
|
|---|
| 241 | for(i=0;i<rows;i++){
|
|---|
| 242 | for(j=0;j<cols;j++){
|
|---|
| 243 | tmatrix_ptr[j*rows+i]=matrix_ptr[i*cols+j];
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | /*create matlab matrix: */
|
|---|
| 248 | dataref=mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 249 | mxSetM(dataref,rows);
|
|---|
| 250 | mxSetN(dataref,cols);
|
|---|
| 251 | mxSetPr(dataref,tmatrix_ptr);
|
|---|
| 252 |
|
|---|
| 253 | /*Free ressources:*/
|
|---|
| 254 | xDelete<double>(matrix_ptr);
|
|---|
| 255 | }
|
|---|
| 256 | else{
|
|---|
| 257 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | *pdataref=dataref;
|
|---|
| 261 | }
|
|---|
| 262 | /*}}}*/
|
|---|
| 263 | /*FUNCTION WriteData(mxArray** pdataref,SeqVec<double>* vector){{{*/
|
|---|
| 264 | void WriteData(mxArray** pdataref,SeqVec<double>* vector){
|
|---|
| 265 |
|
|---|
| 266 | mxArray* dataref=NULL;
|
|---|
| 267 | double* vector_ptr=NULL;
|
|---|
| 268 | double* vector_matlab=NULL;
|
|---|
| 269 | int rows;
|
|---|
| 270 |
|
|---|
| 271 | if(vector){
|
|---|
| 272 | /*call toolkit routine: */
|
|---|
| 273 | vector_ptr=vector->ToMPISerial();
|
|---|
| 274 | vector->GetSize(&rows);
|
|---|
| 275 |
|
|---|
| 276 | /*now create the matlab vector with Matlab's memory manager */
|
|---|
| 277 | vector_matlab=(double*)mxMalloc(rows*sizeof(double));
|
|---|
| 278 | for(int i=0;i<rows;i++) vector_matlab[i]=vector_ptr[i];
|
|---|
| 279 |
|
|---|
| 280 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 281 | mxSetM(dataref,rows);
|
|---|
| 282 | mxSetN(dataref,1);
|
|---|
| 283 | mxSetPr(dataref,vector_matlab);
|
|---|
| 284 | }
|
|---|
| 285 | else{
|
|---|
| 286 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /*Clean-up and return*/
|
|---|
| 290 | xDelete<double>(vector_ptr);
|
|---|
| 291 | *pdataref=dataref;
|
|---|
| 292 | }
|
|---|
| 293 | /*}}}*/
|
|---|
| 294 |
|
|---|
| 295 | /*Toolkit*/
|
|---|
| 296 | /*FUNCTION SetStructureField{{{*/
|
|---|
| 297 | void SetStructureField(mxArray* dataref,const char* fieldname,int M,int N,double* fieldpointer){
|
|---|
| 298 |
|
|---|
| 299 | mxArray* field = NULL;
|
|---|
| 300 |
|
|---|
| 301 | /*Convert field*/
|
|---|
| 302 | WriteData(&field,fieldpointer,M,N);
|
|---|
| 303 |
|
|---|
| 304 | /*Assign to structure*/
|
|---|
| 305 | mxSetField(dataref,0,fieldname,field);
|
|---|
| 306 | }
|
|---|
| 307 | /*}}}*/
|
|---|