BinaryIO.cpp 3.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

#include  <iostream>
#include  <cfloat>
using namespace std;
#include "error.hpp"
#include "AFunction.hpp"
#include "rgraph.hpp"
#include "RNM.hpp"
#include "fem.hpp"
#include "FESpace.hpp" 
#include "MeshPoint.hpp"
#include "AFunction_ext.hpp" // Extension of "AFunction.hpp" to deal with more than 3 parameters function
using namespace Fem2D;


double SaveVecAppend(KN<double> *const & f, string *const & nome){
  std::ofstream outfile (nome->data(),ios_base::binary | std::ofstream::app);
 
  long int nn = f->N(); // get number of nodes
  long int dim=nn;
  long pos = outfile.tellp();
  if (!pos)
    outfile.write ((char*) &dim, sizeof(long int));//write the dimension of the vector
  double ftemp ;
  for(long int i=0; i<nn; i++) {
    
    ftemp = *(f[0]+i) ;
    outfile.write ((char*) &ftemp, sizeof(double));
  }
  outfile.close();
  return 0.0;  // dummy return value.
}

double SetSizeVec(long int *const & size, string *const & nome){
  //std::ofstream outfile (nome->data(),ios_base::binary | std::ofstream::app);
  std::fstream outfile(nome->data(), ios_base::binary | std::fstream::out | std::fstream::in);
  
  long int N=*size;
  
  outfile.write ((char*) &N, sizeof(long int));//write the dimension of the vector
  outfile.close();
  return 0.0;  // dummy return value.
}

double SaveVec(KN<double> *const & f, string *const & nome)   
{
  std::ofstream outfile (nome->data(),ios_base::binary);
  // To access value at node i of vector N, do as follow: *(N[0]+i)
  // Explanation (C++ for dummies as I am ;-):
  //   N         is an alias to the KN object.
  //   N[0]      is a pointer to the first element of the vector.
  //   N[0]+i    is a pointer to the ith element of the vector.
  //   *(N[0]+i) is the value of the ith element of the vector.
 
  long int nn = f->N(); // get number of nodes
  long int dim=nn;
  outfile.write ((char*) &dim, sizeof(long int));//write the dimension of the vector
  double ftemp ;
  for(long int i=0; i<nn; i++) {
    
    ftemp = *(f[0]+i) ;
    outfile.write ((char*) &ftemp, sizeof(double));
  }
  outfile.close();
  return 0.0;  // dummy return value.
}

double GetSizeVec(long int *const & size,string *const & nome){
  std::ifstream infile (nome->data(),ios_base::binary);
  long int dim;
  infile.read((char *) &dim, sizeof(long int));
  *size=dim;
  infile.close();
  return 0.0;
}
double LoadVec(KN<double> *const & ww, string *const & nome)   
{
  std::ifstream infile (nome->data(),ios_base::binary);
  long int dim;
  infile.read((char *) &dim, sizeof(long int));
  double dtemp;
  for(long int i=0; i<dim; i++)
   {
   infile.read((char *) &dtemp, sizeof(double));
    *(ww[0]+i)=dtemp ;
    }
  return 0.0;  // dummy return value.
}

 
double LoadFlag(long int *const & ww, string *const & nome)   
{
  std::ifstream infile (nome->data(),ios_base::binary);
  long int flag;
  infile.read((char *) &flag, sizeof(long int));
  *ww=flag;
  return 0.0;  // dummy return value.
}

double flag(long int *const & FLAG,string *const &nome)   
{
  std::ofstream outfile (nome->data(),ios_base::binary);
long int Flag;
 Flag= *FLAG;   
 outfile.write ((char*) &Flag, sizeof(long int));
outfile.close();
 return 0.0;
}



//   add the function name to the freefem++ table 
/*  class Init { public:
  Init();
};	
$1 */
static void Load_Init(){	
		
  Global.Add("LoadVec","(",new OneOperator2_<double,  KN<double>*, string* >(LoadVec)); 
  Global.Add("LoadFlag","(",new OneOperator2_<double,long int*, string* >(LoadFlag));
  Global.Add("SaveVec","(",new OneOperator2_<double,KN<double>*, string* >(SaveVec));
  Global.Add("flag","(",new OneOperator2_<double,long int*,string* >(flag));  
  Global.Add("GetSizeVec","(",new OneOperator2_<double,long int*,string* >(GetSizeVec));
  Global.Add("SaveVecAppend","(",new OneOperator2_<double,KN<double>*, string* >(SaveVecAppend));
  Global.Add("SetSizeVec","(",new OneOperator2_<double,long int*, string* >(SetSizeVec));
  
  
}

LOADFUNC(Load_Init)