4 libMesh::PetscMatrix<libMesh::Number>& matrixOutput)
6 if(matrixOutput.initialized())
11 int M = matrixInput.m();
12 int N = matrixInput.n();
16 PetscInt local_M, local_N;
18 MatGetLocalSize(matrixInput.mat(),&local_M,&local_N);
21 matrixOutput.init(M,N,local_M,local_N,1,0);
23 libMesh::PetscVector<libMesh::Number> UnityVec(matrixInput.comm(),M,local_M);
24 libMesh::PetscVector<libMesh::Number> DummyVector(matrixInput.comm(),M,local_M);
26 VecSet(UnityVec.vec(),1);
30 matrixInput.vector_mult(DummyVector,UnityVec);
32 MatDiagonalSet(matrixOutput.mat(),DummyVector.vec(),INSERT_VALUES);
38 libMesh::PetscMatrix<libMesh::Number>& matrixOutput)
40 if(matrixOutput.initialized())
45 int M = matrixInput.m();
46 int N = matrixInput.n();
50 PetscInt local_M, local_N;
52 MatGetLocalSize(matrixInput.mat(),&local_M,&local_N);
55 matrixOutput.init(M,N,local_M,local_N,1,0);
57 libMesh::PetscVector<libMesh::Number> UnityVec(matrixInput.comm(),M,local_M);
58 libMesh::PetscVector<libMesh::Number> DummyVector(matrixInput.comm(),M,local_M);
60 VecSet(UnityVec.vec(),1);
64 matrixInput.vector_mult(DummyVector,UnityVec);
66 DummyVector.reciprocal();
68 MatDiagonalSet(matrixOutput.mat(),DummyVector.vec(),INSERT_VALUES);
74 libMesh::PetscVector<libMesh::Number>& vecOutput)
76 if(vecOutput.initialized())
81 int M = matrixInput.m();
82 int N = matrixInput.n();
86 PetscInt local_M, local_N;
88 MatGetLocalSize(matrixInput.mat(),&local_M,&local_N);
91 vecOutput.init(M,local_M,
false);
93 libMesh::PetscVector<libMesh::Number> UnityVec(matrixInput.comm(),M,local_M);
95 VecSet(UnityVec.vec(),1);
99 matrixInput.vector_mult(vecOutput,UnityVec);
101 vecOutput.reciprocal();
106 libMesh::Real accumulator = 0;
107 const libMesh::Parallel::Communicator& MatrixComm = CouplingTestMatrix.comm();
109 std::cout <<
"| M_i,j : " << CouplingTestMatrix.m() <<
" x " << CouplingTestMatrix.n() << std::endl;
111 int nodes = MatrixComm.size();
113 PetscInt local_M, local_N;
114 MatGetLocalSize(CouplingTestMatrix.mat(),&local_M,&local_N);
116 bool bPrintOnTerminal = CouplingTestMatrix.m() < 101 && CouplingTestMatrix.n() < 101 && nodes > 1;
119 for(
unsigned int iii = 0; iii < CouplingTestMatrix.m(); ++iii)
121 for(
unsigned int jjj = 0; jjj < CouplingTestMatrix.n(); ++jjj)
123 std::cout <<
" " << CouplingTestMatrix(iii,jjj);
125 std::cout << std::endl;
129 libMesh::PetscVector<libMesh::Number> dummy_vec(MatrixComm,CouplingTestMatrix.n(),local_N);
130 MatGetRowSum(CouplingTestMatrix.mat(),dummy_vec.vec());
132 VecSum(dummy_vec.vec(),&accumulator);
133 std::cout <<
"|" << std::endl;
134 std::cout <<
"| Sum( M_i,j ) = " << accumulator << std::endl << std::endl;
139 libMesh::PetscVector<libMesh::Number> col_sum(CouplingTestMatrix.comm(),CouplingTestMatrix.m());
140 libMesh::PetscVector<libMesh::Number> row_sum(CouplingTestMatrix.comm(),CouplingTestMatrix.n());
142 for(
unsigned int iii = 0; iii < CouplingTestMatrix.m(); ++iii)
144 for(
unsigned int jjj = 0; jjj < CouplingTestMatrix.n(); ++jjj)
146 col_sum.add(iii,CouplingTestMatrix(iii,jjj));
147 row_sum.add(jjj,CouplingTestMatrix(iii,jjj));
152 #ifdef PRINT_MATLAB_DEBUG
153 col_sum.print_matlab(name_base +
"_col.m");
154 row_sum.print_matlab(name_base +
"_row.m");
160 std::cout <<
"| M_i,j : " << CouplingTestMatrix.m() <<
" x " << CouplingTestMatrix.n() << std::endl;
162 CouplingTestMatrix.print_matlab(name_base);
167 std::cout <<
"| M_i,j : " << CouplingTestMatrix.m() <<
" x " << CouplingTestMatrix.n() << std::endl;
171 MatGetInfo(CouplingTestMatrix.mat(),MAT_LOCAL,&temp_info);
172 std::cout <<
"| LOCAL : memory = " << temp_info.memory << std::endl;
173 std::cout <<
"| non-zeros used = " << (100.*temp_info.nz_used)/temp_info.nz_allocated <<
" % " << std::endl;
189 MatGetInfo(CouplingTestMatrix.mat(),MAT_GLOBAL_SUM,&temp_info);
190 std::cout <<
"| GLOBAL : memory = " << temp_info.memory << std::endl;
191 std::cout <<
"| non-zeros used = " << (100.*temp_info.nz_used)/temp_info.nz_allocated <<
" % " << std::endl;
192 std::cout <<
"| total nb. of non-zeros used = " << temp_info.nz_used << std::endl;
193 std::cout <<
"| total nb. of non-zeros allocated = " << temp_info.nz_allocated << std::endl;
194 std::cout <<
"| sparsity = " << (100.*temp_info.nz_used)/(CouplingTestMatrix.m() * CouplingTestMatrix.n()) <<
" % " << std::endl;
196 MatGetInfo(CouplingTestMatrix.mat(),MAT_GLOBAL_MAX,&temp_info);
197 std::cout <<
"| MAX : memory = " << temp_info.memory << std::endl;
198 std::cout <<
"| non-zeros used = " << (100.*temp_info.nz_used)/temp_info.nz_allocated <<
" % " << std::endl << std::endl;
204 const libMesh::Parallel::Communicator& WorldComm = InputMatrix.comm();
206 unsigned int rank = WorldComm.rank();
207 unsigned int nodes = WorldComm.size();
211 MatGetInfo(InputMatrix.mat(),MAT_LOCAL,&local_info);
212 MatGetInfo(InputMatrix.mat(),MAT_LOCAL,&global_info);
215 int l_nz_used = local_info.nz_used;
216 int l_nz_allocated = local_info.nz_allocated;
217 int l_memory = local_info.memory;
222 double accumulator_n = 0;
223 double accumulator_m = 0;
225 MatGetLocalSize(InputMatrix.mat(),&l_n,&l_m);
227 std::vector<int> full_nz_used;
228 std::vector<int> full_nz_allocated;
229 std::vector<int> full_memory;
231 std::vector<int> full_n;
232 std::vector<int> full_m;
236 full_nz_used.resize(nodes,0);
237 full_nz_allocated.resize(nodes,0);
238 full_memory.resize(nodes,0);
239 full_n.resize(nodes,0);
240 full_m.resize(nodes,0);
243 WorldComm.gather(0,l_nz_used,full_nz_used);
244 WorldComm.gather(0,l_nz_allocated,full_nz_allocated);
245 WorldComm.gather(0,l_memory,full_memory);
246 WorldComm.gather(0,l_n,full_n);
247 WorldComm.gather(0,l_m,full_m);
251 os <<
"# rank \t local_n \t local_m \t start_n \t start_m \t nz_alloc \t nz_used \t memory " << std::endl;
252 for(
unsigned int iii = 0; iii < nodes; ++iii)
255 << full_n[iii] <<
" \t "
256 << full_m[iii] <<
" \t "
257 << accumulator_n <<
" \t "
258 << accumulator_m <<
" \t "
259 << full_nz_allocated[iii] <<
" \t "
260 << full_nz_used[iii] <<
" \t "
261 << full_memory[iii] << std::endl;
262 accumulator_n += full_n[iii];
263 accumulator_m += full_m[iii];
271 libMesh::PetscVector<libMesh::Number>& b,
272 libMesh::PetscVector<libMesh::Number>& x,
285 libMesh::Mesh& IntersectionMesh,
286 libMesh::Real CouplingScale,
287 const std::string matrixType,
290 std::cout <<
"| " << matrixType << std::endl;
291 libMesh::Real accumulator = 0;
293 std::cout <<
"| M_i,j : " << CouplingTestMatrix.m() <<
" x " << CouplingTestMatrix.n() << std::endl;
294 for(
unsigned int iii = 0; iii < CouplingTestMatrix.m(); ++iii)
296 for(
unsigned int jjj = 0; jjj < CouplingTestMatrix.n(); ++jjj)
298 accumulator += CouplingTestMatrix(iii,jjj);
302 libMesh::Real vol = 0;
303 libMesh::Elem* silly_elem;
304 for(libMesh::MeshBase::element_iterator itBegin = IntersectionMesh.elements_begin();
305 itBegin != IntersectionMesh.elements_end();
308 silly_elem = *itBegin;
309 vol += silly_elem->volume();
312 libMesh::Real difference = accumulator - n_var*CouplingScale*vol;
314 std::cout <<
"|" << std::endl;
315 std::cout <<
"| Sum( M_i,j ) = " << accumulator << std::endl;
316 std::cout <<
"| n * C * Volume = " << n_var*CouplingScale*vol << std::endl;
317 std::cout <<
"| > Difference = " << difference << std::endl << std::endl;
321 const std::string& filename,
int rank, MPI_Comm comm,
int dim)
324 PetscViewerBinaryOpen(comm,filename.c_str(),FILE_MODE_WRITE,&viewer);
325 VecView(input_vec,viewer);
327 PetscViewerDestroy(&viewer);
332 std::ofstream vec_info(filename +
".info");
333 vec_info <<
"-vecload_block_size " << std::to_string(dim) << std::endl;
339 const std::string& filename,
int dim )
341 write_PETSC_vector(input_vec.vec(),filename,input_vec.comm().rank(),input_vec.comm().get(),dim);
345 const std::string& filename, MPI_Comm comm)
348 PetscViewerCreate(comm,&viewer);
349 PetscViewerASCIIOpen(comm,filename.c_str(),&viewer);
350 PetscViewerPushFormat (viewer,PETSC_VIEWER_ASCII_MATLAB);
351 VecView(input_vec,viewer);
353 PetscViewerDestroy(&viewer);
359 PetscViewerCreate(comm,&viewer);
360 PetscViewerASCIIOpen(comm,filename.c_str(),&viewer);
361 PetscViewerPushFormat (viewer,PETSC_VIEWER_ASCII_MATLAB);
362 MatView(input_mat,viewer);
364 PetscViewerDestroy(&viewer);
368 const std::string& filename, MPI_Comm comm)
371 PetscViewerBinaryOpen(comm,filename.c_str(),FILE_MODE_READ,&viewer);
372 VecLoad(input_vec,viewer);
374 PetscViewerDestroy(&viewer);
378 const std::string& filename)
384 const std::string& filename_base,
int nb_of_vecs,
int dimension )
390 MatGetLocalSize(mat_sys.mat(),NULL,&local_N);
392 std::string vector_path;
395 for(
int iii = 0; iii < nb_of_vecs; ++iii)
397 VecCreate(mat_sys.comm().get(),&rb_vecs[iii]);
398 VecSetSizes(rb_vecs[iii],local_N,mat_sys.n());
400 vector_path = filename_base +
"_" + std::to_string(iii) +
"_n_" + std::to_string(nb_of_vecs) +
".petscvec";
404 MatNullSpace nullsp_sys;
405 MatNullSpaceCreate(mat_sys.comm().get(), PETSC_FALSE, nb_of_vecs, rb_vecs, &nullsp_sys);
406 MatSetNullSpace(mat_sys.mat(),nullsp_sys);
408 MatNullSpaceDestroy(&nullsp_sys);
409 for(
int iii = 0; iii < nb_of_vecs; ++iii)
411 VecDestroy(&rb_vecs[iii]);
416 const std::string& filename,
int rank, MPI_Comm comm,
int dim)
419 PetscViewerBinaryOpen(comm,filename.c_str(),FILE_MODE_WRITE,&viewer);
420 MatView(input_mat,viewer);
422 PetscViewerDestroy(&viewer);
427 std::ofstream mat_info(filename +
".info");
428 mat_info <<
"-matload_block_size " << std::to_string(dim) << std::endl;
434 const std::string& filename,
int dim)
436 write_PETSC_matrix(input_mat.mat(),filename,input_mat.comm().rank(),input_mat.comm().get(),dim);
440 const std::string& filename, MPI_Comm comm)
444 PetscViewerBinaryOpen(comm,filename.c_str(),FILE_MODE_READ,&viewer);
445 MatLoad(input_mat,viewer);
447 PetscViewerDestroy(&viewer);
451 const std::string& filename)
460 VecGetLocalSize(vecs_in[0],&M_local);
461 VecGetSize(vecs_in[0],&M);
463 MatCreateDense(PETSC_COMM_WORLD,M_local,PETSC_DECIDE,M,nb_vecs,NULL,&matrix_out);
466 PetscInt own_low, own_high;
467 VecGetOwnershipRange(vecs_in[0],&own_low,&own_high);
469 std::vector<PetscInt> row(M_local,0);
471 for(
int iii = 0; iii < M_local; ++iii)
473 row[iii] = own_low + iii;
478 for(
int iii = 0; iii < nb_vecs; ++iii)
480 VecGetArray(vecs_in[iii],&data);
481 MatSetValues(matrix_out,M_local,row.data(),1,&iii,data,INSERT_VALUES);
482 VecRestoreArray(vecs_in[iii],&data);
486 MatAssemblyBegin(matrix_out,MAT_FINAL_ASSEMBLY);
487 MatAssemblyEnd(matrix_out,MAT_FINAL_ASSEMBLY);
497 MatFactorInfo factor_info;
501 MatDuplicate(matrix_in,MAT_DO_NOT_COPY_VALUES,&matrix_out);
502 MatDuplicate(matrix_in,MAT_DO_NOT_COPY_VALUES,&Id_mat);
505 MatZeroEntries(Id_mat);
509 MatGetOrdering(matrix_in,MATORDERINGNATURAL, &rperm, &cperm);
510 MatFactorInfoInitialize(&factor_info);
511 MatLUFactor(matrix_in,rperm,cperm,&factor_info);
514 MatMatSolve(matrix_in,Id_mat,matrix_out);
517 MatSetUnfactored(matrix_in);
529 MatType mat_type_query;
530 VecType vec_in_type_query;
531 VecType vec_out_type_query;
533 MatGetType(mat_seq,&mat_type_query);
534 VecGetType(vec_seq_in,&vec_in_type_query);
535 VecGetType(vec_seq_out,&vec_out_type_query);
537 homemade_assert_msg(std::strcmp(mat_type_query,MATSEQDENSE) == 0 ,
"Matrix is not dense and sequential");
538 homemade_assert_msg(std::strcmp(vec_in_type_query,VECSEQ) == 0 ,
"Input vector is not sequential");
539 homemade_assert_msg(std::strcmp(vec_out_type_query,VECSEQ) == 0 ,
"Output vector is not sequential");
542 PetscInt vec_dim = 0;
543 VecGetSize(vec_seq_in,&vec_dim);
547 MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
550 PetscScalar * dummy_seq_array;
555 MatMult(mat_seq,vec_seq_in,vec_seq_out);
556 VecScale(vec_seq_out,a_const);
560 VecGetArray(vec_seq_out,&dummy_seq_array);
561 MPI_Bcast(dummy_seq_array, vec_dim, MPIU_SCALAR, 0, PETSC_COMM_WORLD);
562 MPI_Barrier(PETSC_COMM_WORLD);
563 VecRestoreArray(vec_seq_out,&dummy_seq_array);
void check_coupling_matrix(libMesh::PetscMatrix< libMesh::Number > &CouplingTestMatrix, libMesh::Mesh &IntersectionMesh, libMesh::Real CouplingScale, const std::string matrixtype, int n_var=3)
void lump_matrix_and_invert(libMesh::PetscMatrix< libMesh::Number > &matrixInput, libMesh::PetscMatrix< libMesh::Number > &matrixOutput)
void print_matrix_col_line_sum(libMesh::PetscMatrix< libMesh::Number > &CouplingTestMatrix, const std::string name_base)
void write_PETSC_vector(libMesh::PetscVector< libMesh::Number > &input_vec, const std::string &filename, int dim=1)
void create_PETSC_dense_matrix_from_vectors(const Vec *vecs_in, int nb_vecs, Mat &matrix_out)
void solve_linear_PETSC(libMesh::PetscMatrix< libMesh::Number > &A, libMesh::PetscVector< libMesh::Number > &b, libMesh::PetscVector< libMesh::Number > &x, KSP &ksp, PC &pc)
void attach_rigid_body_mode_vectors(libMesh::PetscMatrix< libMesh::Number > &mat_sys, const std::string &filename_base, int nb_of_vecs, int dimension)
void lump_matrix(libMesh::PetscMatrix< libMesh::Number > &matrixInput, libMesh::PetscMatrix< libMesh::Number > &matrixOutput)
void PETSC_MatMultScale_Bcast(Mat mat_seq, Vec vec_seq_in, Vec vec_seq_out, double a_const)
void PETSC_invert_dense_matrix(Mat &matrix_in, Mat &matrix_out)
void read_PETSC_matrix(Mat input_mat, const std::string &filename, MPI_Comm comm=PETSC_COMM_WORLD)
void print_matrix_info(libMesh::PetscMatrix< libMesh::Number > &InputMatrix, std::ostream &os=libMesh::out)
void print_matrix_matlab(libMesh::PetscMatrix< libMesh::Number > &CouplingTestMatrix, const std::string name_base)
void write_PETSC_matrix_MATLAB(Mat input_mat, const std::string &filename, MPI_Comm comm=PETSC_COMM_WORLD)
void print_matrix_dim(libMesh::PetscMatrix< libMesh::Number > &CouplingTestMatrix, bool bDetailed=false)
void print_matrix(libMesh::PetscMatrix< libMesh::Number > &CouplingTestMatrix)
void read_PETSC_vector(libMesh::PetscVector< libMesh::Number > &input_vec, const std::string &filename)
void write_PETSC_vector_MATLAB(Vec input_vec, const std::string &filename, MPI_Comm comm=PETSC_COMM_WORLD)
void write_PETSC_matrix(Mat input_mat, const std::string &filename, int rank, MPI_Comm comm=PETSC_COMM_WORLD, int dim=1)