14 : _nelem(0), _nvert_e(
nvert_e), _ndof_e(ndof_e), _nnode(0), _ndim(
ndim), _ia(0), _xc(0)
21void Mesh::SetValues(std::vector<double> &
v,
const std::function<
double(
double,
double)> &func)
const
24 assert(
nnode ==
static_cast<int>(
v.size()) );
25 for (
int k = 0; k <
nnode; ++k)
27 v[k] = func( _xc[2 * k], _xc[2 * k + 1] );
34 cout <<
"\n ############### Debug M E S H ###################\n";
35 cout <<
"\n ............... Coordinates ...................\n";
36 for (
int k = 0; k < _nnode; ++k)
39 for (
int i = 0; i < _ndof_e; ++i )
41 cout << _xc[2*k+i] <<
" ";
45 cout <<
"\n ............... Elements ...................\n";
46 for (
int k = 0; k < _nelem; ++k)
49 for (
int i = 0; i < _ndof_e; ++i )
50 cout << _ia[_ndof_e * k + i] <<
" ";
58 assert(
Nnodes() ==
static_cast<int>(
v.size()));
61 if ( !fout.is_open() )
63 cout <<
"\nFile " << fname <<
" has not been opened.\n\n" ;
64 assert( fout.is_open() &&
"File not opened." );
67 string const DELIMETER(
" ");
74 assert(
Nnodes()*
Ndims() ==
static_cast<int>(_xc.size()));
75 for (
int k = 0, kj = 0; k <
Nnodes(); ++k)
77 for (
int j = 0; j <
Ndims(); ++j, ++kj)
79 fout << _xc[kj] << DELIMETER;
86 for (
int k = 0, kj = 0; k <
Nelems(); ++k)
90 fout << _ia[kj] + OFFSET << DELIMETER;
96 for (
int k = 0; k <
Nnodes(); ++k)
109 const string exec_m(
"matlab -nosplash < visualize_results.m");
113 const string fname(
"uv.txt");
116 int ierror = system(exec_m.c_str());
120 cout << endl <<
"Check path to Matlab/octave on your system" << endl;
132 _myid(myid), _procx(procx), _procy(procy), _neigh{{-1, -1, -1, -1}}, _color(0),
133_xl(0.0), _xr(1.0), _yb(0.0), _yt(1.0), _nx(nx), _ny(ny)
136 int const ky = _myid / _procx;
137 int const kx = _myid % _procy;
139 _neigh[0] = (ky == 0) ? -1 : _myid - _procx;
140 _neigh[1] = (kx == _procx - 1) ? -1 : _myid + 1;
141 _neigh[2] = (ky == _procy - 1) ? -1 : _myid + _procx;
142 _neigh[3] = (kx == 0) ? -1 : _myid - 1;
144 _color = (kx + ky) & 1 ;
147 double const hx = 1. / _procx;
148 double const hy = 1. / _procy;
155 int const nnode = (_nx + 1) * (_ny + 1);
156 Resize_Coords(
nnode, 2);
157 GetCoordsInRectangle(_nx, _ny, _xl, _xr, _yb, _yt, GetCoords().data());
160 int const nelem = 2 * _nx * _ny;
161 Resize_Connectivity(
nelem, 3);
162 GetConnectivityInRectangle(_nx, _ny, GetConnectivity().data());
171 for (
int j = 0; j <= _ny; ++j)
174 for (
int i = 0; i <= _nx; ++i, ++k)
185 for (
int j = 0; j <= _ny; ++j)
188 for (
int i = 0; i <= _nx; ++i, ++k)
203 tl = _ny * (_nx + 1),
204 tr = (_ny + 1) * (_nx + 1) - 1;
205 int const start[4] = { bl, br, tl, bl},
206 end[4] = { br, tr, tr, tl},
207 step[4] = { dx, dy, dx, dy};
210 for (
int j = 0; j < 4; j++)
214 for (
int i = start[j]; i <= end[j]; i += step[j])
221 sort(idx.begin(), idx.end());
222 idx.erase( unique(idx.begin(), idx.end()), idx.end() );
230 const string tmp( std::to_string(_myid / 100) + to_string((_myid % 100) / 10) + to_string(_myid % 10) );
232 const string namep = name +
"." +
tmp;
233 ofstream ff(namep.c_str());
235 ff.setf(ios::fixed, ios::floatfield);
241 for (
int j = 0; j <= _ny; ++j)
243 for (
int i = 0; i <= _nx; ++i, ++k)
244 ff <<
xc[2 * k + 0] <<
" " <<
xc[2 * k + 1] <<
" " << u[k] << endl;
252void Mesh_2d_3_square::GetCoordsInRectangle(
int const nx,
int const ny,
253 double const xl,
double const xr,
double const yb,
double const yt,
256 const double hx = (xr - xl) / nx,
260 for (
int j = 0; j <= ny; ++j)
262 const double y0 = yb + j * hy;
263 for (
int i = 0; i <= nx; ++i, k += 2)
265 xc[k ] = xl + i * hx;
273void Mesh_2d_3_square::GetConnectivityInRectangle(
int const nx,
int const ny,
int ia[])
275 const int dx = nx + 1;
278 for (
int j = 0; j < ny; ++j, ++k)
280 for (
int i = 0; i < nx; ++i, ++k)
284 ia[l + 2] = k + dx + 1;
288 ia[l + 2] = k + dx + 1;
301void GetBound(
int const ib,
int const nx,
int const ny,
double const w[],
double s[])
308 tr = (ny + 1) * (nx + 1) - 1;
313 for (
int i = bl, j = 0; i <= br; ++i, ++j)
319 for (
int i = tl, j = 0; i <= tr; ++i, ++j)
325 for (
int i = bl, j = 0; i <= tl; i += dy, ++j)
331 for (
int i = br, j = 0; i <= tr; i += dy, ++j)
337 cout << endl <<
"Wrong parameter ib in " << __FILE__ <<
":" << __LINE__ << endl;
347void AddBound(
int const ib,
int const nx,
int const ny,
double w[],
double const s[])
349 int const dy = nx + 1,
353 tr = (ny + 1) * (nx + 1) - 1;
358 for (
int i = bl, j = 0; i <= br; ++i, ++j)
364 for (
int i = tl, j = 0; i <= tr; ++i, ++j)
370 for (
int i = bl, j = 0; i <= tl; i += dy, ++j)
376 for (
int i = br, j = 0; i <= tr; i += dy, ++j)
382 cout << endl <<
"Wrong parameter ib in " << __FILE__ <<
":" << __LINE__ << endl;
396 if (!(ifs.is_open() && ifs.good()))
398 cerr <<
"Mesh_2d_3_matlab: Error cannot open file " << fname << endl;
399 assert(ifs.is_open());
403 cout <<
"ASCI file " << fname <<
" opened" << endl;
434 bedges.resize(nbedges * 2);
435 for (
int k = 0; k < nbedges * 2; ++k)
497 vector<int> idx(bedges);
499 sort(idx.begin(), idx.end());
500 idx.erase( unique(idx.begin(), idx.end()), idx.end() );
function vertex minimal boundary edge info in an ASCII file Matlab indexing is stored(starts with 1). % % The output file format is compatible with Mesh_2d_3_matlab nnode
Mesh_2d_3_matlab(std::string const &fname)
std::vector< int > Index_DirichletNodes() const override
void SetU(std::vector< double > &u) const
std::vector< int > Index_DirichletNodes() const override
void SaveVectorP(std::string const &name, std::vector< double > const &u) const
void SetF(std::vector< double > &f) const
Mesh_2d_3_square(int nx, int ny, int myid=0, int procx=1, int procy=1)
const std::vector< double > & GetCoords() const
void Resize_Connectivity(int nelem, int nvert_e)
void Resize_Coords(int nnodes, int ndim)
Mesh(int ndim, int nvert_e=0, int ndof_e=0)
void Write_ascii_matlab(std::string const &fname, std::vector< double > const &v) const
int NverticesElements() const
const std::vector< int > & GetConnectivity() const
void SetValues(std::vector< double > &v, const std::function< double(double, double)> &func) const
void Visualize(std::vector< double > const &v) const
void AddBound(int const ib, int const nx, int const ny, double w[], double const s[])
void GetBound(int const ib, int const nx, int const ny, double const w[], double s[])