| 1 | /*!\file IntMatParam.c | 
|---|
| 2 | * \brief: implementation of the IntMatParam object | 
|---|
| 3 | */ | 
|---|
| 4 |  | 
|---|
| 5 | /*header files: */ | 
|---|
| 6 | /*{{{1*/ | 
|---|
| 7 | #ifdef HAVE_CONFIG_H | 
|---|
| 8 | #include <config.h> | 
|---|
| 9 | #else | 
|---|
| 10 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!" | 
|---|
| 11 | #endif | 
|---|
| 12 |  | 
|---|
| 13 | #include <stdio.h> | 
|---|
| 14 | #include <string.h> | 
|---|
| 15 | #include "../objects.h" | 
|---|
| 16 | #include "../../EnumDefinitions/EnumDefinitions.h" | 
|---|
| 17 | #include "../../shared/shared.h" | 
|---|
| 18 | #include "../../Container/Container.h" | 
|---|
| 19 | #include "../../include/include.h" | 
|---|
| 20 | /*}}}*/ | 
|---|
| 21 |  | 
|---|
| 22 | /*IntMatParam constructors and destructor*/ | 
|---|
| 23 | /*FUNCTION IntMatParam::IntMatParam(){{{1*/ | 
|---|
| 24 | IntMatParam::IntMatParam(){ | 
|---|
| 25 | return; | 
|---|
| 26 | } | 
|---|
| 27 | /*}}}*/ | 
|---|
| 28 | /*FUNCTION IntMatParam::IntMatParam(int enum_type,IssmIntMat value){{{1*/ | 
|---|
| 29 | IntMatParam::IntMatParam(int in_enum_type,int* in_value, int in_M,int in_N){ | 
|---|
| 30 |  | 
|---|
| 31 | enum_type=in_enum_type; | 
|---|
| 32 | M=in_M; | 
|---|
| 33 | N=in_N; | 
|---|
| 34 |  | 
|---|
| 35 | value=(int*)xmalloc(M*N*sizeof(int)); | 
|---|
| 36 | memcpy(value,in_value,M*N*sizeof(int)); | 
|---|
| 37 | } | 
|---|
| 38 | /*}}}*/ | 
|---|
| 39 | /*FUNCTION IntMatParam::~IntMatParam(){{{1*/ | 
|---|
| 40 | IntMatParam::~IntMatParam(){ | 
|---|
| 41 | xfree((void**)&value); | 
|---|
| 42 | return; | 
|---|
| 43 | } | 
|---|
| 44 | /*}}}*/ | 
|---|
| 45 |  | 
|---|
| 46 | /*Object virtual functions definitions:*/ | 
|---|
| 47 | /*FUNCTION IntMatParam::Echo {{{1*/ | 
|---|
| 48 | void IntMatParam::Echo(void){ | 
|---|
| 49 |  | 
|---|
| 50 | printf("IntMatParam:\n"); | 
|---|
| 51 | printf("   enum: %i (%s)\n",this->enum_type,EnumToStringx(this->enum_type)); | 
|---|
| 52 | printf("   matrix size: %ix%i\n",this->M,this->N); | 
|---|
| 53 |  | 
|---|
| 54 | } | 
|---|
| 55 | /*}}}*/ | 
|---|
| 56 | /*FUNCTION IntMatParam::DeepEcho{{{1*/ | 
|---|
| 57 | void IntMatParam::DeepEcho(void){ | 
|---|
| 58 |  | 
|---|
| 59 | int i,j; | 
|---|
| 60 |  | 
|---|
| 61 | printf("IntMatParam:\n"); | 
|---|
| 62 | printf("   enum: %i (%s)\n",this->enum_type,EnumToStringx(this->enum_type)); | 
|---|
| 63 | printf("   matrix size: %ix%i\n",this->M,this->N); | 
|---|
| 64 | for(i=0;i<this->M;i++){ | 
|---|
| 65 | for(i=0;i<this->N;i++){ | 
|---|
| 66 | printf("(%i,%i) %i\n",i,j,*(this->value+N*i+j)); | 
|---|
| 67 | } | 
|---|
| 68 | } | 
|---|
| 69 | } | 
|---|
| 70 | /*}}}*/ | 
|---|
| 71 | /*FUNCTION IntMatParam::Id{{{1*/ | 
|---|
| 72 | int    IntMatParam::Id(void){ return -1; } | 
|---|
| 73 | /*}}}*/ | 
|---|
| 74 | /*FUNCTION IntMatParam::MyRank{{{1*/ | 
|---|
| 75 | int    IntMatParam::MyRank(void){ | 
|---|
| 76 | extern int my_rank; | 
|---|
| 77 | return my_rank; | 
|---|
| 78 | } | 
|---|
| 79 | /*}}}*/ | 
|---|
| 80 | #ifdef _SERIAL_ | 
|---|
| 81 | /*FUNCTION IntMatParam::Marshall{{{1*/ | 
|---|
| 82 | void  IntMatParam::Marshall(char** pmarshalled_dataset){ | 
|---|
| 83 |  | 
|---|
| 84 | char* marshalled_dataset=NULL; | 
|---|
| 85 | int   enum_value=0; | 
|---|
| 86 |  | 
|---|
| 87 | /*recover marshalled_dataset: */ | 
|---|
| 88 | marshalled_dataset=*pmarshalled_dataset; | 
|---|
| 89 |  | 
|---|
| 90 | /*get enum value of IntMatParam: */ | 
|---|
| 91 | enum_value=IntMatParamEnum; | 
|---|
| 92 |  | 
|---|
| 93 | /*marshall enum: */ | 
|---|
| 94 | memcpy(marshalled_dataset,&enum_value,sizeof(enum_value));marshalled_dataset+=sizeof(enum_value); | 
|---|
| 95 |  | 
|---|
| 96 | /*marshall IntMatParam data: */ | 
|---|
| 97 | memcpy(marshalled_dataset,&enum_type,sizeof(enum_type));marshalled_dataset+=sizeof(enum_type); | 
|---|
| 98 | memcpy(marshalled_dataset,&M,sizeof(M));marshalled_dataset+=sizeof(M); | 
|---|
| 99 | memcpy(marshalled_dataset,&N,sizeof(N));marshalled_dataset+=sizeof(N); | 
|---|
| 100 | memcpy(marshalled_dataset,value,M*N*sizeof(int));marshalled_dataset+=M*N*sizeof(int); | 
|---|
| 101 |  | 
|---|
| 102 | *pmarshalled_dataset=marshalled_dataset; | 
|---|
| 103 | } | 
|---|
| 104 | /*}}}*/ | 
|---|
| 105 | /*FUNCTION IntMatParam::MarshallSize{{{1*/ | 
|---|
| 106 | int   IntMatParam::MarshallSize(){ | 
|---|
| 107 |  | 
|---|
| 108 | return sizeof(M) | 
|---|
| 109 | +sizeof(N) | 
|---|
| 110 | +M*N*sizeof(int) | 
|---|
| 111 | +sizeof(enum_type)+ | 
|---|
| 112 | +sizeof(int); //sizeof(int) for enum value | 
|---|
| 113 | } | 
|---|
| 114 | /*}}}*/ | 
|---|
| 115 | /*FUNCTION IntMatParam::Demarshall{{{1*/ | 
|---|
| 116 | void  IntMatParam::Demarshall(char** pmarshalled_dataset){ | 
|---|
| 117 |  | 
|---|
| 118 | char* marshalled_dataset=NULL; | 
|---|
| 119 | int   i; | 
|---|
| 120 |  | 
|---|
| 121 | /*recover marshalled_dataset: */ | 
|---|
| 122 | marshalled_dataset=*pmarshalled_dataset; | 
|---|
| 123 |  | 
|---|
| 124 | /*this time, no need to get enum type, the pointer directly points to the beginning of the | 
|---|
| 125 | *object data (thanks to DataSet::Demarshall):*/ | 
|---|
| 126 | memcpy(&enum_type,marshalled_dataset,sizeof(enum_type));marshalled_dataset+=sizeof(enum_type); | 
|---|
| 127 |  | 
|---|
| 128 | /*data: */ | 
|---|
| 129 | memcpy(&M,marshalled_dataset,sizeof(M));marshalled_dataset+=sizeof(M); | 
|---|
| 130 | memcpy(&N,marshalled_dataset,sizeof(N));marshalled_dataset+=sizeof(N); | 
|---|
| 131 | value=(int*)xmalloc(M*N*sizeof(int)); | 
|---|
| 132 | memcpy(value,marshalled_dataset,M*N*sizeof(int));marshalled_dataset+=M*N*sizeof(int); | 
|---|
| 133 |  | 
|---|
| 134 | /*return: */ | 
|---|
| 135 | *pmarshalled_dataset=marshalled_dataset; | 
|---|
| 136 | return; | 
|---|
| 137 | } | 
|---|
| 138 | /*}}}*/ | 
|---|
| 139 | #endif | 
|---|
| 140 | /*FUNCTION IntMatParam::ObjectEnum{{{1*/ | 
|---|
| 141 | int IntMatParam::ObjectEnum(void){ | 
|---|
| 142 |  | 
|---|
| 143 | return IntMatParamEnum; | 
|---|
| 144 |  | 
|---|
| 145 | } | 
|---|
| 146 | /*}}}*/ | 
|---|
| 147 | /*FUNCTION IntMatParam::copy{{{1*/ | 
|---|
| 148 | Object* IntMatParam::copy() { | 
|---|
| 149 |  | 
|---|
| 150 | return new IntMatParam(this->enum_type,this->value,this->M,this->N); | 
|---|
| 151 |  | 
|---|
| 152 | } | 
|---|
| 153 | /*}}}*/ | 
|---|
| 154 |  | 
|---|
| 155 | /*IntMatParam virtual functions definitions: */ | 
|---|
| 156 | /*FUNCTION IntMatParam::GetParameterValue{{{1*/ | 
|---|
| 157 | void  IntMatParam::GetParameterValue(int** pintarray,int* pM,int* pN){ | 
|---|
| 158 | int* output=NULL; | 
|---|
| 159 |  | 
|---|
| 160 | output=(int*)xmalloc((int)(M*N*sizeof(int))); | 
|---|
| 161 | memcpy(output,value,M*N*sizeof(int)); | 
|---|
| 162 |  | 
|---|
| 163 | /*Assign output pointers:*/ | 
|---|
| 164 | if(pM) *pM=M; | 
|---|
| 165 | if(pN) *pN=N; | 
|---|
| 166 | *pintarray=output; | 
|---|
| 167 | } | 
|---|
| 168 | /*}}}*/ | 
|---|
| 169 | /*FUNCTION IntMatParam::GetParameterName{{{1*/ | 
|---|
| 170 | void IntMatParam::GetParameterName(char**pname){ | 
|---|
| 171 | EnumToStringx(pname,this->enum_type); | 
|---|
| 172 | } | 
|---|
| 173 | /*}}}*/ | 
|---|
| 174 | /*FUNCTION IntMatParam::SetMatlabField{{{1*/ | 
|---|
| 175 | #ifdef _HAVE_MATLAB_ | 
|---|
| 176 | void  IntMatParam::SetMatlabField(mxArray* dataref){ | 
|---|
| 177 |  | 
|---|
| 178 | char    *name        = NULL; | 
|---|
| 179 | double  *doublearray = NULL; | 
|---|
| 180 | int     *intarray    = NULL; | 
|---|
| 181 | mxArray *pfield      = NULL; | 
|---|
| 182 | mxArray *pfield2     = NULL; | 
|---|
| 183 |  | 
|---|
| 184 | this->GetParameterValue(&intarray,NULL,NULL); | 
|---|
| 185 | this->GetParameterName(&name); | 
|---|
| 186 |  | 
|---|
| 187 | /*cast intarray into doublearray for Matlab*/ | 
|---|
| 188 | doublearray=(double*)xmalloc(M*N*sizeof(double)); | 
|---|
| 189 | for(int i=0;i<M*N;i++)doublearray[i]=(double)intarray[i]; | 
|---|
| 190 | xfree((void**)&intarray); | 
|---|
| 191 |  | 
|---|
| 192 | pfield=mxCreateDoubleMatrix(0,0,mxREAL); | 
|---|
| 193 | mxSetM(pfield,M); | 
|---|
| 194 | mxSetN(pfield,N); | 
|---|
| 195 | mxSetPr(pfield,doublearray); | 
|---|
| 196 |  | 
|---|
| 197 | //transpose the matrix, written directly to matlab! from C to matlab. | 
|---|
| 198 | mexCallMATLAB(1,&pfield2, 1, &pfield, "transpose"); | 
|---|
| 199 | mxSetField( dataref, 0, name,pfield2); | 
|---|
| 200 | } | 
|---|
| 201 | #endif | 
|---|
| 202 | /*}}}*/ | 
|---|
| 203 | /*FUNCTION IntMatParam::SetValue{{{1*/ | 
|---|
| 204 | void  IntMatParam::SetValue(int* intarray,int in_M,int in_N){ | 
|---|
| 205 |  | 
|---|
| 206 | /*avoid leak: */ | 
|---|
| 207 | xfree((void**)&this->value); | 
|---|
| 208 |  | 
|---|
| 209 | this->value=(int*)xmalloc(in_M*in_N*sizeof(int)); | 
|---|
| 210 | memcpy(this->value,intarray,in_M*in_N*sizeof(int)); | 
|---|
| 211 |  | 
|---|
| 212 | this->M=in_M; | 
|---|
| 213 | this->N=in_N; | 
|---|
| 214 | } | 
|---|
| 215 | /*}}}*/ | 
|---|
| 216 | /*FUNCTION IntMatParam::UnitConversion{{{1*/ | 
|---|
| 217 | void  IntMatParam::UnitConversion(int direction_enum){ | 
|---|
| 218 | /*do nothing, no unit conversion*/ | 
|---|
| 219 | } | 
|---|
| 220 | /*}}}*/ | 
|---|