From a8af3fc3e6c2a8fe0c5fd0740f50a73c6af48a5f Mon Sep 17 00:00:00 2001 From: "J.A. de Jong @ vulgaris" Date: Sun, 13 Nov 2016 20:45:26 +0100 Subject: [PATCH] Updated triplets and tripletlist --- src/sys/triplets.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++ src/sys/triplets.h | 52 ++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 src/sys/triplets.cpp create mode 100644 src/sys/triplets.h diff --git a/src/sys/triplets.cpp b/src/sys/triplets.cpp new file mode 100644 index 0000000..895b9c1 --- /dev/null +++ b/src/sys/triplets.cpp @@ -0,0 +1,88 @@ +#include "triplets.h" +#include "tasmet_tracer.h" +#include "tasmet_io.h" +#include "tasmet_exception.h" + +TripletList::operator sdmat() const { + + TRACE(15,"TripletList::operator sdmat()"); + + vector validtrlist; + + validtrlist.reserve(triplets.size()); + + for(auto& tr :triplets) { + if(tr.valid) { + validtrlist.push_back(tr); + } + } + + us nvals=validtrlist.size(); + + arma::umat locations(2,nvals); + + vd values(nvals); + + for(us i=0;i= _ndofs || t.col >=_ndofs){ + throw TaSMETError("Invalid position for triplet"); + } + triplets.push_back(t); + +} +void TripletList::zeroOutRow(us rownr){ + TRACE(15,"zeroOutRow()"); + + for(Triplet& tr: triplets){ + if(tr.row==rownr){ + tr.valid = false; + } + } +} + + +void TripletList::show() const { + for(const auto& t: triplets){ + cout << "Row: " << t.row << " , column: " << t.col << " , value: " << t.value << "\n"; + } +} +void TripletList::multiplyTriplets(const d& factor){ + TRACE(15,"multiplyTriplets()"); + for(auto tr: triplets){ + tr.value*= factor; + } +} + +void TripletList::reserveExtraDofs(us n){ + TRACE(15,"reserveExtraDofs()"); + us cursize=triplets.size(); + triplets.reserve(cursize+n); +} + +void TripletList::shiftTriplets(int nrows,int ncols){ + // shift the position of the values in a matrix. nrows and ncols + // can be negative numbers. + TRACE(15,"shiftTriplets()"); + TRACE(100,"EXTRA CHECKS HERE!"); + for(auto tr: triplets){ + tr.col+=ncols; + tr.row+=nrows; + } +} + + + diff --git a/src/sys/triplets.h b/src/sys/triplets.h new file mode 100644 index 0000000..c9b9654 --- /dev/null +++ b/src/sys/triplets.h @@ -0,0 +1,52 @@ +#pragma once +#ifndef _TRIPLETS_H_ +#define _TRIPLETS_H_ + +#include "tasmet_types.h" + +struct Triplet{ + us row,col; + d value; + bool valid = true; + + Triplet(us row,us col,d value):row(row),col(col),value(value){} +}; + + +class TripletList { + + // Size of the matrix to build eventually + us _ndofs; + + vector triplets; + +public: + TripletList(us ndofs); + ~TripletList(){} + + // Convert to Armadillo Sparse matrix + operator sdmat() const; + + void setNdofs(us ndofs){_ndofs=ndofs;} + + void show() const; + + void addTriplet(const Triplet&); + + // Make one row zero + void zeroOutRow(us rownr); + + void multiplyTriplets(const d& multiplicationfactor); + + // Add to capacity + void reserveExtraDofs(us n); + + // Shift position of triplets a certain number of rows and cols. + void shiftTriplets(int nrows,int ncols); + +}; + + + +#endif /* _TRIPLETS_H_ */ +