Exemple #1
0
/* Functions to format the results */
matrix* GetXDisplacements(matrix *Soln) {
    int i;
    matrix* xd;
    xd = CreateMatrix(nRows(Soln), 1);

    for(i=0; i<nRows(xd); i++) {
        setval(xd, val(Soln, i*2, 0), i, 0);
    }

    return xd;
}
Exemple #2
0
// Matrix transpose
Matrix Matrix::transpose() const
{
	Matrix result( nCols(), nRows() );
	
	for ( int i = 0; i < nRows(); i++ ) {
		for ( int j = 0; j < nCols(); j++ ) {
			result( j, i ) = getElement( i, j );
		}
	}
	return( result );
}
Exemple #3
0
matrix* GetYDisplacements(matrix *Soln)
{
    int i;
    matrix* yd;
    yd = CreateMatrix(nRows(Soln), 1);

    for(i=0; i<nRows(yd); i++) {
        setval(yd, val(Soln, i*2+1, 0), i, 0);
    }

    return yd;
}
Exemple #4
0
// vertices 0..3 = 4 corners
//
void Mesh::addColumn()
{
    // Create new vertices 2d (temporary).
    IndexVector2d newVertices2d;
    resizeVertices2d(newVertices2d, nColumns()+1, nRows());

    // Left displacement of points already there.
    qreal leftMoveProp = 1.0f/(nColumns()-1) - 1.0f/nColumns();

    // Add a point at each row.
    int k = nVertices();
    for (int y=0; y<nRows(); y++)
    {
        // Get left and right vertices.
        QPointF left  = getVertex2d( 0,            y );
        QPointF right = getVertex2d( nColumns()-1, y );
        QPointF diff  = right - left;

        // First pass: move middle points.
        for (int x=1; x<nColumns()-1; x++)
        {
            QPointF p = getVertex2d(x, y);
            p -= diff * x * leftMoveProp;
            _rawSetVertex( _vertices2d[x][y], p );
        }

        // Create and add new point.
        QPointF newPoint = right - diff * 1.0f/nColumns();
        _addVertex(newPoint);

        // Assign new vertices 2d.
        for (int x=0; x<nColumns()-1; x++)
            newVertices2d[x][y] = _vertices2d[x][y];

        // The new point.
        newVertices2d[nColumns()-1][y] = k;

        // The rightmost point.
        newVertices2d[nColumns()][y]   = _vertices2d[nColumns()-1][y];

        k++;
    }

    // Copy new mapping.
    _vertices2d = newVertices2d;

    // Increment number of columns.
    _nColumns++;

    // Reorder.
    _reorderVertices();
}
Exemple #5
0
void Mesh::addRow()
{
    // Create new vertices 2d (temporary).
    IndexVector2d newVertices2d;
    resizeVertices2d(newVertices2d, nColumns(), nRows()+1);

    // Top displacement of points already there.
    qreal topMoveProp = 1.0f/(nRows()-1) - 1.0f/nRows();

    // Add a point at each row.
    int k = nVertices();
    for (int x=0; x<nColumns(); x++)
    {
        // Get left and right vertices.
        QPointF top    = getVertex2d(x, 0);
        QPointF bottom = getVertex2d(x, nRows()-1);
        QPointF diff   = bottom - top;

        // First pass: move middle points.
        for (int y=1; y<nRows()-1; y++)
        {
            QPointF p = getVertex2d(x, y);
            p -= diff * y * topMoveProp;
            _rawSetVertex( _vertices2d[x][y], p );
        }

        // Create and add new point.
        QPointF newPoint = bottom - diff * 1.0f/nRows();
        _addVertex(newPoint);

        // Assign new vertices 2d.
        for (int y=0; y<nRows()-1; y++)
            newVertices2d[x][y] = _vertices2d[x][y];

        // The new point.
        newVertices2d[x][nRows()-1] = k;

        // The rightmost point.
        newVertices2d[x][nRows()]   = _vertices2d[x][nRows()-1];

        k++;
    }

    // Copy new mapping.
    _vertices2d = newVertices2d;

    // Increment number of columns.
    _nRows++;

    // Reorder.
    _reorderVertices();
}
Exemple #6
0
matrix* FormatDisplacements(struct fe *p, matrix *Soln)
{
    matrix *Def;
    int i;
    Def = CreateMatrix(nRows(Soln)/2, 2);

    for(i=0; i<nRows(Soln)/2; i++) {
        setval(Def, val(Soln, 2*i, 0),  i, 0);
        setval(Def, val(Soln, 2*i+1, 0), i, 1);
    }

    return Def;
}
Exemple #7
0
matrix* GetDeformedCoords(struct fe *p, matrix *Soln)
{
    matrix *Def;
    int i;
    Def = CreateMatrix(nRows(Soln)/2, 2);

    for(i=0; i<nRows(Soln)/2; i++) {
        setval(Def, val(Soln, 2*i, 0) + valV(GetNodeCoordinates(p->mesh, i), 0),  i, 0);
        setval(Def, val(Soln, 2*i+1, 0) + valV(GetNodeCoordinates(p->mesh, i), 1), i, 1);
    }

    return Def;
}
Exemple #8
0
QPolygonF Mesh::toPolygon() const
{
    QPolygonF polygon;
    for (int i=0; i<nColumns(); i++)
        polygon.append(getVertex2d(i, 0));
    for (int i=0; i<nRows(); i++)
        polygon.append(getVertex2d(nColumns()-1, i));
    for (int i=nColumns()-1; i>=0; i--)
        polygon.append(getVertex2d(i, nRows()-1));
    for (int i=nRows()-1; i>=1; i--)
        polygon.append(getVertex2d(0, i));
    return polygon;
}
Exemple #9
0
  /**
   * Factor A. A is overwritten with the LU decomposition of A.
   */
  int SquareMatrix::factor() {
    integer n = static_cast<int>(nRows());
    int info=0;
    m_factored = true;
    ct_dgetrf(n, n, &(*(begin())), static_cast<int>(nRows()),
	      DATA_PTR(ipiv()), info);
    if (info != 0) {
      cout << "Singular matrix, info = " << info << endl;
      throw CanteraError("invert",
			 "DGETRF returned INFO="+int2str(info));
    }
    return 0;
  }
matrix* makedata(matrix *t, double T, double M)
{
    matrix *J;
    int i;
    double Ji, ti;
    J = CreateMatrix(nRows(t), 1);
    for(i=0; i<nRows(t); i++) {
        ti = val(t, i, 0);
        Ji = LLauraCreep(ti, T, M, 0);
        setval(J, Ji, i, 0);
    }
    return J;
}
Exemple #11
0
// equality test
bool Matrix::operator==( const Matrix& other ) const
{
    // compare dimensions
    if ( nRows() != other.nRows() || nCols() != other.nCols() )
        return false;
    
    // compare elements
    for ( int i = 0; i < nRows(); i++ ) {
        for ( int j = 0; j < nCols(); j++ ) {
            if ( getElement( i, j ) != other.getElement( i, j ) ) return false;	
        }	
    }
    
    return true;
}
Exemple #12
0
// check if square
bool Matrix::isSquare() const
{
    if ( nRows() == nCols() ) 
        return true;
    else 
        return false;	
}
Exemple #13
0
/**
 * Check the convergence of the nonlinear solver for a 1D problem.
 *
 * @param problem A pointer to the problem struct
 * @param dt A matrix containing the change in the x vector
 * @returns 0 if not converged, and 1 if we're done iterating.
 *
 * @see CheckConverg
 */
int CheckConverg1D(struct fe1d *problem, matrix *dx)
{
    int rows = nRows(dx);
    int i;

    for(i=0; i<rows; i++) {
        /* Chech to ensure that each element of the dx matrix is less than the
         * tolerance. If not, then return 0, indicating more iterations are
         * required. */
        if(fabs(val(dx, i, 0)) > problem->tol)
            return 0;

        /* If one of the values in the dx matrix is NaN, then clearly something
         * is wrong. The best thing to do is to quit and spit out an error. */
        if(isnan(val(dx, i, 0))) {
            printf("Nonlinear solver failed to converge. "
                       "Solver returned value of \"NaN\".\n"
                       "Failed to calculate solution at time step %d of %d\n"
                       "Exiting.\n",
                   problem->t, problem->maxsteps);
            exit(0);
        }
    }

    /* All the values are less than the specified tolerance, so we're good to
     * go. */
    return 1;
}
double PronyModel(double t, matrix* beta, void *params)
{
    matrix *Ji, *taui;
    double *p;
    double J0, J, Jval, tauval;
    int n, i;
    p = (double*) params;
    J0 = *p;

    /* Pull out all the parameter values */
    n = nRows(beta)/2;
    Ji = CreateMatrix(n, 1);
    taui = CreateMatrix(n, 1);

    for(i=0; i<n; i++) {
        setval(Ji, val(beta, 2*i, 0), i, 0);
        setval(taui, val(beta, 2*i+1, 0), i, 0);
    }

    J = J0;
    for(i=0; i<n; i++) {
        Jval = val(Ji, i, 0)*val(Ji, i, 0);
        tauval = val(taui, i, 0)*val(taui, i, 0);
        J += Jval * (1-exp(-t/tauval));
    }
    DestroyMatrix(Ji);
    DestroyMatrix(taui);
    return J;
}
Exemple #15
0
void SPxSolver::qualConstraintViolation(Real& maxviol, Real& sumviol) const
{
   maxviol = 0.0;
   sumviol = 0.0;

   DVector solu( nCols() );

   getPrimal( solu );

   for( int row = 0; row < nRows(); ++row )
   {
      const SVector& rowvec = rowVector( row );

      Real val = 0.0;         

      for( int col = 0; col < rowvec.size(); ++col )
         val += rowvec.value( col ) * solu[rowvec.index( col )];

      Real viol = 0.0;

      assert(lhs( row ) <= rhs( row ));

      if (val < lhs( row )) 
         viol = spxAbs(val - lhs( row ));
      else
         if (val > rhs( row ))
            viol = spxAbs(val - rhs( row ));

      if (viol > maxviol)
         maxviol = viol;

      sumviol += viol;
   }
}
Exemple #16
0
double _PsiElec(int m, int n, unifac_solution *s, double T)
{
    int i;
    matrix *a;
    double a_mn, b_mn, c_mn;
    double result;

    if(solution_group_count(m, s) == 0)
        return 0;
    if(solution_group_count(n, s) == 0)
        return 0;

    a = s->dat->interactions;

    for(i=0; i<nRows(a); i++) {
        if( (val(a, i, 0) == m) && (val(a, i, 1) == n) ) {
            a_mn = val(a, i, 2);
            b_mn = val(a, i, 3);
            c_mn = val(a, i, 4);
        }
    }

    result = (-a_mn + b_mn*T + c_mn*pow(T,2))/T;
    return result;
}
Exemple #17
0
void matrix::print_real(FILE* fp, const char* fmt) const
{	const complex* thisData = this->data();
	for(int i=0; i<nRows(); i++)
	{	for(int j=0; j<nCols(); j++)
			fprintf(fp, fmt, thisData[index(i,j)].real());
		fprintf(fp,"\n");
	}
}
int main(int argc, char *argv[])
{
    int i, j, k;
    double Ti, Mj, percent;
    vector *T, *M;
    matrix *t, *Jij, *betaij, *output, *ttmp;

    /*
    if(argc < 3) {
        printf("Usage:\n"
               "fitcreep: <file> <t1> <t2> ... <tn>\n"
               "<file>: Filename containing the creep function data.\n"
               "<t1>: First retardation time\n"
               "<t2>: Second retardation time\n"
               "...\n"
               "<tn>: Nth retardation time.\n");
        exit(0);
    }
    */

    T = linspaceV(333, 363, 10);
    M = linspaceV(.05, .4, 10);

    ttmp = linspace(1e-3, 1e3, 1000);
    t = mtxtrn(ttmp);
    DestroyMatrix(ttmp);

    output = CreateMatrix(len(T)*len(M), 2+5);

    for(i=0; i<len(T); i++) {
        Ti = valV(T, i);
        for(j=0; j<len(M); j++) {
            Mj = valV(M, j);
            Jij = makedata(t, Ti, Mj);
            betaij = fitdata(t, Jij);

            setval(output, Ti, i*len(M)+j, 0);
            setval(output, Mj, i*len(M)+j, 1);
            setval(output, val(Jij, 0, 0), i*len(M)+j, 2);
            for(k=0; k<nRows(betaij); k++)
                setval(output, pow(val(betaij, k, 0), 2), i*len(T)+j, k+3);
            DestroyMatrix(Jij);
            DestroyMatrix(betaij);

            /* Print the percent done */
            percent = (1.*i*len(M)+j)/(len(M)*len(T))*100.;
            printf("%3.2f %%\r", percent);
            fflush(stdout);
        }
    }
    
    DestroyMatrix(t);
    DestroyVector(T);
    DestroyVector(M);
    mtxprntfilehdr(output, "output.csv", "T,M,J0,J1,tau1,J2,tau2\n");
    DestroyMatrix(output);
    return 0;
}
Exemple #19
0
void Mesh::_reorderVertices()
{
    // Populate new vertices vector.
    QVector<QPointF> newVertices(vertices.size());
    int k = 0;
    for (int y=0; y<nRows(); y++)
        for (int x=0; x<nColumns(); x++)
            newVertices[k++] = getVertex2d( x, y );

    // Populate _vertices2d.
    k = 0;
    for (int y=0; y<nRows(); y++)
        for (int x=0; x<nColumns(); x++)
            _vertices2d[x][y] = k++;

    // Copy.
    vertices = newVertices;
}
Exemple #20
0
void matrix::scan(FILE* fp, const char* fmt)
{	complex* thisData = this->data();
	for(int i=0; i<nRows(); i++)
	{	for(int j=0; j<nCols(); j++)
		{	complex& c = thisData[index(i,j)];
			fscanf(fp, fmt, &c.real(), &c.imag());
		}
	}
}
Exemple #21
0
// Construct from a complex diagonal matrix:
matrix::matrix(const std::vector<complex>& d)
{	nr = d.size();
	nc = d.size();
	if(d.size())
	{	memInit("matrix", nr*nc); zero();
		complex* thisData = data();
		for(int i=0; i<nRows(); i++) thisData[index(i,i)] = d[i];
	}
}
Exemple #22
0
void Mesh::removeRow(int rowId)
{
  // Cannot remove first and last columns
  Q_ASSERT(rowId >= 1 && rowId < nRows()-1);

  // Temporary containers that will be used to rebuild new vertex space.
  IndexVector2d newVertices2d;
  resizeVertices2d(newVertices2d, nColumns(), nRows()-1);

  QVector<QPointF> newVertices(vertices.size()-nColumns());

  // Bottom displacement of points already there.
  qreal bottomMoveProp = 1.0f/(nRows()-2) - 1.0f/(nRows()-1);

  // Process all columns.
  int k = 0;
  for (int x=0; x<nColumns(); x++)
  {
    // Get top and bottom vertices.
    QPointF top    = getVertex2d(x, 0);
    QPointF bottom = getVertex2d(x, nRows()-1);
    QPointF diff   = bottom - top;

    // Move all rows.
    for (int y=0; y<nRows(); y++)
    {
      // Ignore points from target row.
      if (y == rowId)
        continue;

      // Get current vertex.
      QPointF p = getVertex2d( x, y );

      // The y value of this point in the new space.
      int newY = y < rowId ? y : y-1;

      // Move middle points.
      if (y > 0 && y < nRows()-1)
      {
        p += (y < rowId ? +1 : -1) * diff * newY * bottomMoveProp;
      }

      // Assign new containers.
      newVertices[k]         = p;
      newVertices2d[x][newY] = k;
      k++;
    }
  }

  // Copy new mapping.
  vertices    = newVertices;
  _vertices2d = newVertices2d;

  // Decrement number of rows.
  _nRows--;

  // Reorder.
  _reorderVertices();
}
Exemple #23
0
void matrix::diagonalize(matrix& evecs, diagMatrix& eigs) const
{	static StopWatch watch("matrix::diagonalize");
	watch.start();
	
	myassert(nCols()==nRows());
	int N = nRows();
	myassert(N > 0);
	
	//Check hermiticity
	const complex* thisData = data();
	double errNum=0.0, errDen=0.0;
	for(int i=0; i<N; i++)
		for(int j=0; j<N; j++)
		{	errNum += norm(thisData[index(i,j)]-thisData[index(j,i)].conj());
			errDen += norm(thisData[index(i,j)]);
		}
	double hermErr = sqrt(errNum / (errDen*N));
	if(hermErr > 1e-10)
	{	logPrintf("Relative hermiticity error of %le (>1e-10) encountered in diagonalize\n", hermErr);
		stackTraceExit(1);
	}
	
	char jobz = 'V'; //compute eigenvectors and eigenvalues
	char range = 'A'; //compute all eigenvalues
	char uplo = 'U'; //use upper-triangular part
	matrix A = *this; //copy input matrix (zheevr destroys input matrix)
	double eigMin = 0., eigMax = 0.; //eigenvalue range (not used for range-type 'A')
	int indexMin = 0, indexMax = 0; //eignevalue index range (not used for range-type 'A')
	double absTol = 0.; int nEigsFound;
	eigs.resize(N);
	evecs.init(N, N);
	std::vector<int> iSuppz(2*N);
	int lwork = (64+1)*N; std::vector<complex> work(lwork); //Magic number 64 obtained by running ILAENV as suggested in doc of zheevr (and taking the max over all N)
	int lrwork = 24*N; std::vector<double> rwork(lrwork); //from doc of zheevr
	int liwork = 10*N; std::vector<int> iwork(liwork); //from doc of zheevr
	int info=0;
	zheevr_(&jobz, &range, &uplo, &N, A.data(), &N,
		&eigMin, &eigMax, &indexMin, &indexMax, &absTol, &nEigsFound,
		eigs.data(), evecs.data(), &N, iSuppz.data(), work.data(), &lwork,
		rwork.data(), &lrwork, iwork.data(), &liwork, &info);
	if(info<0) { logPrintf("Argument# %d to LAPACK eigenvalue routine ZHEEVR is invalid.\n", -info); stackTraceExit(1); }
	if(info>0) { logPrintf("Error code %d in LAPACK eigenvalue routine ZHEEVR.\n", info); stackTraceExit(1); }
	watch.stop();
}
Exemple #24
0
// matrix addition
Matrix& Matrix::operator+=( const Matrix& other )
{
    // check dimensions
    assert( nRows() == other.nRows() && nCols() == other.nCols() );

    // addition
    gsl_matrix_add( data, other.data );
    
    return ( *this );
}
Exemple #25
0
  /*
   * Factor A. A is overwritten with the LU decomposition of A.
   */
  int SquareMatrix::factor() {
    if (useQR_) {
      return factorQR();
    }
    a1norm_ = ct_dlange('1', m_nrows, m_nrows, &(*(begin())), m_nrows, DATA_PTR(work));
    integer n = static_cast<int>(nRows());
    int info=0;
    m_factored = 1;
    ct_dgetrf(n, n, &(*(begin())), static_cast<int>(nRows()), DATA_PTR(ipiv()), info);
    if (info != 0) {
      if (m_printLevel) {
	writelogf("SquareMatrix::factor(): DGETRS returned INFO = %d\n", info);
      }
      if (! m_useReturnErrorCode) {
        throw CELapackError("SquareMatrix::factor()", "DGETRS returned INFO = "+int2str(info));
      }
    }
    return info;
  }
Exemple #26
0
void stfnum::Table::AppendRows(std::size_t nRows_) {
    std::size_t oldRows=nRows();
    rowLabels.resize(oldRows+nRows_);
    values.resize(oldRows+nRows_);
    empty.resize(oldRows+nRows_);
    for (std::size_t nRow = 0; nRow < oldRows + nRows_; ++nRow) {
        values[nRow].resize(nCols());
        empty[nRow].resize(nCols());
    }
}
Exemple #27
0
void matrix::scan_real(FILE* fp)
{	complex* thisData = this->data();
	for(int i=0; i<nRows(); i++)
	{	for(int j=0; j<nCols(); j++)
		{	complex& c = thisData[index(i,j)];
			fscanf(fp, "%lg", &c.real());
			c.imag() = 0;
		}
	}
}
Exemple #28
0
// matrix multiplication
Matrix Matrix::operator*( const Matrix& other ) const
{
    // check dimensions
    assert( nCols() == other.nRows() );
    
    // matrix multiplication
    Matrix result( nRows(), other.nCols() );
    gsl_linalg_matmult( data, other.data, result.data );
    
    return result;
}
Exemple #29
0
// matrix addition
Matrix Matrix::operator+( const Matrix& other ) const
{
    // check dimensions
    assert( nRows() == other.nRows() && nCols() == other.nCols() );
    
    // addition
    Matrix result( *this );
    gsl_matrix_add( result.data, other.data );	

    return result;
}
Exemple #30
0
bool Matrix::isSymmetric() const
{
    assert( isSquare() );
    for ( int i = 0; i < nRows(); i++ ) {
        for ( int j = 0; j < i; j++ ) {
            if ( abs( getElement( i, j ) - getElement( j, i ) ) > DBL_EPSILON ) return false;
        }
    }

    return true;
}