37 if (B.
Rows()!=this->Rows())
return;
38 int64_t rows = this->Rows();
44 aux+=GetVal(i,k)*B.
GetVal(i,k);
65 fPardisoControl.SetMatrix(
this);
74 fPardisoControl.SetMatrix(
this);
87 fPardisoControl = cp.fPardisoControl;
88 fPardisoControl.SetMatrix(
this);
97 int64_t nrows = cp.
Rows();
99 int64_t count = 0, c = 0, r = 0;
107 typename map< pair<int64_t,int64_t>, TVar>::const_iterator it;
112 int64_t row = it->first.first;
124 int64_t col = it->first.second;
126 TVar
val = it->second;
143 for(k=
fIA[row];k<
fIA[row+1];k++){
152 cout <<
"TPZFYsmpMatrix::PutVal: Non existing position on sparse matrix: line = " << row <<
" column " << col << endl;
166 for(i=0;i<elmat.
Rows();i++){
167 for(j=0;j<elmat.
Rows();j++){
168 ipos=destinationindex[i];
169 jpos=destinationindex[j];
176 if(k >=
fIA[ipos] && k <
fIA[ipos+1] &&
fJA[k]==jpos)
182 for(k=
fIA[ipos];k<
fIA[ipos+1];k++){
183 if(
fJA[k]==jpos ||
fJA[k]==-1){
199 if(!flag) cout <<
"TPZFYsmpMatrix::AddKel: Non existing position on sparse matrix: line =" << ipos <<
" column =" << jpos << endl; }
211 ipos=destinationindex[i];
212 jpos=destinationindex[j];
213 value=elmat.
GetVal(sourceindex[i],sourceindex[j]);
219 if(k >=
fIA[ipos] && k <
fIA[ipos+1] &&
fJA[k]==jpos)
225 for(k=
fIA[ipos];k<
fIA[ipos+1];k++){
226 if(
fJA[k]==jpos ||
fJA[k]==-1){
242 if(!flag) cout <<
"TPZFYsmpMatrix::AddKel: Non existing position on sparse matrix: line =" << ipos <<
" column =" << jpos << endl; }
253 int64_t nel = destinationindex.
NElements();
254 std::multimap<int64_t,int64_t> mapindex;
255 std::multimap<int64_t,int64_t>::iterator hint = mapindex.begin();
257 ilocal = destinationindex[i];
258 hint = mapindex.insert(hint,std::make_pair(ilocal,i));
262 ilocal = destinationindex[i];
263 int64_t jfirst =
fIA[ilocal];
264 int64_t jlast =
fIA[ilocal+1];
265 int64_t *Aptr = &
fJA[jfirst];
266 int64_t *AptrLast = &
fJA[jlast];
268 std::multimap<int64_t,int64_t>::iterator itelmat = mapindex.
begin();
269 while(j<nel && Aptr != AptrLast)
271 if(*Aptr == (*itelmat).first)
273 int64_t jel = (*itelmat).second;
274 fA[jfirst] += elmat(i,jel);
276 if(itelmat != mapindex.end() && (*itelmat).second != jel)
283 else if(*Aptr < (*itelmat).first)
288 else if(*Aptr > (*itelmat).second)
290 std::cout << __PRETTY_FUNCTION__ <<
" inconsistent\n";
291 int64_t *iptr = &
fJA[jfirst];
292 while(iptr < AptrLast)
294 cout << *iptr <<
" ";
298 std::multimap<int64_t,int64_t>::iterator itelmat2 = mapindex.
begin();
299 for(;itelmat2 != mapindex.end(); itelmat2++)
301 cout << (*itelmat2).first <<
"/" << (*itelmat2).second <<
" ";
309 std::cout << __PRETTY_FUNCTION__ <<
" inconsistent2 j = " << j <<
" nel " << nel <<
"\n";
310 int64_t *iptr = &
fJA[jfirst];
311 while(iptr < AptrLast)
313 cout << *iptr <<
" ";
317 std::multimap<int64_t,int64_t>::iterator itelmat2 = mapindex.
begin();
318 for(;itelmat2 != mapindex.end(); itelmat2++)
320 cout << (*itelmat2).first <<
"/" << (*itelmat2).second <<
" ";
341 fPardisoControl.SetMatrix(
this);
344 cerr <<
"TPZFYsmpMatrix(int rows,int cols)\n";
352 cerr <<
"~TPZFYsmpMatrix()\n";
372 int64_t loccol = col;
373 for(int64_t ic=
fIA[row] ; ic <
fIA[row+1]; ic++ ) {
374 if (
fJA[ic] == loccol &&
fJA[ic] != -1 )
return fA[ic];
390 const TVar alpha,
const TVar beta,
const int opt ) {
395 cout <<
"\nERROR! in TPZVerySparseMatrix::MultiplyAdd : incompatible dimensions in x, y or z\n";
399 int64_t ir, ic, icol, xcols;
402 int64_t r = (opt) ? this->
Cols() : this->
Rows();
405 for(ic=0; ic<xcols; ic++) {
406 TVar *zp = &(z(0,ic));
408 const TVar *yp = &(y.
g(0,0));
412 memcpy(zp,yp,r*
sizeof(TVar));
415 TVar *zp = &(z(0,0)), *zlast = zp+r;
423 if(xcols == 1 && opt == 0)
427 cout <<
"\nERROR! in TPZFYsmpMatrix::MultiplyAddMT: incompatible dimensions in opt=false\n";
430 for(ir=0; ir<r; ir++) {
431 int64_t icolmin =
fIA[ir];
432 int64_t icolmax =
fIA[ir+1];
433 const TVar *xptr = &(x.
g(0,0));
435 int64_t *JAptr = &
fJA[0];
436 for(sum = 0.0, icol=icolmin; icol<icolmax; icol++ ) {
437 sum += Aptr[icol] * xptr[JAptr[icol]];
439 z(ir,0) += alpha * sum;
444 for(ic=0; ic<xcols; ic++) {
447 for(ir=0; ir<this->
Rows(); ir++) {
448 for(sum = 0.0, icol=
fIA[ir]; icol<
fIA[ir+1]; icol++ ) {
449 sum +=
fA[icol] * x.
g((
fJA[icol]),ic);
451 z(ir,ic) += alpha * sum;
460 cout <<
"\nERROR! in TPZFYsmpMatrix::MultiplyAddMT: incompatible dimensions in opt=true\n";
465 for(ir=0; ir<this->
Rows(); ir++) {
466 for(icol=
fIA[ir]; icol<
fIA[ir+1]; icol++ ) {
467 if(
fJA[icol]==-1)
break;
469 TVar aval =
fA[icol];
471 z(jc,ic) += alpha * aval * x.
g(ir,ic);
488 const TVar alpha,
const TVar beta,
const int opt)
const {
493 if ((!opt && this->
Cols() != x.
Rows()) || (opt && this->
Rows() != x.
Rows())) {
494 std::cout <<
"TPZFMatrix::MultAdd matrix x with incompatible dimensions>" ;
497 if(beta != (
double)0. && ((!opt && this->
Rows() != y.
Rows()) || (opt && this->
Cols() != y.
Rows()) || y.
Cols() != x.
Cols())) {
498 std::cout <<
"TPZFMatrix::MultAdd matrix y with incompatible dimensions>";
511 if(this->
Cols() == 0) {
518 int64_t r = (opt) ? this->
Cols() : this->
Rows();
521 for(ic=0; ic<xcols; ic++) {
522 TVar *zp = &(z(0,ic));
524 const TVar *yp = &(y.
g(0,0));
534 memcpy(zp,yp,r*
sizeof(REAL));
537 TVar *zp = &(z(0,ic)), *zlast = zp+r;
554 if(opt) numthreads = 1;
563 alldata[i].target =
this;
564 alldata[i].fFirsteq = firsteq;
565 alldata[i].fLasteq = firsteq+eqperthread;
566 firsteq += eqperthread;
567 if(i==numthreads-1) alldata[i].fLasteq = this->
Rows();
570 alldata[i].fAlpha = alpha;
571 alldata[i].fOpt = opt;
591 out <<
"\nTFYsmpMatrix Print: " << title <<
'\n' 592 <<
"\tRows = " << this->
Rows() <<
'\n' 593 <<
"\tColumns = " << this->
Cols() <<
'\n';
595 out <<
"\tIA\tJA\tA\n" 597 for(i=0; i<this->
Rows(); i++) {
621 int64_t rows = this->
Rows();
623 for(int64_t ir=0; ir<rows; ir++) {
631 const REAL overrelax, REAL &
tol,
632 const int FromCurrent,
const int direction ) {
635 int64_t irStart = 0,irLast = this->
Rows(),irInc = 1;
637 irStart = this->
Rows()-1;
641 if(!FromCurrent) x.
Zero();
644 for(iteration=0; iteration<numiterations && eqres >=
tol; iteration++) {
647 while(ir != irLast) {
648 TVar xnewval=rhs.
g(ir,0);
649 for(int64_t ic=
fIA[ir]; ic<
fIA[ir+1]; ic++) {
650 xnewval -=
fA[ic] * x(
fJA[ic],0);
652 eqres += xnewval*xnewval;
653 x(ir,0) += overrelax*(xnewval/
fDiag[ir]);
659 numiterations = iteration;
660 if(residual) this->
Residual(x,rhs,*residual);
686 cout <<
"TPZSYsmpMatrix::Jacobi cannot be called without diagonal\n";
694 int64_t c = F.
Cols();
695 int64_t r = this->
Rows();
699 for(int64_t ic=0; ic<c; ic++) {
700 for(int64_t i=0; i<r; i++) {
701 result(i,ic) += scratch(i,ic)/(
fDiag)[i];
706 for(int64_t ic=0; ic<c; ic++) {
707 for(int64_t i=0; i<r; i++) {
716 for(int64_t it=1; it<numiterations && res >
tol; it++) {
717 for(int64_t ic=0; ic<c; ic++) {
718 for(int64_t i=0; i<r; i++) {
719 result(i,ic) += (scratch)(i,ic)/(
fDiag)[i];
726 if(residual) *residual = scratch;
736 int64_t xcols = data->
fX->Cols();
739 if(xcols == 1 && data->
fOpt == 0)
741 for(ir=data->
fFirsteq; ir<data->fLasteq; ir++) {
742 int64_t icolmin = mat->
fIA[ir];
743 int64_t icolmax = mat->
fIA[ir+1];
744 const TVar *xptr = &(data->
fX->g(0,0));
745 TVar *Aptr = &mat->
fA[0];
746 int64_t *JAptr = &mat->
fJA[0];
747 for(sum = 0.0, icol=icolmin; icol<icolmax; icol++ ) {
748 sum += Aptr[icol] * xptr[JAptr[icol]];
750 data->
fZ->operator()(ir,0) += data->
fAlpha * sum;
755 for(ic=0; ic<xcols; ic++) {
756 if(data->
fOpt == 0) {
758 for(ir=data->
fFirsteq; ir<data->fLasteq; ir++) {
759 for(sum = 0.0, icol=mat->
fIA[ir]; icol<mat->
fIA[ir+1]; icol++ ) {
760 sum += mat->
fA[icol] * data->
fX->g((mat->
fJA[icol]),ic);
762 data->
fZ->operator()(ir,ic) += data->
fAlpha * sum;
771 for(ir=data->
fFirsteq; ir<data->fLasteq; ir++)
773 for(icol=mat->
fIA[ir]; icol<mat->
fIA[ir+1]; icol++ )
775 if(mat->
fJA[icol]==-1)
break;
777 data->
fZ->operator()(jc,ic) += data->
fAlpha * mat->
fA[icol] * data->
fX->g(ir,ic);
786 static int FindCol(int64_t *colf, int64_t *coll, int64_t col)
788 if(col == *colf)
return 0;
789 int64_t *begin = colf;
793 int64_t
dist = (end-begin)/2;
794 int64_t *mid = begin+
dist;
795 if(*mid == col)
return (mid-colf);
796 else if(*mid > col) end=mid;
806 for(ir=0; ir<rowSize; ir++)
808 int64_t row = sRow+ir;
809 int64_t colfirst =
fIA[row];
810 int64_t collast =
fIA[row+1];
811 int64_t iacol =
FindCol(&
fJA[0]+colfirst,&
fJA[0]+collast-1,sCol);
813 for(ic=0; ic<colSize; ic++) A(ir,ic) =
fA[iacol+colfirst];
822 std::map<int64_t,int64_t> indord;
824 for(i=0; i<size; i++)
826 indord[indices[i]] = i;
828 std::map<int64_t,int64_t>::iterator itset,jtset;
829 for(itset = indord.begin(); itset != indord.end(); itset++)
831 int64_t *jfirst = &
fJA[0]+
fIA[(*itset).first];
832 int64_t *jlast = &
fJA[0]+fIA[(*itset).first+1]-1;
834 for(jtset = indord.begin(); jtset != indord.end(); jtset++)
836 int64_t col =
FindCol(jfirst,jlast,(*jtset).first);
837 int64_t
dist = jfirst+col-&
fJA[0];
838 block((*itset).second,(*jtset).second) =
fA[
dist];
850 int64_t *sourcefirst = &
fJA[0]+
fIA[sourcerow];
851 int64_t *sourcelast = &
fJA[0]+fIA[sourcerow+1]-1;
852 int64_t sourcecol =
FindCol(sourcefirst,sourcelast,sourcerow);
855 cout << __PRETTY_FUNCTION__ <<
" at line " << __LINE__ <<
" source not found\n";
858 int64_t sourcedist = sourcefirst+sourcecol-&
fJA[0];
859 int64_t *destfirst = &fJA[0]+fIA[destrow];
860 int64_t *destlast = &fJA[0]+fIA[destrow+1]-1;
861 int64_t destcol =
FindCol(destfirst,destlast,destrow);
862 int64_t destdist = destfirst+destcol-&fJA[0];
865 cout << __PRETTY_FUNCTION__ <<
" at line " << __LINE__ <<
" destrow not found\n";
868 if(
fA[sourcedist] < 1.e-15)
870 cout << __PRETTY_FUNCTION__ <<
" at line " << __LINE__ <<
" small pivot " <<
fA[sourcedist] <<
"\n";
873 TVar mult =
fA[destdist]/
fA[sourcedist];
874 if(mult == 0.)
return;
877 while(destdist < fIA[destrow+1] && sourcedist < fIA[sourcerow+1])
879 if(fJA[destdist] == fJA[sourcedist])
881 fA[destdist] -=
fA[sourcedist]*mult;
885 else if(fJA[destdist] < fJA[sourcedist])
901 if (symmetric && nrow != ncol) {
912 for (int64_t row=0; row<nrow; row++) {
913 if(nrow == ncol) eqs[row].insert(row);
914 for (int64_t col = 0; col<ncol; col++) {
915 REAL
test = rand()*1./RAND_MAX;
917 eqs[row].insert(col);
919 eqs[col].insert(row);
925 for (int64_t row=0; row< nrow; row++) {
926 for (std::set<int64_t>::iterator col = eqs[row].begin(); col != eqs[row].
end(); col++) {
928 A.
Push(orig(row,*col));
930 IA[row+1] = JA.
size();
954 fPardisoControl.SetMatrixType(TPZPardisoControl<TVar>::ENonSymmetric,TPZPardisoControl<TVar>::EIndefinite);
955 fPardisoControl.Decompose();
961 int64_t neq = this->
Rows();
962 for(row=1; row<neq; row++)
965 int64_t lastcol =
fIA[row+1];
967 if(
fJA[lastcol-1] < row)
continue;
968 while(
fJA[colind] < row)
984 fPardisoControl.Solve(*B,x);
990 int64_t bcol = B->
Cols();
992 int64_t neq = this->
Rows();
995 for(row=0; row<neq; row++)
997 int64_t firstrow =
fIA[row];
998 int64_t lastrow =
fIA[row+1];
999 if(
fJA[firstrow] > row ||
fJA[lastrow-1] < row)
1001 cout << __PRETTY_FUNCTION__ <<
" " << __LINE__ <<
" inconsistent column information for row " << row << endl;
1004 int64_t rowcounter = firstrow;
1005 while(
fJA[rowcounter] < row)
1007 for(col=0; col<bcol; col++)
1009 (*B)(row,col) -=
fA[rowcounter]*(*B)(
fJA[rowcounter],col);
1012 for(col=0; col<bcol; col++)
1014 (*B)(row,col) /=
fA[rowcounter];
1018 for(row = neq-1; row >= 0; row--)
1020 int64_t firstrow =
fIA[row];
1021 int64_t lastrow =
fIA[row+1];
1022 int64_t col =
FindCol(&
fJA[0]+firstrow,&
fJA[0]+lastrow-1,row);
1025 cout << __PRETTY_FUNCTION__ <<
" " << __LINE__ <<
" inconsistent column information for row " << row << endl;
1028 int64_t coldist = firstrow+col+1;
1029 while(coldist < lastrow)
1031 for(col=0; col<bcol; col++)
1033 (*B)(row,col) -=
fA[coldist]*(*B)(
fJA[coldist],col);
1041 template<
class TVar>
void AutoFill(int64_t nrow, int64_t ncol, int symmetric)
Fill matrix storage with randomic values.
void MultiplyDummy(TPZFYsmpMatrix< TVar > &B, TPZFYsmpMatrix< TVar > &Res)
virtual int Substitution(TPZFMatrix< TVar > *B) const override
Computes Forward and Backward substitution for a "LU" decomposed matrix.
An auxiliary structure to hold the data of the subset of equations used to multiply in a multi-thre...
virtual void Residual(const TPZFMatrix< TVar > &x, const TPZFMatrix< TVar > &rhs, TPZFMatrix< TVar > &res)
Computes res = rhs - this * x.
virtual void Print(const char *title, std::ostream &out=std::cout, const MatrixOutputFormat form=EFormatted) const override
Print the matrix along with a identification title.
virtual void AddKel(TPZFMatrix< TVar > &elmat, TPZVec< int64_t > &destinationindex) override
Add a contribution of a stiffness matrix.
virtual void resize(const int64_t newsize)
std::map< std::pair< int64_t, int64_t >, TVar > fExtraSparseData
Save elements different from zero, of Sparse matrix.
virtual void AddKelOld(TPZFMatrix< TVar > &elmat, TPZVec< int > &destinationindex)
Add a contribution of a stiffness matrix putting it on destination indexes position.
Templated vector implementation.
int PutVal(const int64_t row, const int64_t col, const TVar &Value) override
Put values without bounds checking This method is faster than "Put" if DEBUG is defined.
MatrixOutputFormat
Defines output format.
virtual int Resize(const int64_t newRows, const int64_t newCols)
Redimensions a matriz keeping the previous values.
void RowLUUpdate(int64_t sourcerow, int64_t destrow)
void SetIsDecomposed(int val)
Sets current matrix to decomposed state.
virtual ~TPZFYsmpMatrix()
char fDecomposed
Decomposition type used to decompose the current matrix.
Implements a non symmetric sparse matrix (Yale Sparse Matrix Storage). Matrix.
REAL val(STATE &number)
Returns value of the variable.
virtual int Zero() override
Zeroes the matrix.
virtual void SetData(int64_t *IA, int64_t *JA, TVar *A)
Pass the data to the class.
static TVar gZero
Initializing value to static variable.
virtual void MultAdd(const TPZFMatrix< TVar > &x, const TPZFMatrix< TVar > &y, TPZFMatrix< TVar > &z, const TVar alpha=1., const TVar beta=0., const int opt=0) const override
It computes z = beta * y + alpha * opt(this)*x but z and x can not overlap in memory.
#define PZ_PTHREAD_JOIN(thread, val, fn)
int Zero() override
Makes Zero all the elements.
int64_t size() const
Returns the number of elements of the vector.
void InitializeData()
Implements a initialization method for the sparse structure. It sets the initial value for the fIA an...
void Push(const T object)
Pushes a copy of the object on the stack.
virtual int Decompose_LU() override
int IsDecomposed() const
Checks if current matrix is already decomposed.
TVar Norm(const TPZFMatrix< TVar > &A)
Returns the norm of the matrix A.
Contains TPZMatrixclass which implements full matrix (using column major representation).
#define DebugStop()
Returns a message to user put a breakpoint in.
Implements a matrix whose nonzero elements are stored in binary tree. Matrix.
static void * ExecuteMT(void *entrydata)
int64_t Rows() const
Returns number of rows.
Contains the TPZFYsmpMatrix class which implements a non symmetric sparse matrix. ...
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ sqrt
T * begin() const
Casting operator. Returns The fStore pointer.
Full matrix class. Matrix.
int Redim(const int64_t newRows, const int64_t newCols) override
Redimension a matrix and ZERO your elements.
virtual void MultAddMT(const TPZFMatrix< TVar > &x, const TPZFMatrix< TVar > &y, TPZFMatrix< TVar > &z, const TVar alpha=1., const TVar beta=0., const int opt=0)
int32_t Hash(std::string str)
virtual int GetSub(const int64_t sRow, const int64_t sCol, const int64_t rowSize, const int64_t colSize, TPZFMatrix< TVar > &A) const override
Gets submatrix storing it on Target.
REAL dist(TPZVec< T1 > &vec1, TPZVec< T1 > &vec2)
virtual const TVar & GetVal(const int64_t row, const int64_t col) const override
Get the matrix entry at (row,col) without bound checking.
void AutoFill(int64_t nrow, int64_t ncol, int symmetric)
Fill matrix storage with randomic values.
#define PZ_PTHREAD_CREATE(thread, attr, routine, args, fn)
void Fill(const T ©, const int64_t from=0, const int64_t numelem=-1)
Will fill the elements of the vector with a copy object.
Contains TPZVerySparseMatrix class which implements a matrix whose nonzero elements are stored in bin...
int64_t Cols() const
Returns number of cols.
static int FindCol(int64_t *colf, int64_t *coll, int64_t col)
int64_t NElements() const
Returns the number of elements of the vector.
const TPZFMatrix< TVar > * fX
virtual void SolveJacobi(int64_t &numiterations, const TPZFMatrix< TVar > &F, TPZFMatrix< TVar > &result, TPZFMatrix< TVar > *residual, TPZFMatrix< TVar > &scratch, REAL &tol, const int FromCurrent=0) override
Solves the linear system using Jacobi method. .
int ClassId() const override
Define the class id associated with the class.
const TVar & GetVal(const int64_t row, const int64_t col) const override
Get values without bounds checking This method is faster than "Get" if DEBUG is defined.
void SolveSOR(int64_t &numiterations, const TPZFMatrix< TVar > &rhs, TPZFMatrix< TVar > &x, TPZFMatrix< TVar > *residual, TPZFMatrix< TVar > &scratch, const REAL overrelax, REAL &tol, const int FromCurrent=0, const int direction=1) override
Solves the linear system using Successive Over Relaxation method (Gauss Seidel). ...
const TPZFYsmpMatrix< TVar > * target
TPZFYsmpMatrix & operator=(const TPZFYsmpMatrix< TVar > ©)
T * end() const
Returns a pointer to the last+1 element.
TVar & g(const int64_t row, const int64_t col) const
int ClassId() const override
Define the class id associated with the class.
Root matrix class (abstract). Matrix.