source: issm/trunk-jpl/src/c/objects/ExternalResults/PetscVecExternalResult.cpp@ 11843

Last change on this file since 11843 was 11843, checked in by Eric.Larour, 13 years ago

Made matlab and python layers more independent in the ISSM code

File size: 7.2 KB
RevLine 
[4050]1/*!\file PetscVecExternalResult.c
2 * \brief: implementation of the PetscVecExternalResult object
3 */
4
5/*header files: */
6/*{{{1*/
7#ifdef HAVE_CONFIG_H
[9320]8 #include <config.h>
[4050]9#else
10#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
11#endif
12
[9320]13#include <stdio.h>
[4050]14#include <string.h>
15#include "../objects.h"
16#include "../../EnumDefinitions/EnumDefinitions.h"
17#include "../../shared/shared.h"
[4236]18#include "../../Container/Container.h"
[4050]19#include "../../include/include.h"
20/*}}}*/
21
[4248]22/*PetscVecExternalResult constructors and destructor*/
[4050]23/*FUNCTION PetscVecExternalResult::PetscVecExternalResult(){{{1*/
24PetscVecExternalResult::PetscVecExternalResult(){
25 return;
26}
27/*}}}*/
28/*FUNCTION PetscVecExternalResult::PetscVecExternalResult(int enum_type,IssmPetscVec value){{{1*/
[11695]29PetscVecExternalResult::PetscVecExternalResult(int in_id, int in_enum_type,Vector* in_value,int in_step, double in_time){
[4050]30
31 id=in_id;
32 enum_type=in_enum_type;
33
34 value=NULL;
35
36 if(in_value){
[11695]37 value=in_value->Duplicate();
38 in_value->Copy(value);
[4050]39 }
[4195]40 else value=NULL;
41
[4050]42 step=in_step;
43 time=in_time;
44}
45/*}}}*/
46/*FUNCTION PetscVecExternalResult::~PetscVecExternalResult(){{{1*/
47PetscVecExternalResult::~PetscVecExternalResult(){
48 VecFree(&value);
49}
50/*}}}*/
51
[4248]52/*Object virtual functions definitions:*/
53/*FUNCTION PetscVecExternalResult::Echo {{{1*/
54void PetscVecExternalResult::Echo(void){
[4050]55
[4248]56 printf("PetscVecExternalResult:\n");
[8224]57 printf(" enum: %i (%s)\n",this->enum_type,EnumToStringx(this->enum_type));
[4248]58
[4050]59}
60/*}}}*/
61/*FUNCTION PetscVecExternalResult::DeepEcho{{{1*/
62void PetscVecExternalResult::DeepEcho(void){
63
64 int i;
65 printf("PetscVecExternalResult:\n");
66 printf(" id: %i\n",this->id);
[8224]67 printf(" enum: %i (%s)\n",this->enum_type,EnumToStringx(this->enum_type));
[4050]68 printf(" step: %i\n",this->step);
69 printf(" time: %g\n",this->time);
70 VecView(value,PETSC_VIEWER_STDOUT_WORLD);
71}
72/*}}}*/
[4248]73/*FUNCTION PetscVecExternalResult::Id{{{1*/
74int PetscVecExternalResult::Id(void){ return -1; }
75/*}}}*/
76/*FUNCTION PetscVecExternalResult::MyRank{{{1*/
77int PetscVecExternalResult::MyRank(void){
78 extern int my_rank;
79 return my_rank;
80}
81/*}}}*/
[9777]82#ifdef _SERIAL_
[4248]83/*FUNCTION PetscVecExternalResult::Marshall{{{1*/
84void PetscVecExternalResult::Marshall(char** pmarshalled_dataset){
85
86 char* marshalled_dataset=NULL;
87 int enum_value=0;
88 int M;
89 double* serial_value=NULL;
90
91 /*recover marshalled_dataset: */
92 marshalled_dataset=*pmarshalled_dataset;
93
94 /*get enum value of PetscVecExternalResult: */
95 enum_value=PetscVecExternalResultEnum;
96
97 /*marshall enum: */
98 memcpy(marshalled_dataset,&enum_value,sizeof(enum_value));marshalled_dataset+=sizeof(enum_value);
99
100 /*marshall PetscVecExternalResult data: */
101 memcpy(marshalled_dataset,&id,sizeof(id));marshalled_dataset+=sizeof(id);
102 memcpy(marshalled_dataset,&enum_type,sizeof(enum_type));marshalled_dataset+=sizeof(enum_type);
103 memcpy(marshalled_dataset,&step,sizeof(step));marshalled_dataset+=sizeof(step);
104 memcpy(marshalled_dataset,&time,sizeof(time));marshalled_dataset+=sizeof(time);
105
106 if(value){
107 VecGetSize(value,&M);
108 VecToMPISerial(&serial_value,value);
109 memcpy(marshalled_dataset,&M,sizeof(M));marshalled_dataset+=sizeof(M);
110 memcpy(marshalled_dataset,serial_value,M*sizeof(double));marshalled_dataset+=(M*sizeof(double));
111 }
112 else{
113 M=0;
114 memcpy(marshalled_dataset,&M,sizeof(M));marshalled_dataset+=sizeof(M);
115 }
116 /*Free ressources:*/
117 xfree((void**)&serial_value);
118
119 /*return:*/
120 *pmarshalled_dataset=marshalled_dataset;
121}
122/*}}}*/
123/*FUNCTION PetscVecExternalResult::MarshallSize{{{1*/
124int PetscVecExternalResult::MarshallSize(){
125
126 int M=0;
127 if(value)VecGetSize(value,&M);
128
129 return sizeof(M)+M*sizeof(double)
130 +sizeof(id)
131 +sizeof(enum_type)
132 +sizeof(step)
133 +sizeof(time)
134 +sizeof(int); //sizeof(int) for enum value
135}
136/*}}}*/
[4050]137/*FUNCTION PetscVecExternalResult::Demarshall{{{1*/
138void PetscVecExternalResult::Demarshall(char** pmarshalled_dataset){
139
140 char* marshalled_dataset=NULL;
141 int i;
142 int M;
143 double* serial_vec=NULL;
144 int* idxm=NULL;
145
146 /*recover marshalled_dataset: */
147 marshalled_dataset=*pmarshalled_dataset;
148
149 /*this time, no need to get enum type, the pointer directly points to the beginning of the
150 *object data (thanks to DataSet::Demarshall):*/
151 memcpy(&id,marshalled_dataset,sizeof(id));marshalled_dataset+=sizeof(id);
152 memcpy(&enum_type,marshalled_dataset,sizeof(enum_type));marshalled_dataset+=sizeof(enum_type);
153
154 /*data: */
155 memcpy(&step,marshalled_dataset,sizeof(step));marshalled_dataset+=sizeof(step);
156 memcpy(&time,marshalled_dataset,sizeof(time));marshalled_dataset+=sizeof(time);
157
158 memcpy(&M,marshalled_dataset,sizeof(M));marshalled_dataset+=sizeof(M);
159 if(M){
160 serial_vec=(double*)xmalloc(M*sizeof(double));
161 memcpy(serial_vec,marshalled_dataset,M*sizeof(double));marshalled_dataset+=(M*sizeof(double));
162
163 value=NewVec(M);
164 idxm=(int*)xmalloc(M*sizeof(int));
165 for(i=0;i<M;i++)idxm[i]=i;
166 VecSetValues(value,M,idxm,serial_vec,INSERT_VALUES);
167
168 VecAssemblyBegin(value);
169 VecAssemblyEnd(value);
170
171
172 }
173 else{
174 value=NULL;
175 }
176
177 /*Free ressources:*/
178 xfree((void**)&serial_vec);
179 xfree((void**)&idxm);
180
181 /*return: */
182 *pmarshalled_dataset=marshalled_dataset;
183}
184/*}}}*/
[9777]185#endif
[9883]186/*FUNCTION PetscVecExternalResult::ObjectEnum{{{1*/
187int PetscVecExternalResult::ObjectEnum(void){
[4050]188
189 return PetscVecExternalResultEnum;
190
191}
192/*}}}*/
[4248]193/*FUNCTION PetscVecExternalResult::copy{{{1*/
194Object* PetscVecExternalResult::copy() {
[4050]195
[4248]196 return new PetscVecExternalResult(this->id,this->enum_type,this->value,this->step,this->time);
[4050]197
198}
199/*}}}*/
200
[4248]201/*PetscVecExternalResult management: */
[4546]202/*FUNCTION PetscVecExternalResult::WriteData{{{1*/
[6389]203void PetscVecExternalResult::WriteData(FILE* fid,bool io_gather){
[4139]204
205 int length;
206 int type;
[4143]207 int size;
[4139]208 char *name = NULL;
209 double *serialvec = NULL;
[9761]210 extern int my_rank;
[4139]211
212 /*serialize: */
213 VecGetSize(this->value,&size);
214 VecToMPISerial(&serialvec,this->value);
215
216 /*now, exit if we are not on cpu 0: */
217 if(my_rank)return;
218
219 /*First write enum: */
[11202]220 EnumToStringx(&name,this->enum_type);
[4139]221 length=(strlen(name)+1)*sizeof(char);
222 fwrite(&length,sizeof(int),1,fid);
223 fwrite(name,length,1,fid);
[11205]224 xfree((void**)&name);
[4139]225
226 /*Now write time and step: */
227 fwrite(&time,sizeof(double),1,fid);
228 fwrite(&step,sizeof(int),1,fid);
229
230 /*writing a double, type is 1, size is 1: */
231 type=1;
232
233 fwrite(&type,sizeof(int),1,fid);
234 fwrite(&size,sizeof(int),1,fid);
235 fwrite(serialvec,size*sizeof(double),1,fid);
236
237 /*Free ressources:*/
238 xfree((void**)&serialvec);
239}
240/*}}}1*/
[4546]241/*FUNCTION PetscVecExternalResult::GetResultName{{{1*/
[11202]242void PetscVecExternalResult::GetResultName(char**pname){
243 EnumToStringx(pname,this->enum_type);
[4166]244}
245/*}}}*/
[4546]246/*FUNCTION PetscVecExternalResult::SetMatlabField{{{1*/
[11843]247#ifdef _HAVE_MATLAB_
[4166]248void PetscVecExternalResult::SetMatlabField(mxArray* dataref){
249
250 mxArray* pfield=NULL;
251 char* name=NULL;
252 double* doublevec=NULL;
253 int M;
254
255 VecToMPISerial(&doublevec,value);
256 VecGetSize(value,&M);
[11202]257 this->GetResultName(&name);
[4166]258
259 pfield=mxCreateDoubleMatrix(0,0,mxREAL);
260 mxSetM(pfield,M);
261 mxSetN(pfield,1);
262 mxSetPr(pfield,doublevec);
263
[4182]264 mxSetField( dataref, this->step-1, name, pfield);
[4321]265 mxSetField( dataref, this->step-1, "time",mxCreateDoubleScalar((double)this->time));
266 mxSetField( dataref, this->step-1, "step",mxCreateDoubleScalar((double)this->step));
[4182]267
[4166]268}
269#endif
270/*}}}*/
[4546]271/*FUNCTION PetscVecExternalResult::GetStep{{{1*/
[4182]272int PetscVecExternalResult::GetStep(void){
273
274 return this->step;
275}
276/*}}}*/
Note: See TracBrowser for help on using the repository browser.