コード例 #1
0
int main(int argc, char *argv[])
{
	if (argc < 3) {
		std::cout << "Usage: " << argv[0] << " input-matrix output-matrix" << std::endl;
		return 1;
	}

	std::string fname_mat (argv[1]);
	bool verbose (true);

	// *** reading matrix in crs format from file
	std::ifstream in(fname_mat.c_str(), std::ios::in | std::ios::binary);
	double *A(NULL);
	unsigned *iA(NULL), *jA(NULL), n;
	if (in) {
		if (verbose) {
			std::cout << "reading matrix from " << fname_mat << " ... " << std::flush;
		}
		BaseLib::RunTime timer;
		timer.start();
		CS_read(in, n, iA, jA, A);
		in.close();
		timer.stop();
		if (verbose) {
			std::cout << "ok, " << timer.elapsed() << " s" << std::endl;
		}
	} else {
		std::cout << "error reading matrix from " << fname_mat << std::endl;
	}
	unsigned nnz(iA[n]);
	if (verbose) {
		std::cout << "Parameters read: n=" << n << ", nnz=" << nnz << std::endl;
	}

	MathLib::CRSMatrix<double, unsigned> *mat (new MathLib::CRSMatrix<double, unsigned>(n, iA, jA, A));

	const unsigned n_rows_cols_to_erase(300);
	unsigned *rows_cols_to_erase(new unsigned[n_rows_cols_to_erase]);

	for (unsigned k(0); k<n_rows_cols_to_erase; k++) {
		rows_cols_to_erase[k] = (k+1)*2;
	}

	BaseLib::RunTime timer;
	std::cout << "erasing " << n_rows_cols_to_erase << " rows and columns ... " << std::flush;
	timer.start();
	mat->eraseEntries(n_rows_cols_to_erase, rows_cols_to_erase);
	timer.stop();
	std::cout << "ok, " << timer.elapsed() << " s" << std::endl;
	delete[] rows_cols_to_erase;

	fname_mat = argv[2];
	std::ofstream out (fname_mat.c_str(), std::ios::binary);
	CS_write (out, mat->getNRows(), mat->getRowPtrArray(), mat->getColIdxArray(), mat->getEntryArray());
	out.close();

	std::cout << "wrote " << fname_mat << " with " << mat->getNRows() << " rows and " << mat->getRowPtrArray()[mat->getNRows()] << " entries" << std::endl;

	delete mat;
}
コード例 #2
0
ファイル: FEMTests.cpp プロジェクト: hobit03/ogs-1
void setKnownXi_ReduceSizeOfEQS(vector<IndexValue> &list_dirichlet_bc, MathLib::CRSMatrix<double, INDEX_TYPE> &eqsA, double* org_eqsRHS, double* org_eqsX, double** eqsRHS, double** eqsX, map<INDEX_TYPE,INDEX_TYPE> &map_solved_orgEqs)
{
    const size_t n_org_rows = eqsA.getNRows();
    vector<INDEX_TYPE> removed_rows(list_dirichlet_bc.size());
    for (size_t i=0; i<list_dirichlet_bc.size(); i++) {
        IndexValue &bc = list_dirichlet_bc.at(i);
        const size_t id = bc.id;
        const double val = bc.val;
        removed_rows.at(i) = id;

        //b_i -= A(i,k)*val, i!=k
        for (size_t j=0; j<eqsA.getNCols(); j++)
            org_eqsRHS[j] -= eqsA.getValue(j, id)*val;
        //b_k = A_kk*val
        org_eqsRHS[id] = val; //=eqsA(id, id)*val;
        org_eqsX[id] = val; //=eqsA(id, id)*val;
    }

    //remove rows and columns
    eqsA.eraseEntries(removed_rows.size(), &removed_rows[0]);

    //remove X,RHS
    (*eqsX) = new double[n_org_rows-removed_rows.size()];
    (*eqsRHS) = new double[n_org_rows-removed_rows.size()];
    size_t new_id = 0;
    for (size_t i=0; i<n_org_rows; i++) {
        if (find(removed_rows.begin(), removed_rows.end(), i)!=removed_rows.end()) continue;
        (*eqsRHS)[new_id] = org_eqsRHS[i];
        (*eqsX)[new_id] = org_eqsX[i];
        map_solved_orgEqs[new_id] = i;
        new_id++;
    }
}
コード例 #3
0
int main(int argc, char *argv[])
{
	(void) argc;
	(void) argv;

	// *** reading matrix in crs format from file
	std::string fname("/work/Thomas Fischer/data/testmat.bin");
//	std::ifstream in(fname.c_str(), std::ios::binary);
	MathLib::CRSMatrix<double, unsigned> *mat (new MathLib::CRSMatrix<double, unsigned>(fname));
/*	double *A(NULL);
	unsigned *iA(NULL), *jA(NULL), n;
	if (in) {
		std::cout << "reading matrix from " << fname << " ... " << std::flush;
		CS_read(in, n, iA, jA, A);
		in.close();
		std::cout << " ok" << std::endl;
	}
	unsigned nnz(iA[n]);
*/
	unsigned n (mat->getNRows());
	std::cout << "Parameters read: n=" << n << std::endl;

	double *x(new double[n]);
	double *b(new double[n]);

	// *** init start vector x
	for (std::size_t k(0); k<n; k++) {
		x[k] = 1.0;
	}
	// *** read rhs
	fname = "/work/Thomas Fischer/data/rhs.dat";
	std::ifstream in(fname.c_str());
	if (in) {
		read (in, n, b);
		in.close();
	} else {
		std::cout << "problem reading rhs - initializing b with 1.0" << std::endl;
		for (std::size_t k(0); k<n; k++) {
			b[k] = 1.0;
		}
	}

	std::cout << "solving system with CG method ... " << std::flush;
	time_t start_time, end_time;
	time(&start_time);
//	double cg_time (cputime(0.0));
	double eps (1.0e-6);
	unsigned steps (4000);
	CG (mat, b, x, eps, steps);
//	cg_time = cputime(cg_time);
	time(&end_time);
	std::cout << " in " << steps << " iterations (residuum is " << eps << ") took " << /*cg_time <<*/ " sec time and " << (end_time-start_time) << " sec" << std::endl;

	delete mat;
	delete [] x;
	delete [] b;

	return 0;
}
コード例 #4
0
void setKnownXi_ReduceSizeOfEQS(std::vector<IndexValue> &list_dirichlet_bc, MathLib::CRSMatrix<
				double, INDEX_TYPE> &eqsA, double* org_eqsRHS, double* org_eqsX, double** eqsRHS,
				double** eqsX, std::map<INDEX_TYPE, INDEX_TYPE> &map_solved_orgEqs)
{
	const size_t n_org_rows = eqsA.getNRows();
	std::vector<INDEX_TYPE> removed_rows(list_dirichlet_bc.size());
	std::cout << "[BC] (transpose matrix) ... " << std::flush;
	RunTimeTimer run_trans;
	run_trans.start();
	MathLib::CRSMatrix<double, INDEX_TYPE>* transpose_mat (eqsA.getTranspose());
	run_trans.stop();
	std::cout << run_trans.elapsed() << " s" << std::endl;

	INDEX_TYPE const*const row_ptr (transpose_mat->getRowPtrArray());
	INDEX_TYPE const*const col_idx (transpose_mat->getColIdxArray());
	double const*const data (transpose_mat->getEntryArray());

	for (size_t i = 0; i < list_dirichlet_bc.size(); i++) {
		IndexValue &bc = list_dirichlet_bc.at(i);
		const size_t id = bc.id;
		const double val = bc.val;
		removed_rows.at(i) = id;

		//b_i -= A(i,k)*val, i!=k
//		for (size_t j = 0; j < eqsA.getNCols(); j++)
//			org_eqsRHS[j] -= eqsA.getValue(j, id) * val;

		const INDEX_TYPE end(row_ptr[id+1]);
		for (INDEX_TYPE k(row_ptr[id]); k<end; k++) {
			const INDEX_TYPE j(col_idx[k]);
			org_eqsRHS[j] -= data[j] * val;
		}

		//b_k = A_kk*val
		org_eqsRHS[id] = val; //=eqsA(id, id)*val;
		org_eqsX[id] = val; //=eqsA(id, id)*val;
	}
	delete transpose_mat;

	//remove rows and columns
	eqsA.eraseEntries(removed_rows.size(), &removed_rows[0]);

	//remove X,RHS
	(*eqsX) = new double[n_org_rows - removed_rows.size()];
    (*eqsRHS) = new double[n_org_rows-removed_rows.size()];
    size_t new_id = 0;
    for (size_t i=0; i<n_org_rows; i++) {
        if (std::find(removed_rows.begin(), removed_rows.end(), static_cast<unsigned>(i))!=removed_rows.end()) continue;
        (*eqsRHS)[new_id] = org_eqsRHS[i];
        (*eqsX)[new_id] = org_eqsX[i];
        map_solved_orgEqs[new_id] = i;
        new_id++;
    }
}