コード例 #1
0
int main(int argc, char** argv) {
  cout << "> Creating sm <" << endl;
  SparseMatrix<int> sm;
  cin >> sm;
  cout << sm << endl;
  sm.Output(cout);

  SparseMatrix<int> tsm;
  sm.Transpose(tsm);
  cout << tsm << endl;
  tsm.Output(cout);

  //-------------------------------------------------------
  cout << "> Creating sm2 <" << endl;
  SparseMatrix<int> sm2;
  cin >> sm2;
  cout << sm2 << endl;
  sm2.Output(cout);

  SparseMatrix<int> sm3;
  sm.Add(sm2, sm3);
  cout << sm3 << endl;
  sm3.Output(cout);

  return 0;
}
コード例 #2
0
void StableFluid3D::initPoisson()
{
	clearPoisson();
	int n = resX * resY * resZ;

	for (int i = 0; i < 2; ++i)
	{
		r.push_back(Vector(n));
		z.push_back(Vector(n));
	}

	p.Resize(n);

	SparseMatrix hTranspose;
	SparseMatrix h;

	laplacian = SparseMatrix(n, n);
	hTranspose = SparseMatrix(n, n);
	h = SparseMatrix(n, n);
	preconditioner = SparseMatrix(n, n);

	pressure.Resize(n);
	tempPressure.Resize(n);
	vel.Resize(n);
	ones.Resize(n);

	for (int i = 0; i < n; ++i)
	{
		ones[i] = 1;
	}

	double rdxSqr = (resX * resX) / (width * width);
	double rdySqr = (resY * resY) / (height * height);
	double rdzSqr = (resZ * resZ) / (depth * depth);

	// initialize values for the laplacian matrix
	for (int i = 0; i < resX; ++i)
	{
		for (int j = 0; j < resY; ++j)
		{
			for (int k = 0; k < resZ; ++k)
			{
				int row = makeIndex(i, j, k);

				int x1 = (i == (resX - 1) ? row : row + 1);
				int x2 = (i == 0 ? row : row - 1);

				int y1 = (j == (resY - 1) ? row : row + resX);
				int y2 = (j == 0 ? row : row - resX);

				int z1 = (k == (resZ - 1) ? row : row + resY * resX);
				int z2 = (k == 0 ? row : row - resY * resX);

				laplacian[row][row] += 6 * (rdxSqr + rdySqr + rdzSqr);
				laplacian[row][x1] -= rdxSqr;
				laplacian[row][x2] -= rdxSqr;
				laplacian[row][y1] -= rdySqr;
				laplacian[row][y2] -= rdySqr;
				laplacian[row][z1] -= rdzSqr;
				laplacian[row][z2] -= rdzSqr;
			}
		}
	}

	// perform Cholesky preconditioning
	// decompose laplacian into H * H^T
	SparseVector::iterator iter;

	hTranspose = laplacian.Transpose();
	SparseMatrix& A = hTranspose;
	for (int k = 0; k < n; ++k)
	{
		SparseVector::iterator aik(A[k]);
		aik.seek(k);

		if (aik.getIndex() != k)
		{
			assert(0);
		}

		double divisor = std::sqrt(*aik);

		*aik = divisor;
		aik++;

		while (aik)
		{
			*aik /= divisor;
			aik++;
		}

		SparseVector::iterator ajk(A[k]);
		ajk.seek(k + 1);

		while (ajk)
		{
			int j = ajk.getIndex();
			aik.reset();
			aik.seek(j);

			SparseVector::iterator aij(A[j]);
			aij.seek(j);


			while (aij && aik)
			{
				int i = aij.getIndex();
				if (aik.getIndex() == i)
				{
					*aij -= (*aik) * (*ajk);
					aik++;
				}

				aij++;
				if (aij)
				{
					i = aij.getIndex();

					while (aik && aik.getIndex() < i)
					{
						aik++;
					}
				}
			}
			ajk++;
		}
	}

	h = hTranspose.Transpose();

	//	cerr<<"Sparsity of laplacian is " << laplacian.Sparsity() << endl;

	preconditioner = h.FastMultiplyTranspose();

	int maxIndex = resX * resY * resZ;
	//	for (int i = 0; i < maxIndex; ++i)
	//		preconditioner[i][i] = 1;

	//	cerr<<"Sparsity of preconditioner is " << preconditioner.Sparsity() << endl;
}