main.cpp 27.7 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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "inmost.h"
using namespace INMOST;

#ifndef M_PI
#define M_PI 3.141592653589
#endif

#if defined(USE_MPI)
#define BARRIER MPI_Barrier(MPI_COMM_WORLD);
#else
#define BARRIER
#endif

bool report_residual = false;
bool write_steps = false;
bool two_point = false;

//shortcuts
typedef Storage::bulk bulk;
typedef Storage::real real;
typedef Storage::integer integer;
typedef Storage::enumerator enumerator;
typedef Storage::real_array real_array;
typedef Storage::var_array var_array;


variable refU(real xyz[3], real nu, real t)
{
	unknown x(xyz[0],0);
	unknown y(xyz[1],1);
	unknown z(xyz[2],2);
	const real a = M_PI/4.0;
	const real d = M_PI/2.0;
	return -a*(exp(a*x)*sin(a*y+d*z)+exp(a*z)*cos(a*x+d*y))*exp(-nu*d*d*t);
}

variable refV(real xyz[3], real nu, real t)
{
	unknown x(xyz[0],0);
	unknown y(xyz[1],1);
	unknown z(xyz[2],2);
	const real a = M_PI/4.0;
	const real d = M_PI/2.0;
	return -a*(exp(a*y)*sin(a*z+d*x)+exp(a*x)*cos(a*y+d*z))*exp(-nu*d*d*t);
}

variable refW(real xyz[3], real nu, real t)
{
	unknown x(xyz[0],0);
	unknown y(xyz[1],1);
	unknown z(xyz[2],2);
	const real a = M_PI/4.0;
	const real d = M_PI/2.0;
	return -a*(exp(a*z)*sin(a*x+d*y)+exp(a*y)*cos(a*z+d*x))*exp(-nu*d*d*t);
}

real refP(real xyz[3], real nu, real t)
{
	real x = xyz[0];
	real y = xyz[1];
	real z = xyz[2];
	const real a = M_PI/4.0;
	const real d = M_PI/2.0;
	return -a*a/2.0*(exp(2*a*x)+exp(2*a*y)+exp(2*a*z)+
					 2*sin(a*x+d*y)*cos(a*z+d*x)*exp(a*(y+z))+
					 2*sin(a*y+d*z)*cos(a*x+d*y)*exp(a*(z+x))+
					 2*sin(a*z+d*x)*cos(a*y+d*z)*exp(a*(x+y)))*exp(-2*nu*d*d*t);
}

// (done)todo: adapt for parallel use
Kirill Terekhov's avatar
Kirill Terekhov committed
76
void CheckResidual(Mesh & m, const TagReal & tag_P, const TagReal & tag_nU, real visc, real T, MarkerType mrk)
77 78 79 80 81 82 83 84 85
{
	{
		TagReal      tag_refP = m.CreateTag("refP" ,DATA_REAL,CELL|FACE,NONE,1);
		real p_ref_min = 1.0e20, p_ref_max = -1.0e20;
		real p_min = 1.0e20, p_max = -1.0e20;
		for(ElementType etype = FACE; etype <= CELL; etype = NextElementType(etype) )
			for(integer it = 0; it < m.LastLocalID(etype); ++it) if( m.isValidElement(etype,it) )
			{
				Element e = m.ElementByLocalID(etype,it);
Kirill Terekhov's avatar
Kirill Terekhov committed
86
				if( e.GetStatus() == Element::Ghost ) continue;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
				real x[3],p;
				e.Centroid(x);
				p = refP(x,visc,T);
				if( p < p_ref_min ) p_ref_min = p;
				if( p > p_ref_max ) p_ref_max = p;
				p = tag_P[e];
				if( p < p_min ) p_min = p;
				if( p > p_max ) p_max = p;
			}
		p_max = m.AggregateMax(p_max);
		p_min = m.AggregateMin(p_min);
		p_ref_max = m.AggregateMax(p_ref_max);
		p_ref_min = m.AggregateMin(p_ref_min);
		for(ElementType etype = FACE; etype <= CELL; etype = NextElementType(etype) )
			for(integer it = 0; it < m.LastLocalID(etype); ++it) if( m.isValidElement(etype,it) )
			{
				Element e = m.ElementByLocalID(etype,it);
Kirill Terekhov's avatar
Kirill Terekhov committed
104
				if( e.GetStatus() == Element::Ghost ) continue;
105 106 107 108 109 110 111 112 113 114 115 116
				real x[3];
				e.Centroid(x);
				tag_refP[e] = refP(x,visc,T) - p_ref_min;
			}
		if( m.GetProcessorRank() == 0 )
		{
			std::cout << "p_ref " << p_ref_min << ":" << p_ref_max << std::endl;
			std::cout << "p     " << p_min << ":" << p_max << std::endl;
		}
		TagReal      tag_E   = m.CreateTag("ERROR_P" ,DATA_REAL,CELL|FACE,NONE,1);
		TagRealArray tag_EU  = m.CreateTag("ERROR_U" ,DATA_REAL,CELL,NONE,3);
		TagRealArray tag_U  =  m.CreateTag("U" ,DATA_REAL,CELL,NONE,3);
Kirill Terekhov's avatar
Kirill Terekhov committed
117
		TagRealArray tag_refU  =  m.CreateTag("refU" ,DATA_REAL,CELL,NONE,3);
118 119 120 121 122 123 124 125
		TagReal      tag_EnU = m.CreateTag("ERROR_nU",DATA_REAL,FACE,NONE,1);
		real C, L2, volume, Cu[3], L2u[3], Cnu, L2nu;
		C = L2 = volume = 0.0;
		Cu[0] = Cu[1] = Cu[2] = L2u[0] = L2u[1] = L2u[2] = 0;
		for(integer q = 0; q < m.CellLastLocalID(); ++q ) if( m.isValidCell(q) )
		{
			Cell c = m.CellByLocalID(q);
			if( c.GetStatus() == Element::Ghost ) continue;
Kirill Terekhov's avatar
Kirill Terekhov committed
126
			real x[3], z[3], n[3];
127 128 129 130 131 132 133 134
			c.Centroid(x);
			real err = tag_P[c] - p_min - tag_refP[c];
			real vol = c.Volume();
			if( C < fabs(err) ) C = fabs(err);
			L2 += err*err*vol;
			volume += vol;
			tag_E[c] = err;
			
Kirill Terekhov's avatar
Kirill Terekhov committed
135 136 137
			real V[3],Q[3]; //restored velocity
			V[0] = V[1] = V[2] = 0;
			Q[0] = Q[1] = Q[2] = 0;
138 139 140 141
			real a,s,v;
			ElementArray<Face> cfaces = c.getFaces();
			for(integer kt = 0; kt < cfaces.size(); ++kt)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
142 143
				//cfaces[kt].FixNormalOrientation();
				//if( !cfaces[kt].FixNormalOrientation() ) std::cout << "Orientation is bad" << std::endl;
144
				cfaces[kt].Centroid(z);
Kirill Terekhov's avatar
Kirill Terekhov committed
145
				cfaces[kt].UnitNormal(n);
146 147
				a = cfaces[kt].Area();
				s = cfaces[kt].FaceOrientedOutside(c) ? 1.0 : -1.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
148
				s*= cfaces[kt].GetMarker(mrk) ? -1.0 : 1.0;
149
				v = tag_nU[cfaces[kt]]*s*a;
Kirill Terekhov's avatar
Kirill Terekhov committed
150 151 152 153
				//v = n[0]*refU(z,visc,T).GetValue()
				//  + n[1]*refV(z,visc,T).GetValue()
				//  + n[2]*refW(z,visc,T).GetValue();
				//v*= a*s;
154 155 156 157 158 159 160 161 162 163 164 165
				V[0] += v*(z[0]-x[0]);
				V[1] += v*(z[1]-x[1]);
				V[2] += v*(z[2]-x[2]);
			}
			V[0] /= c.Volume();
			V[1] /= c.Volume();
			V[2] /= c.Volume();
			
			tag_U[c][0] = V[0];
			tag_U[c][1] = V[1];
			tag_U[c][2] = V[2];
			
Kirill Terekhov's avatar
Kirill Terekhov committed
166 167 168 169 170
			tag_refU[c][0] = refU(x,visc,T).GetValue();
			tag_refU[c][1] = refV(x,visc,T).GetValue();
			tag_refU[c][2] = refW(x,visc,T).GetValue();
			
			err = V[0] - tag_refU[c][0];
171 172 173 174
			if( Cu[0] < fabs(err) ) Cu[0] = fabs(err);
			L2u[0] += err*err*vol;
			tag_EU[c][0] = err;
			
Kirill Terekhov's avatar
Kirill Terekhov committed
175
			err = V[1] - tag_refU[c][1];
176 177 178 179
			if( Cu[1] < fabs(err) ) Cu[1] = fabs(err);
			L2u[1] += err*err*vol;
			tag_EU[c][1] = err;
			
Kirill Terekhov's avatar
Kirill Terekhov committed
180
			err = V[2] - tag_refU[c][2];
181 182 183 184 185
			if( Cu[2] < fabs(err) ) Cu[2] = fabs(err);
			L2u[2] += err*err*vol;
			tag_EU[c][2] = err;
		}
		m.ExchangeData(tag_U,CELL,0);
Kirill Terekhov's avatar
Kirill Terekhov committed
186
		m.ExchangeData(tag_refU,CELL,0);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
		volume = m.Integrate(volume);
		L2 = sqrt(m.Integrate(L2)/volume);
		m.Integrate(L2u,3);
		L2u[0] = sqrt(L2u[0]/volume);
		L2u[1] = sqrt(L2u[1]/volume);
		L2u[2] = sqrt(L2u[2]/volume);
		C = m.AggregateMax(C);
		m.AggregateMax(Cu,3);
		if( m.GetProcessorRank() == 0 )
		{
			std::cout << "Pressure error on cells, C-norm " << C << " L2-norm " << L2 << std::endl;
			std::cout << "Reconstructed U error on cells, C-norm " << Cu[0] << " L2-norm " << L2u[0] << std::endl;
			std::cout << "Reconstructed V error on cells, C-norm " << Cu[1] << " L2-norm " << L2u[1] << std::endl;
			std::cout << "Reconstructed W error on cells, C-norm " << Cu[2] << " L2-norm " << L2u[2] << std::endl;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
202
		
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
		C = L2 = volume = 0.0;
		Cnu = L2nu = 0.0;
		for( int q = 0; q < m.FaceLastLocalID(); ++q ) if( m.isValidFace(q) )
		{
			Face f = m.FaceByLocalID(q);
			
			if( f.GetStatus() == Element::Ghost ) continue;
			
			real x[3],y[3],n[3];
			f.Centroid(x);
			f.UnitNormal(n);
			f.BackCell().Centroid(y);
			real H = n[0]*(x[0]-y[0])+n[1]*(x[1]-y[1])+n[2]*(x[2]-y[2]);
			if( f.FrontCell().isValid() )
			{
				f.FrontCell().Centroid(y);
				H += n[0]*(y[0]-x[0])+n[1]*(y[1]-x[1])+n[2]*(y[2]-x[2]);
			}
			real err = tag_P[f] - p_min - tag_refP[f];
			real vol = f.Area()*H;
			if( C < fabs(err) ) C = fabs(err);
			L2 += err*err*vol;
			volume += vol;
			tag_E[f] = err;
			
			real V[3], nV;
			V[0] = refU(x,visc,T).GetValue();
			V[1] = refV(x,visc,T).GetValue();
			V[2] = refW(x,visc,T).GetValue();
Kirill Terekhov's avatar
Kirill Terekhov committed
232
			nV = (n[0]*V[0]+n[1]*V[1]+n[2]*V[2])*(f.GetMarker(mrk)?-1:1);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
			err = tag_nU[f] - nV;
			
			if( Cnu < fabs(err) ) Cnu = fabs(err);
			L2nu += err*err*vol;
			volume += vol;
			tag_EnU[f] = err;
		}
		volume = m.Integrate(volume);
		L2 = sqrt(m.Integrate(L2)/volume);
		L2nu = sqrt(m.Integrate(L2nu)/volume);
		C = m.AggregateMax(C);
		Cnu = m.AggregateMax(Cnu);
		if( m.GetProcessorRank() == 0 )
		{
			if( !two_point ) std::cout << "Pressure error on faces, C-norm " << C << " L2-norm " << L2 << std::endl;
			std::cout << "Normal velocity error on faces, C-norm " << Cnu << " L2-norm " << L2nu << std::endl;
		}
		
		real div_max = -1.0e20, div_min = 1.0e20, div_int = 0;
		for(integer it = 0; it < m.CellLastLocalID(); ++it) if( m.isValidCell(it) ) //loop over cells
		{
			Cell c = m.CellByLocalID(it);
			if( c.GetStatus() == Element::Ghost ) continue;
			ElementArray<Face> cfaces = c.getFaces(); //obtain faces of the cell
			real div = 0, a, s;
			for(integer kt = 0; kt < cfaces.size(); ++kt)
			{
				a = cfaces[kt].Area();
				s = cfaces[kt].FaceOrientedOutside(c)?1.0:-1.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
262
				s*= cfaces[kt].GetMarker(mrk) ? -1.0 : 1.0;
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
				div += tag_nU[cfaces[kt]]*a*s;
			}
			if( div > div_max ) div_max = div;
			if( div < div_min ) div_min = div;
			div_int += div;
		}
		div_max = m.AggregateMax(div_max);
		div_min = m.AggregateMin(div_min);
		div_int = m.Integrate(div_int);
		if( m.GetProcessorRank() == 0 )
		{
			std::cout << "Divergence of velocity: " << div_min << ":" << div_max << " integral " << div_int << std::endl;
		}
	}
}
		
int main(int argc,char ** argv)
{
    Solver::Initialize(&argc,&argv,""); // Initialize the solver and MPI activity
#if defined(USE_PARTITIONER)
    Partitioner::Initialize(&argc,&argv); // Initialize the partitioner activity
#endif
    if( argc > 1 )
    {
		
		//parameters
		
		real dT = 1.0/50.0;
		real T = 1.0/10.0;
		real visc = 0.0;
		
		if( argc > 2 ) dT = atof(argv[2]);
		if( argc > 3 ) T = atof(argv[3]);
		if( argc > 4 ) visc = atof(argv[4]);
		
		if( visc > 0 )
		{
			std::cout << "Viscosity is not calculated right now." << std::endl;
			visc = 0;
		}
		
        bool repartition = false; // Is it required to redistribute the mesh?
        Mesh m; // Create an empty mesh
        { // Load the mesh
            m.SetCommunicator(INMOST_MPI_COMM_WORLD); // Set the MPI communicator for the mesh
            if( m.GetProcessorRank() == 0 ) std::cout << "Processors: " << m.GetProcessorsNumber() << std::endl;
            if( m.isParallelFileFormat(argv[1]) ) //The format is
            {
                m.Load(argv[1]); // Load mesh from the parallel file format
                repartition = true; // Ask to repartition the mesh
            }
            else if( m.GetProcessorRank() == 0 ) m.Load(argv[1]); // Load mesh from the serial file format
        }

		
#if defined(USE_PARTITIONER)
        if (m.GetProcessorsNumber() > 1 )//&& !repartition) // Currently only non-distributed meshes are supported by Inner_RCM partitioner
        {
            { // Compute mesh partitioning
                Partitioner p(&m); //Create Partitioning object
                p.SetMethod(Partitioner::Inner_RCM,repartition ? Partitioner::Repartition : Partitioner::Partition); // Specify the partitioner
                p.Evaluate(); // Compute the partitioner and store new processor ID in the mesh
            }

            { //Distribute the mesh
                m.Redistribute(); // Redistribute the mesh data
                m.ReorderEmpty(CELL|FACE|EDGE|NODE); // Clean the data after reordring
            }
        }
#endif
		//map grid into [-1,1] box
		real xmax[3] = {-1.0e20,-1.0e20,-1.0e20};
		real xmin[3] = {+1.0e20,+1.0e20,+1.0e20};
		for(integer it = 0; it < m.NodeLastLocalID(); ++it) if( m.isValidNode(it) )
		{
			real_array c = m.NodeByLocalID(it).Coords();
			for(int k = 0; k < 3; ++k)
			{
				if( xmin[k] > c[k] ) xmin[k] = c[k];
				if( xmax[k] < c[k] ) xmax[k] = c[k];
			}
		}
		m.AggregateMax(xmax,3);
		m.AggregateMin(xmin,3);
		
		for(integer it = 0; it < m.NodeLastLocalID(); ++it) if( m.isValidNode(it) )
		{
			real_array c = m.NodeByLocalID(it).Coords();
			for(int k = 0; k < 3; ++k)
				c[k] = 2.0*(c[k]-xmin[k])/(xmax[k]-xmin[k])-1.0;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
354 355 356
		
		
		
357 358 359 360 361 362 363
        { // prepare geometrical data on the mesh
            Mesh::GeomParam table;
            table[CENTROID]    = CELL | FACE; //Compute averaged center of mass
            table[NORMAL]      = FACE;        //Compute normals
            table[ORIENTATION] = FACE;        //Check and fix normal orientation
            table[MEASURE]     = CELL | FACE; //Compute volumes and areas
            //table[BARYCENTER]  = CELL | FACE; //Compute volumetric center of mass
Kirill Terekhov's avatar
Kirill Terekhov committed
364
			m.RemoveGeometricData(table);
365 366
            m.PrepareGeometricData(table); //Ask to precompute the data
        }
Kirill Terekhov's avatar
Kirill Terekhov committed
367 368
		
		
369 370 371 372 373 374 375
        // data tags for
		TagReal tag_nU; // Normal component of velocity
		TagReal tag_nUold; // on previous step
        TagReal tag_P;  // Pressure
		TagReal tag_Q;  // Pressure update
        TagReal tag_F;  // Forcing term
        TagRealArray tag_W; // Gradient matrix
Kirill Terekhov's avatar
Kirill Terekhov committed
376
		TagInteger tag_i; // row number for current face in back cell matrix
377
		
Kirill Terekhov's avatar
Kirill Terekhov committed
378
		
379 380 381 382
        if( m.GetProcessorsNumber() > 1 ) //skip for one processor job
        { // Exchange ghost cells
            m.ExchangeGhost(1,FACE); // Produce layer of ghost cells
        }
Kirill Terekhov's avatar
Kirill Terekhov committed
383 384 385 386 387 388 389 390
		
		
		MarkerType nrm_ornt = m.CreateMarker();
		MarkerType boundary = m.CreateMarker();
		
		m.MarkNormalOrientation(nrm_ornt);
		m.MarkBoundaryFaces(boundary);
		
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
        { //initialize data
            tag_P = m.CreateTag("PRESSURE",DATA_REAL,CELL|FACE,NONE,1); // Create a new tag for the pressure
			tag_W = m.CreateTag("W",DATA_REAL,CELL,NONE);
			tag_i = m.CreateTag("row",DATA_INTEGER,FACE,NONE,1);
			tag_F = m.CreateTag("FORCE",DATA_REAL,CELL,NONE,1);
			tag_Q = m.CreateTag("PUPDATE",DATA_REAL,CELL|FACE,NONE,1);
			tag_nU = m.CreateTag("NORMAL_VELOCITY",DATA_REAL,FACE,NONE,1);
			tag_nUold = m.CreateTag("NORMAL_VELOCITY_OLD",DATA_REAL,FACE,NONE,1);
			//Assemble gradient matrix W on cells
#if defined(USE_OMP)
#pragma omp parallel for
#endif
            for(integer it = 0; it < m.CellLastLocalID(); ++it ) if( m.isValidCell(it) )
            {
                Cell c = m.CellByLocalID(it);
				ElementArray<Face> faces = c.getFaces(); //obtain faces of the cell
Kirill Terekhov's avatar
Kirill Terekhov committed
407 408
				Matrix<real,real_array> W(tag_W[c]);
				W.Resize(faces.size(),faces.size());
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
				real xP[3]; //center of the cell
				real yF[3]; //center of the face
				real nF[3]; //normal to the face
				real aF; //area of the face
				real vP = c.Volume(); //volume of the cell
				c.Centroid(xP);
				rMatrix N(faces.size(),3), R(faces.size(),3); //big gradient matrix, co-normals, directions
				for(int k = 0; k < faces.size(); ++k) //loop over faces
				{
					aF = faces[k].Area();
					faces[k].Centroid(yF);
					faces[k].OrientedUnitNormal(c,nF);
					// assemble matrix of directions
					R(k,0) = (yF[0]-xP[0])*aF;
					R(k,1) = (yF[1]-xP[1])*aF;
					R(k,2) = (yF[2]-xP[2])*aF;
					// assemble matrix of co-normals
					N(k,0) = nF[0];
					N(k,1) = nF[1];
					N(k,2) = nF[2];
					// remember position
					if( faces[k].BackCell() == c )
						tag_i[faces[k]] = k;
				} //end of loop over faces
				W = N*(N.Transpose()*R).Invert(true).first*N.Transpose(); //stability part
				W += (rMatrix::Unit(faces.size()) - R*(R.Transpose()*R).Invert(true).first*R.Transpose())*(4.0/faces.size()*W.Trace());
            } //end of loop over cells
			
			//initialize normal velocity
#if defined(USE_OMP)
#pragma omp parallel for
#endif
			for(integer it = 0; it < m.FaceLastLocalID(); ++it) if( m.isValidFace(it) )
			{
				Face f = m.FaceByLocalID(it);
				real x[3], n[3], u[3];
				f.UnitNormal(n);
				f.Centroid(x);
				u[0] = refU(x,visc,0).GetValue();
				u[1] = refV(x,visc,0).GetValue();
				u[2] = refW(x,visc,0).GetValue();
Kirill Terekhov's avatar
Kirill Terekhov committed
450
				tag_nUold[f] = tag_nU[f] = (u[0]*n[0]+u[1]*n[1]+u[2]*n[2])*(f.GetMarker(nrm_ornt)?-1:1);
451 452 453 454
				//pressure
				tag_P[f] = refP(x,visc,0);
			}
			
Kirill Terekhov's avatar
Kirill Terekhov committed
455
			
456 457 458 459 460 461 462 463 464 465 466
			//initialize pressure
#if defined(USE_OMP)
#pragma omp parallel for
#endif
			for(integer it = 0; it < m.CellLastLocalID(); ++it) if( m.isValidCell(it) )
			{
				Cell c = m.CellByLocalID(it);
				real x[3];
				c.Centroid(x);
				tag_P[c] = refP(x,visc,0);
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
467 468 469 470 471 472 473 474
			
			//CheckResidual(m,tag_P,tag_nU,visc,0,nrm_ornt);
			
			m.ExchangeData(tag_nU,FACE,0);
			m.ExchangeData(tag_nUold,FACE,0);
			
			//CheckResidual(m,tag_P,tag_nU,visc,0,nrm_ornt);
			
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
        } //end of initialize data
		
		

        if( m.GetProcessorRank() == 0 ) std::cout << "Initialization done" << std::endl;
        { //Main loop for problem solution
            Automatizator aut; // declare class to help manage unknowns
			
			dynamic_variable Q(aut,aut.RegisterTag(tag_Q,two_point ? CELL : (CELL|FACE))); //register pressure as primary unknown
            aut.EnumerateTags(); //enumerate all primary variables
			
			std::cout << "Enumeration done, size " << aut.GetLastIndex() - aut.GetFirstIndex() << std::endl;

            Residual R("",aut.GetFirstIndex(),aut.GetLastIndex());
            Sparse::LockService Locks(aut.GetFirstIndex(),aut.GetLastIndex());
            Sparse::AnnotationService Text(aut.GetFirstIndex(),aut.GetLastIndex());
            Sparse::Vector Update  ("",aut.GetFirstIndex(),aut.GetLastIndex()); //vector for update
            {//Annotate matrix
                for( int q = 0; q < m.CellLastLocalID(); ++q ) if( m.isValidCell(q) )
                {
                    Cell cell = m.CellByLocalID(q);
                    if( cell.GetStatus() != Element::Ghost )
                        Text.SetAnnotation(Q.Index(cell),"Cell-centered pressure value");
                }
                if( !two_point ) for( int q = 0; q < m.FaceLastLocalID(); ++q ) if( m.isValidFace(q) )
                {
                    Face face = m.FaceByLocalID(q);
                    if( face.GetStatus() != Element::Ghost )
                    {
                        //if( tag_BC.isValid() && face.HaveData(tag_BC) )
                        //    Text.SetAnnotation(P.Index(face),"Pressure guided by boundary condition");
                        //else
                            Text.SetAnnotation(Q.Index(face),"Interface pressure");
                    }
                }
            }

            if( m.GetProcessorRank() == 0 )  std::cout << "Matrix was annotated" << std::endl;
			
			//checks
			/*
			for(integer it = 0; it < m.FaceLastLocalID(); ++it) if( m.isValidFace(it) )
			{
				Face f = m.FaceByLocalID(it);
				Cell c = f.BackCell();
				ElementArray<Face> cfaces = c.getFaces();
				Matrix<real,real_array> W(tag_W[c],cfaces.size(),cfaces.size());
				real ngrad[3] = {0,0,0};
				real n[3], xf[3], xc[3];
				f.UnitNormal(n);
				c.Centroid(xc);
				for(integer kt = 0; kt < cfaces.size(); ++kt)
				{
					f.Centroid(xf);
					ngrad[0] += W(tag_i[f],kt)*(xf[0] - xc[0])*cfaces[kt].Area();
					ngrad[1] += W(tag_i[f],kt)*(xf[1] - xc[1])*cfaces[kt].Area();
					ngrad[2] += W(tag_i[f],kt)*(xf[2] - xc[2])*cfaces[kt].Area();
				}
				std::cout << "1/area: " << 1.0/f.Area() << std::endl;
				std::cout << "normal: " << n[0] << " " << n[1] << " " << n[2] << std::endl;
				std::cout << "ngrad:  " << ngrad[0] << " " << ngrad[1] << " " << ngrad[2] << std::endl;
			}
			 */
			
			
			int step = 0;
			real alpha = 1, beta = -1, gamma = 0;
			for(real t = 0; t < T-1.0e-9; t+=dT)
			{
				//if( step == 1 ) break;
				if( t > T ) t = T;
				
				if( report_residual )
				{
					if( m.GetProcessorRank() == 0 )  std::cout << "Before velocity update " << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
550
					CheckResidual(m,tag_P,tag_nU,visc,t,nrm_ornt);
551 552 553 554 555 556 557 558 559
				}
				
				
#if defined(USE_OMP)
#pragma omp parallel for
#endif
				for(integer it = 0; it < m.FaceLastLocalID(); ++it) if( m.isValidFace(it) )
				{
					Face f = m.FaceByLocalID(it);
Kirill Terekhov's avatar
Kirill Terekhov committed
560 561 562
					if( f.GetStatus() == Element::Ghost ) continue;
					real x[3], n[3], V[3], nV, U, Uold, s;
					s = f.GetMarker(nrm_ornt) ? -1 : 1;
563 564 565 566 567 568 569 570 571 572
					f.UnitNormal(n);
					f.Centroid(x);
					U = tag_nU[f];
					Uold = tag_nUold[f];
					if( f.GetMarker(boundary) ) //Boundary condition
					{
						f.Centroid(x);
						V[0] = refU(x,visc,t+dT).GetValue();
						V[1] = refV(x,visc,t+dT).GetValue();
						V[2] = refW(x,visc,t+dT).GetValue();
Kirill Terekhov's avatar
Kirill Terekhov committed
573
						nV = (n[0]*V[0]+n[1]*V[1]+n[2]*V[2])*s;
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
						tag_nU[f] = nV;
						tag_nUold[f] = U;
					}
					else //Update with advection and pressure gradient
					{
						real ngradP = 0;
						if( two_point )
						{
							if( f.FrontCell().isValid() )
							{
								real xf[3],xb[3],n[3],d;
								f.UnitNormal(n);
								f.FrontCell().Centroid(xf);
								f.BackCell().Centroid(xb);
								d = n[0]*(xf[0]-xb[0]) + n[1]*(xf[1]-xb[1]) + n[2]*(xf[2]-xb[2]);
								ngradP = (tag_P[f.FrontCell()] - tag_P[f.BackCell()])/d;
							}
						}
						else
						{
							Cell c = f.BackCell();
							ElementArray<Face> cfaces = c.getFaces();
							Matrix<real,real_array> W(tag_W[c],cfaces.size(),cfaces.size());
							for(integer kt = 0; kt < cfaces.size(); ++kt)
								ngradP += W(tag_i[f],kt)*(tag_P[cfaces[kt]] - tag_P[c])*cfaces[kt].Area();
						}
						variable u = refU(x,visc,t+dT);
						variable v = refV(x,visc,t+dT);
						variable w = refW(x,visc,t+dT);
						V[0] = u.GetValue()*u.GetRow()[0] + v.GetValue()*u.GetRow()[1] + w.GetValue()*u.GetRow()[2];
						V[1] = u.GetValue()*v.GetRow()[0] + v.GetValue()*v.GetRow()[1] + w.GetValue()*v.GetRow()[2];
						V[2] = u.GetValue()*w.GetRow()[0] + v.GetValue()*w.GetRow()[1] + w.GetValue()*w.GetRow()[2];
Kirill Terekhov's avatar
Kirill Terekhov committed
606
						nV = (n[0]*V[0]+n[1]*V[1]+n[2]*V[2])*s;
607 608 609 610 611 612
						tag_nU[f] = -(beta*U + gamma*Uold + (nV + ngradP)*dT)/alpha;
						tag_nUold[f] = U;
					}
				}
				
				m.ExchangeData(tag_nU,FACE,0);
Kirill Terekhov's avatar
Kirill Terekhov committed
613
				m.ExchangeData(tag_nUold,FACE,0);
614 615 616 617
				
				if( report_residual )
				{
					if( m.GetProcessorRank() == 0 )  std::cout << "After velocity update " << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
618
					CheckResidual(m,tag_P,tag_nU,visc,t+dT,nrm_ornt);
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
				}
				
				R.Clear(); //clean up the residual
				
				//clean up Q just in case, check does this affect the result
				//for(ElementType etype = FACE; etype <= CELL; etype = NextElementType(etype) )
				//	for(integer it = 0; it < m.LastLocalID(etype); ++it) if( m.isValidElement(etype,it) )
				//		tag_Q[m.ElementByLocalID(etype,it)] = 0;
				
				
				Automatizator::MakeCurrent(&aut);
				
#if defined(USE_OMP)
#pragma omp parallel 
#endif
				for(integer it = 0; it < m.CellLastLocalID(); ++it) if( m.isValidCell(it) ) //loop over cells
				{
					Cell c = m.CellByLocalID(it);
					ElementArray<Face> cfaces = c.getFaces(); //obtain faces of the cell
					vMatrix FLUX(cfaces.size(),1); //computed flux on faces
					if( two_point )
					{
						for(integer kt = 0; kt < cfaces.size(); ++kt)
						{
							Face f = cfaces[kt];
							if( f.FrontCell().isValid() )
							{
								real xf[3],xb[3],n[3],d,s;
								f.UnitNormal(n);
								f.FrontCell().Centroid(xf);
								f.BackCell().Centroid(xb);
								d = n[0]*(xf[0]-xb[0]) + n[1]*(xf[1]-xb[1]) + n[2]*(xf[2]-xb[2]);
								s = f.FaceOrientedOutside(c) ? 1.0 : -1.0;
								FLUX(kt,0) = s*(Q[f.FrontCell()] - Q[f.BackCell()])/d;
							}
						}
					}
					else
					{
						Matrix<real,real_array> W(tag_W[c],cfaces.size(),cfaces.size()); //Matrix for gradient
						vMatrix dQ(cfaces.size(),1); //vector of pressure differences on faces
						for(integer kt = 0; kt < cfaces.size(); ++kt)
							dQ(kt,0) = (Q[cfaces[kt]] - Q[c])*cfaces[kt].Area();
						FLUX = W*dQ; //fluxes on faces
					}
					
					if( c.GetStatus() != Element::Ghost )
					{
						real div = 0, a, s;
						for(integer kt = 0; kt < cfaces.size(); ++kt)
						{
							a = cfaces[kt].Area();
							s = cfaces[kt].FaceOrientedOutside(c)?1.0:-1.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
672
							s*= cfaces[kt].GetMarker(nrm_ornt) ? -1 : 1;
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
							div += tag_nU[cfaces[kt]]*a*s;
						}
						tag_F[c] = div/dT;
						R[Q.Index(c)] = alpha*div/dT;
						for(integer kt = 0; kt < cfaces.size(); ++kt) //loop over faces of
							R[Q.Index(c)] += FLUX(kt,0)*cfaces[kt].Area();
					}
					if( !two_point ) for(integer kt = 0; kt < cfaces.size(); ++kt) //loop over faces of current cell
					{
						if( cfaces[kt].GetStatus() == Element::Ghost ) continue;
						int index = Q.Index(cfaces[kt]);
						Locks.Lock(index);
						R[index] += FLUX(kt,0);
						Locks.UnLock(index);
					}
				} //end of loop over cells
				
				{
					//set one of the boundary pressure corrections to be zero
					//(done) todo: in parallel processors should calculate number of local boundary interfaces
					//if all are neumann then the processor with minimum number should set one of the boundary
					//faces to zero
					//
					// todo: in future detect dirichlet bc, if they are present this is not needed
					if( m.GetProcessorRank() == 0 )
					{
						for(integer it = 0; it < m.CellLastLocalID(); ++it) if( m.isValidCell(it) )
						{
							Cell c = m.CellByLocalID(it);
							if( c.GetStatus() != Element::Ghost  )
							{
								R[Q.Index(c)] = Q[c] - 0.0;
								break;
							}
						}
					}
				}
				
				Automatizator::RemoveCurrent();
				
				//std::cout << "residual: " << R.Norm() << std::endl;
				
				//Solver S(Solver::SUPERLU);
				//Solver S(Solver::INNER_ILU2);
				Solver S(Solver::INNER_MPTILUC);
				//Solver S(Solver::INNER_DDPQILUC);
				S.SetParameter("relative_tolerance", "1.0e-14");
				S.SetParameter("absolute_tolerance", "1.0e-12");
				S.SetParameter("drop_tolerance", "5.0e-2");
				S.SetParameter("reuse_tolerance", "1.0e-3");
Kirill Terekhov's avatar
Kirill Terekhov committed
723
				S.SetParameter("schwartz_overlap", "2");
724 725 726 727 728 729 730 731 732 733 734 735 736 737
				S.SetMatrix(R.GetJacobian());;
				if( S.Solve(R.GetResidual(),Update) )
				{
					
					//real Q_min = 1.0e54, Q_max = -1.0e54;
					for(ElementType etype = FACE; etype <= CELL; etype = NextElementType(etype) )
					{
						if( two_point && etype == FACE ) continue;
#if defined(USE_OMP)
#pragma omp parallel for
#endif
						for(integer it = 0; it < m.LastLocalID(etype); ++it) if( m.isValidElement(etype,it) )
						{
							Element e = m.ElementByLocalID(etype,it);
Kirill Terekhov's avatar
Kirill Terekhov committed
738
							if( e.GetStatus() == Element::Ghost ) continue;
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
							tag_Q[e] -= Update[Q.Index(e)];
							//if( tag_Q[e] < Q_min ) Q_min = tag_Q[e];
							//if( tag_Q[e] > Q_max ) Q_max = tag_Q[e];
						}
					}
					
					//std::cout << "Q: " << Q_min << ":" << Q_max << std::endl;
					/*
					std::cout << "Q_min:" << Q_min << std::endl;
					for(ElementType etype = FACE; etype <= CELL; etype = NextElementType(etype) )
						for(integer it = 0; it < m.LastLocalID(etype); ++it) if( m.isValidElement(etype,it) )
							tag_Q[m.ElementByLocalID(etype,it)] -= Q_min;
					 */
					
					m.ExchangeData(tag_Q, CELL|FACE, 0);
#if defined(USE_OMP)
#pragma omp parallel for
#endif
					for(integer it = 0; it < m.CellLastLocalID(); ++it) if( m.isValidCell(it) )
					{
						Cell c = m.CellByLocalID(it);
						tag_P[c] -= tag_Q[c];
					}
#if defined(USE_OMP)
#pragma omp parallel for
#endif
					for(integer it = 0; it < m.FaceLastLocalID(); ++it) if( m.isValidFace(it) )
					{
						Face f = m.FaceByLocalID(it);
						//update pressure
						tag_P[f] -= tag_Q[f];
						//update for velocity field
						real ngradP = 0;
						if( two_point )
						{
							if( f.FrontCell().isValid() )
							{
								real xf[3],xb[3],n[3],d;
								f.UnitNormal(n);
								f.FrontCell().Centroid(xf);
								f.BackCell().Centroid(xb);
								d = n[0]*(xf[0]-xb[0]) + n[1]*(xf[1]-xb[1]) + n[2]*(xf[2]-xb[2]);
								ngradP = (tag_Q[f.FrontCell()] - tag_Q[f.BackCell()])/d;
							}
						}
						else
						{
							Cell c = f.BackCell();
							ElementArray<Face> cfaces = c.getFaces();
							Matrix<real,real_array> W(tag_W[c],cfaces.size(),cfaces.size());
							for(integer kt = 0; kt < cfaces.size(); ++kt)
								ngradP += W(tag_i[f],kt)*(tag_Q[cfaces[kt]] - tag_Q[c])*cfaces[kt].Area();
						}
						tag_nU[f] += ngradP*dT/alpha;
						
					}
					m.ExchangeData(tag_P, CELL|FACE, 0);
					m.ExchangeData(tag_nU, FACE, 0);
					
					if( report_residual )
					{
						if( m.GetProcessorRank() == 0 )  std::cout << "After projection" << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
801
						CheckResidual(m,tag_P,tag_nU,visc,t+dT,nrm_ornt);
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
					}
					
					if( write_steps )
					{
						std::stringstream str;
						str << "step" << step;
						if( m.GetProcessorsNumber() == 1 )
							str << ".vtk";
						else
							str << ".pvtk";
						m.Save(str.str());
					}
					
				}
				else
				{
					if( m.GetProcessorRank() == 0 )  std::cout << "Unable to solve: " << S.ReturnReason() << std::endl;
					break;
				}
				
				if( m.GetProcessorRank() == 0 )  std::cout << "step " << step << " time " << t+dT << "/" << T << std::endl;
				step++;
				alpha = 3.0/2.0;
				beta = -4.0/2.0;
				gamma = 1.0/2.0;
			} //end time step
			
		}
		
		
Kirill Terekhov's avatar
Kirill Terekhov committed
832
		CheckResidual(m,tag_P,tag_nU,visc,T,nrm_ornt);
833 834 835 836 837 838 839 840 841
		

        if( m.GetProcessorsNumber() == 1 )
            m.Save("out.vtk");
        else
            m.Save("out.pvtk");
		
		
		m.ReleaseMarker(boundary,FACE);
Kirill Terekhov's avatar
Kirill Terekhov committed
842
		m.ReleaseMarker(nrm_ornt,FACE);
843 844 845 846 847 848 849 850 851 852 853 854
    }
    else
    {
        std::cout << argv[0] << " mesh_file [dt=1/50] [T=1/10] [visc=0]" << std::endl;
    }

#if defined(USE_PARTITIONER)
    Partitioner::Finalize(); // Finalize the partitioner activity
#endif
    Solver::Finalize(); // Finalize solver and close MPI activity
    return 0;
}