comparison src/Function.cc @ 268:61830a4f9ab9

Improve formatting
author Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
date Thu, 14 Aug 2014 12:26:55 +0200
parents 8fe68d94ab76
children f4d6ae912a08
comparison
equal deleted inserted replaced
267:53039ac90368 268:61830a4f9ab9
23 #include <boost/mpi.hpp> 23 #include <boost/mpi.hpp>
24 #include <boost/serialization/vector.hpp> 24 #include <boost/serialization/vector.hpp>
25 #include <boost/serialization/utility.hpp> 25 #include <boost/serialization/utility.hpp>
26 #endif 26 #endif
27 27
28 void copy_vector (Array <double> const&, SHARED_PTR <dolfin::GenericVector>); 28 void copy_vector (Array <double> const &, SHARED_PTR <dolfin::GenericVector>);
29 29
30 DEFUN_DLD (Function, args, , "-*- texinfo -*-\n\ 30 DEFUN_DLD (Function, args, , "-*- texinfo -*-\n\
31 @deftypefn {Function File} {[@var{func}]} = \ 31 @deftypefn {Function File} {[@var{func}]} = \
32 Function (@var{name}, @var{FunctionSpace} (or @var{Function}),\ 32 Function (@var{name}, @var{FunctionSpace} (or @var{Function}),\
33 @var{Vector} (or @var{index}))\n\ 33 @var{Vector} (or @var{index}))\n\
59 @end deftypefn") 59 @end deftypefn")
60 { 60 {
61 61
62 int nargin = args.length (); 62 int nargin = args.length ();
63 octave_value retval; 63 octave_value retval;
64 64
65 if (nargin < 3 || nargin > 3) 65 if (nargin < 3 || nargin > 3)
66 print_usage (); 66 { print_usage (); }
67 else 67 else
68 { 68 {
69 if (! functionspace_type_loaded) 69 if (! functionspace_type_loaded)
70 { 70 {
71 functionspace::register_type (); 71 functionspace::register_type ();
82 82
83 if (args(1).type_id () == functionspace::static_type_id ()) 83 if (args(1).type_id () == functionspace::static_type_id ())
84 { 84 {
85 std::string str = args(0).string_value (); 85 std::string str = args(0).string_value ();
86 const functionspace & fspo = 86 const functionspace & fspo =
87 static_cast<const functionspace&> (args(1).get_rep ()); 87 static_cast<const functionspace &> (args(1).get_rep ());
88 Array <double> myu = args(2).array_value (); 88 Array <double> myu = args(2).array_value ();
89 89
90 if (!error_state) 90 if (!error_state)
91 { 91 {
92 const SHARED_PTR <const dolfin::FunctionSpace> 92 const SHARED_PTR <const dolfin::FunctionSpace>
93 & V = fspo.get_pfsp (); 93 & V = fspo.get_pfsp ();
94 94
95 if (V->dim () != myu.length ()) 95 if (V->dim () != myu.length ())
96 error("The size of the functional space \ 96 {
97 and of the vector must agree"); 97 error ("The size of the functional space \
98 and of the vector must agree");
99 }
98 else 100 else
99 { 101 {
100 dolfin::Function aux (V); 102 dolfin::Function aux (V);
101 copy_vector (myu, aux.vector ()); 103 copy_vector (myu, aux.vector ());
102 104
103 SHARED_PTR <dolfin::Function const> 105 SHARED_PTR <dolfin::Function const>
104 u (new dolfin::Function (aux)); 106 u (new dolfin::Function (aux));
105 retval = new function (str, u); 107 retval = new function (str, u);
106 } 108 }
107 } 109 }
108 } 110 }
109 else if (args(1).type_id () == function::static_type_id ()) 111 else if (args(1).type_id () == function::static_type_id ())
110 { 112 {
111 std::string str = args(0).string_value (); 113 std::string str = args(0).string_value ();
112 const function & fspo = 114 const function & fspo =
113 static_cast<const function&> (args(1).get_rep ()); 115 static_cast<const function &> (args(1).get_rep ());
114 int idx = args(2).int_value (); 116 int idx = args(2).int_value ();
115 117
116 if (!error_state) 118 if (!error_state)
117 { 119 {
118 const SHARED_PTR <const dolfin::Function> 120 const SHARED_PTR <const dolfin::Function>
122 error ("Function: The function you provided\ 124 error ("Function: The function you provided\
123 isn't a vecotr field"); 125 isn't a vecotr field");
124 else 126 else
125 { 127 {
126 if (idx < 1 || idx > f->value_dimension (0)) 128 if (idx < 1 || idx > f->value_dimension (0))
127 error ("Function: index out of bounds"); 129 { error ("Function: index out of bounds"); }
128 else 130 else
129 { 131 {
130 SHARED_PTR <const dolfin::Function> 132 SHARED_PTR <const dolfin::Function>
131 u (new dolfin::Function((*f)[idx - 1])); 133 u (new dolfin::Function ((*f)[idx - 1]));
132 134
133 retval = new function (str, u); 135 retval = new function (str, u);
134 } 136 }
135 } 137 }
136 } 138 }
137 } 139 }
138 } 140 }
139 return retval; 141 return retval;
140 } 142 }
141 143
142 void 144 void
143 copy_vector (Array <double> const& arr, SHARED_PTR <dolfin::GenericVector> vec) 145 copy_vector (Array <double> const & arr, SHARED_PTR <dolfin::GenericVector> vec)
144 { 146 {
145 unsigned const size = 147 unsigned const size =
146 #ifdef LATEST_DOLFIN 148 #ifdef LATEST_DOLFIN
147 dolfin::MPI::size (MPI_COMM_WORLD); 149 dolfin::MPI::size (MPI_COMM_WORLD);
148 #else 150 #else
158 std::pair <std::size_t, std::size_t> const loc_range = 160 std::pair <std::size_t, std::size_t> const loc_range =
159 vec->local_range (); 161 vec->local_range ();
160 162
161 for (unsigned p = 1; p < size; ++p) 163 for (unsigned p = 1; p < size; ++p)
162 if (rank == p) 164 if (rank == p)
163 world.send (0, p, loc_range); 165 { world.send (0, p, loc_range); }
164 166
165 if (rank == 0) 167 if (rank == 0)
166 { 168 {
167 for (unsigned p = 0; p < size; ++p) 169 for (unsigned p = 0; p < size; ++p)
168 { 170 {
169 std::pair <std::size_t, std::size_t> range; 171 std::pair <std::size_t, std::size_t> range;
170 if (p == 0) 172 if (p == 0)
171 range = loc_range; 173 { range = loc_range; }
172 else 174 else
173 world.recv (p, p, range); 175 { world.recv (p, p, range); }
174 176
175 std::vector <double> entries; 177 std::vector <double> entries;
176 for (std::size_t i = range.first; i < range.second; ++i) 178 for (std::size_t i = range.first; i < range.second; ++i)
177 entries.push_back (arr(i)); 179 { entries.push_back (arr(i)); }
178 180
179 if (p == 0) 181 if (p == 0)
180 locvals = entries; 182 { locvals = entries; }
181 else 183 else
182 world.send (p, p, entries); 184 { world.send (p, p, entries); }
183 } 185 }
184 } 186 }
185 187
186 for (unsigned p = 1; p < size; ++p) 188 for (unsigned p = 1; p < size; ++p)
187 { 189 {
188 if (rank == p) 190 if (rank == p)
189 world.recv (0, p, locvals); 191 { world.recv (0, p, locvals); }
190 } 192 }
191 193
192 vec->set_local (locvals); 194 vec->set_local (locvals);
193 } 195 }
194 else 196 else
195 for (std::size_t i = 0; i < arr.length (); ++i) 197 for (std::size_t i = 0; i < arr.length (); ++i)
196 vec->setitem (i, arr(i)); 198 { vec->setitem (i, arr (i)); }
197 199
198 vec->apply ("insert"); 200 vec->apply ("insert");
199 201
200 return; 202 return;
201 } 203 }