Updated triplets and tripletlist

This commit is contained in:
J.A. de Jong @ vulgaris 2016-11-13 20:45:26 +01:00
parent 6f2eb63833
commit a8af3fc3e6
2 changed files with 140 additions and 0 deletions

88
src/sys/triplets.cpp Normal file
View File

@ -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<Triplet> 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<nvals;i++) {
locations(0,i)=validtrlist[i].row;
locations(1,i)=validtrlist[i].col;
values(i)=validtrlist[i].value;
TRACE(0,"row: " << validtrlist[i].row << ", col: "<< validtrlist[i].col << ", value: " << validtrlist[i].value);
}
TRACE(0,"ndofs: "<< _ndofs);
return sdmat(true,locations,values,_ndofs,_ndofs);
}
void TripletList::addTriplet(const Triplet& t){
if(t.row >= _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;
}
}

52
src/sys/triplets.h Normal file
View File

@ -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<Triplet> 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_ */