| 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 |
|
|---|
| 117 | /*ISSM objects*/
|
|---|
| 118 | /*FUNCTION WriteData(mxArray** pdataref,BamgGeom* bamggeom){{{*/
|
|---|
| 119 | void WriteData(mxArray** pdataref,BamgGeom* bamggeom){
|
|---|
| 120 |
|
|---|
| 121 | /*Intermediary*/
|
|---|
| 122 | int i;
|
|---|
| 123 | mxArray *dataref = NULL;
|
|---|
| 124 | const int numfields = 7;
|
|---|
| 125 | const char *fnames[numfields];
|
|---|
| 126 | mwSize ndim = 2;
|
|---|
| 127 | mwSize dimensions[2] = {1,1};
|
|---|
| 128 |
|
|---|
| 129 | /*Initialize field names*/
|
|---|
| 130 | i=0;
|
|---|
| 131 | fnames[i++] = "Vertices";
|
|---|
| 132 | fnames[i++] = "Edges";
|
|---|
| 133 | fnames[i++] = "TangentAtEdges";
|
|---|
| 134 | fnames[i++] = "RequiredVertices";
|
|---|
| 135 | fnames[i++] = "RequiredEdges";
|
|---|
| 136 | fnames[i++] = "CrackedEdges";
|
|---|
| 137 | fnames[i++] = "SubDomains";
|
|---|
| 138 | _assert_(i==numfields);
|
|---|
| 139 |
|
|---|
| 140 | /*Initialize Matlab structure*/
|
|---|
| 141 | dataref=mxCreateStructArray(ndim,dimensions,numfields,fnames);
|
|---|
| 142 |
|
|---|
| 143 | /*set each matlab each field*/
|
|---|
| 144 | i=0;
|
|---|
| 145 | i++; SetStructureField(dataref,"Vertices", bamggeom->VerticesSize[0], bamggeom->VerticesSize[1], bamggeom->Vertices);
|
|---|
| 146 | i++; SetStructureField(dataref,"Edges", bamggeom->EdgesSize[0], bamggeom->EdgesSize[1], bamggeom->Edges);
|
|---|
| 147 | i++; SetStructureField(dataref,"TangentAtEdges", bamggeom->TangentAtEdgesSize[0], bamggeom->TangentAtEdgesSize[1], bamggeom->TangentAtEdges);
|
|---|
| 148 | i++; SetStructureField(dataref,"RequiredVertices",bamggeom->RequiredVerticesSize[0],bamggeom->RequiredVerticesSize[1],bamggeom->RequiredVertices);
|
|---|
| 149 | i++; SetStructureField(dataref,"RequiredEdges", bamggeom->RequiredEdgesSize[0], bamggeom->RequiredEdgesSize[1], bamggeom->RequiredEdges);
|
|---|
| 150 | i++; SetStructureField(dataref,"CrackedEdges", bamggeom->CrackedEdgesSize[0], bamggeom->CrackedEdgesSize[1], bamggeom->CrackedEdges);
|
|---|
| 151 | i++; SetStructureField(dataref,"SubDomains", bamggeom->SubDomainsSize[0], bamggeom->SubDomainsSize[1], bamggeom->SubDomains);
|
|---|
| 152 | _assert_(i==numfields);
|
|---|
| 153 |
|
|---|
| 154 | /*Assign output*/
|
|---|
| 155 | *pdataref=dataref;
|
|---|
| 156 | }
|
|---|
| 157 | /*}}}*/
|
|---|
| 158 | /*FUNCTION WriteData(mxArray** pdataref,BamgMesh* bamgmesh){{{*/
|
|---|
| 159 | void WriteData(mxArray** pdataref,BamgMesh* bamgmesh){
|
|---|
| 160 |
|
|---|
| 161 | /*Intermediary*/
|
|---|
| 162 | int i;
|
|---|
| 163 | mxArray *dataref = NULL;
|
|---|
| 164 | const int numfields = 16;
|
|---|
| 165 | const char *fnames[numfields];
|
|---|
| 166 | mwSize ndim = 2;
|
|---|
| 167 | mwSize dimensions[2] = {1,1};
|
|---|
| 168 |
|
|---|
| 169 | /*Initialize field names*/
|
|---|
| 170 | i=0;
|
|---|
| 171 | fnames[i++] = "Triangles";
|
|---|
| 172 | fnames[i++] = "Vertices";
|
|---|
| 173 | fnames[i++] = "Edges";
|
|---|
| 174 | fnames[i++] = "IssmSegments";
|
|---|
| 175 | fnames[i++] = "IssmEdges";
|
|---|
| 176 | fnames[i++] = "Quadrilaterals";
|
|---|
| 177 | fnames[i++] = "VerticesOnGeomVertex";
|
|---|
| 178 | fnames[i++] = "VerticesOnGeomEdge";
|
|---|
| 179 | fnames[i++] = "EdgesOnGeomEdge";
|
|---|
| 180 | fnames[i++] = "SubDomains";
|
|---|
| 181 | fnames[i++] = "SubDomainsFromGeom";
|
|---|
| 182 | fnames[i++] = "ElementConnectivity";
|
|---|
| 183 | fnames[i++] = "NodalConnectivity";
|
|---|
| 184 | fnames[i++] = "NodalElementConnectivity";
|
|---|
| 185 | fnames[i++] = "CrackedVertices";
|
|---|
| 186 | fnames[i++] = "CrackedEdges";
|
|---|
| 187 | _assert_(i==numfields);
|
|---|
| 188 |
|
|---|
| 189 | /*Initialize Matlab structure*/
|
|---|
| 190 | dataref=mxCreateStructArray(ndim,dimensions,numfields,fnames);
|
|---|
| 191 |
|
|---|
| 192 | /*set each matlab each field*/
|
|---|
| 193 | i=0;
|
|---|
| 194 | i++; SetStructureField(dataref,"Triangles", bamgmesh->TrianglesSize[0],bamgmesh->TrianglesSize[1], bamgmesh->Triangles);
|
|---|
| 195 | i++; SetStructureField(dataref,"Vertices",bamgmesh->VerticesSize[0], bamgmesh->VerticesSize[1],bamgmesh->Vertices);
|
|---|
| 196 | i++; SetStructureField(dataref,"Edges", bamgmesh->EdgesSize[0],bamgmesh->EdgesSize[1], bamgmesh->Edges);
|
|---|
| 197 | i++; SetStructureField(dataref,"IssmSegments",bamgmesh->IssmSegmentsSize[0], bamgmesh->IssmSegmentsSize[1],bamgmesh->IssmSegments);
|
|---|
| 198 | i++; SetStructureField(dataref,"IssmEdges", bamgmesh->IssmEdgesSize[0],bamgmesh->IssmEdgesSize[1], bamgmesh->IssmEdges);
|
|---|
| 199 | i++; SetStructureField(dataref,"Quadrilaterals",bamgmesh->QuadrilateralsSize[0], bamgmesh->QuadrilateralsSize[1],bamgmesh->Quadrilaterals);
|
|---|
| 200 | i++; SetStructureField(dataref,"VerticesOnGeomVertex",bamgmesh->VerticesOnGeomVertexSize[0],bamgmesh->VerticesOnGeomVertexSize[1], bamgmesh->VerticesOnGeomVertex);
|
|---|
| 201 | i++; SetStructureField(dataref,"VerticesOnGeomEdge",bamgmesh->VerticesOnGeomEdgeSize[0],bamgmesh->VerticesOnGeomEdgeSize[1], bamgmesh->VerticesOnGeomEdge);
|
|---|
| 202 | i++; SetStructureField(dataref,"EdgesOnGeomEdge", bamgmesh->EdgesOnGeomEdgeSize[0], bamgmesh->EdgesOnGeomEdgeSize[1],bamgmesh->EdgesOnGeomEdge);
|
|---|
| 203 | i++; SetStructureField(dataref,"SubDomains",bamgmesh->SubDomainsSize[0], bamgmesh->SubDomainsSize[1],bamgmesh->SubDomains);
|
|---|
| 204 | i++; SetStructureField(dataref,"SubDomainsFromGeom", bamgmesh->SubDomainsFromGeomSize[0], bamgmesh->SubDomainsFromGeomSize[1],bamgmesh->SubDomainsFromGeom);
|
|---|
| 205 | i++; SetStructureField(dataref,"ElementConnectivity",bamgmesh->ElementConnectivitySize[0],bamgmesh->ElementConnectivitySize[1], bamgmesh->ElementConnectivity);
|
|---|
| 206 | i++; SetStructureField(dataref,"NodalConnectivity",bamgmesh->NodalConnectivitySize[0],bamgmesh->NodalConnectivitySize[1], bamgmesh->NodalConnectivity);
|
|---|
| 207 | i++; SetStructureField(dataref,"NodalElementConnectivity", bamgmesh->NodalElementConnectivitySize[0], bamgmesh->NodalElementConnectivitySize[1],bamgmesh->NodalElementConnectivity);
|
|---|
| 208 | i++; SetStructureField(dataref,"CrackedVertices", bamgmesh->CrackedVerticesSize[0],bamgmesh->CrackedVerticesSize[1], bamgmesh->CrackedVertices);
|
|---|
| 209 | i++; SetStructureField(dataref,"CrackedEdges",bamgmesh->CrackedEdgesSize[0], bamgmesh->CrackedEdgesSize[1],bamgmesh->CrackedEdges);
|
|---|
| 210 | _assert_(i==numfields);
|
|---|
| 211 |
|
|---|
| 212 | /*Assign output*/
|
|---|
| 213 | *pdataref=dataref;
|
|---|
| 214 | }
|
|---|
| 215 | /*}}}*/
|
|---|
| 216 | /*FUNCTION WriteData(mxArray** pdataref,SeqMat<double>* matrix){{{*/
|
|---|
| 217 | void WriteData(mxArray** pdataref,SeqMat<double>* matrix){
|
|---|
| 218 |
|
|---|
| 219 | int i,j;
|
|---|
| 220 | int rows,cols;
|
|---|
| 221 | mxArray *dataref = NULL;
|
|---|
| 222 | double *matrix_ptr = NULL;
|
|---|
| 223 | double *tmatrix_ptr = NULL;
|
|---|
| 224 |
|
|---|
| 225 | if(matrix){
|
|---|
| 226 |
|
|---|
| 227 | matrix_ptr=matrix->ToSerial();
|
|---|
| 228 | matrix->GetSize(&rows,&cols);
|
|---|
| 229 |
|
|---|
| 230 | /*Now transpose the matrix and allocate with Matlab's memory manager: */
|
|---|
| 231 | tmatrix_ptr=(double*)mxMalloc(rows*cols*sizeof(double));
|
|---|
| 232 | for(i=0;i<rows;i++){
|
|---|
| 233 | for(j=0;j<cols;j++){
|
|---|
| 234 | tmatrix_ptr[j*rows+i]=matrix_ptr[i*cols+j];
|
|---|
| 235 | }
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | /*create matlab matrix: */
|
|---|
| 239 | dataref=mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 240 | mxSetM(dataref,rows);
|
|---|
| 241 | mxSetN(dataref,cols);
|
|---|
| 242 | mxSetPr(dataref,tmatrix_ptr);
|
|---|
| 243 |
|
|---|
| 244 | /*Free ressources:*/
|
|---|
| 245 | xDelete<double>(matrix_ptr);
|
|---|
| 246 | }
|
|---|
| 247 | else{
|
|---|
| 248 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | *pdataref=dataref;
|
|---|
| 252 | }
|
|---|
| 253 | /*}}}*/
|
|---|
| 254 | /*FUNCTION WriteData(mxArray** pdataref,SeqVec<double>* vector){{{*/
|
|---|
| 255 | void WriteData(mxArray** pdataref,SeqVec<double>* vector){
|
|---|
| 256 |
|
|---|
| 257 | mxArray* dataref=NULL;
|
|---|
| 258 | double* vector_ptr=NULL;
|
|---|
| 259 | double* vector_matlab=NULL;
|
|---|
| 260 | int rows;
|
|---|
| 261 |
|
|---|
| 262 | if(vector){
|
|---|
| 263 | /*call toolkit routine: */
|
|---|
| 264 | vector_ptr=vector->ToMPISerial();
|
|---|
| 265 | vector->GetSize(&rows);
|
|---|
| 266 |
|
|---|
| 267 | /*now create the matlab vector with Matlab's memory manager */
|
|---|
| 268 | vector_matlab=(double*)mxMalloc(rows*sizeof(double));
|
|---|
| 269 | for(int i=0;i<rows;i++) vector_matlab[i]=vector_ptr[i];
|
|---|
| 270 |
|
|---|
| 271 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 272 | mxSetM(dataref,rows);
|
|---|
| 273 | mxSetN(dataref,1);
|
|---|
| 274 | mxSetPr(dataref,vector_matlab);
|
|---|
| 275 | }
|
|---|
| 276 | else{
|
|---|
| 277 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | /*Clean-up and return*/
|
|---|
| 281 | xDelete<double>(vector_ptr);
|
|---|
| 282 | *pdataref=dataref;
|
|---|
| 283 | }
|
|---|
| 284 | /*}}}*/
|
|---|
| 285 |
|
|---|
| 286 | /*Toolkit*/
|
|---|
| 287 | /*FUNCTION SetStructureField{{{*/
|
|---|
| 288 | void SetStructureField(mxArray* dataref,const char* fieldname,int M,int N,double* fieldpointer){
|
|---|
| 289 |
|
|---|
| 290 | mxArray* field = NULL;
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 | /*Convert field*/
|
|---|
| 294 | WriteData(&field,fieldpointer,M,N);
|
|---|
| 295 |
|
|---|
| 296 | /*Assign to structure*/
|
|---|
| 297 | mxSetField(dataref,0,fieldname,field);
|
|---|
| 298 | }
|
|---|
| 299 | /*}}}*/
|
|---|