From deea50d8cef6556714774b5833ba2f8cbdf45610 Mon Sep 17 00:00:00 2001 From: sebastien Date: Fri, 17 Oct 2008 09:46:50 +0000 Subject: [PATCH] trunk simulate.dll: removed non-GPL code git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@2164 ac1d8469-bf42-47a9-8791-bf33cf982152 --- mex/sources/build_octave.m | 2 +- mex/sources/simulate/Interpreter.cc | 8 + mex/sources/simulate/Interpreter.hh | 4 +- mex/sources/simulate/linbcg.cc | 1498 --------------------------- mex/sources/simulate/linbcg.hh | 458 -------- mex/sources/simulate/simulate.cc | 1 - 6 files changed, 12 insertions(+), 1959 deletions(-) delete mode 100644 mex/sources/simulate/linbcg.cc delete mode 100644 mex/sources/simulate/linbcg.hh diff --git a/mex/sources/build_octave.m b/mex/sources/build_octave.m index 673edb2f7..8ce220ad4 100644 --- a/mex/sources/build_octave.m +++ b/mex/sources/build_octave.m @@ -52,4 +52,4 @@ eval([ COMPILE_COMMAND ' -DMATLAB -Igensylv/cc ' ... 'gensylv/cc/Vector.cpp ' ... '-o ../octave/gensylv.mex']); disp('Compiling simulate...') -eval([ COMPILE_COMMAND ' -Isimulate -I../../preprocessor/include simulate/simulate.cc simulate/Interpreter.cc simulate/Mem_Mngr.cc simulate/SparseMatrix.cc simulate/linbcg.cc -o ../octave/simulate.mex']); +eval([ COMPILE_COMMAND ' -Isimulate -I../../preprocessor/include simulate/simulate.cc simulate/Interpreter.cc simulate/Mem_Mngr.cc simulate/SparseMatrix.cc -o ../octave/simulate.mex']); diff --git a/mex/sources/simulate/Interpreter.cc b/mex/sources/simulate/Interpreter.cc index 53ab86daa..739b41c2e 100644 --- a/mex/sources/simulate/Interpreter.cc +++ b/mex/sources/simulate/Interpreter.cc @@ -579,9 +579,11 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba int giter; int u_count_int; double *y_save; +#ifdef LINBCG LinBCG linbcg; Mat_DP a; Vec_INT indx; +#endif //SparseMatrix sparse_matrix; int nb_endo, u_count_init; @@ -960,6 +962,7 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba begining=get_code_pointer; if(!Gaussian_Elimination) { +#ifdef LINBCG it_=y_kmin; Per_u_=0; Per_y_=it_*y_size; @@ -967,6 +970,7 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba compute_block_time(); linbcg.Initialize(filename, res1, res2, max_res, slowc, ya, direction, iter); linbcg.Preconditioner(periods, y_kmin, y_kmax, size, IM_i, index_vara, index_equa, y_size, y, true, 0, a, indx); +#endif } //GaussSeidel=false; giter=0; @@ -1044,8 +1048,10 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba } else { +#ifdef LINBCG linbcg.Initialize(filename, res1, res2, max_res, slowc, ya, direction, iter); linbcg.SolveLinear(periods, y_kmin, y_kmax, size, IM_i, index_vara, index_equa,y_size,y, true, cvg, a, indx); +#endif } iter++; } @@ -1077,8 +1083,10 @@ Interpreter::simulate_a_block(int size,int type, string file_name, string bin_ba simulate_NG1(Block_Count, symbol_table_endo_nbr, it_, y_kmin, y_kmax, size, periods, true, cvg, iter); else { +#ifdef LINBCG linbcg.Initialize(filename, res1, res2, max_res, slowc, ya, direction, iter); linbcg.SolveLinear(periods, y_kmin, y_kmax, size, IM_i, index_vara, index_equa, y_size, y, true, cvg, a, indx); +#endif } } #ifdef DEBUGC diff --git a/mex/sources/simulate/Interpreter.hh b/mex/sources/simulate/Interpreter.hh index a03cb41ed..25cd73cb1 100644 --- a/mex/sources/simulate/Interpreter.hh +++ b/mex/sources/simulate/Interpreter.hh @@ -29,7 +29,9 @@ //#include "ExprNode.hh" //#include "Mem_Mngr.hh" #include "SparseMatrix.hh" -#include "linbcg.hh" +#ifdef LINBCG +# include "linbcg.hh" +#endif #include "mex.h" //#define DEBUGC diff --git a/mex/sources/simulate/linbcg.cc b/mex/sources/simulate/linbcg.cc deleted file mode 100644 index dba8e786b..000000000 --- a/mex/sources/simulate/linbcg.cc +++ /dev/null @@ -1,1498 +0,0 @@ -/* - * Copyright (C) 2007-2008 Dynare Team - * - * This file is part of Dynare. - * - * Dynare is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dynare is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dynare. If not, see . - */ - -#include - -#include "linbcg.hh" -//#define DEBUG - -LinBCG::LinBCG() -{ -} - - -void LinBCG::Preconditioner(int periods, int y_kmin, int y_kmax, int Size, std::map ,int>, int> IM_i, int* index_vara, int* index_equa, int y_size, double *y, bool print_it, int type, Mat_O_DP &a, Vec_O_INT &indx) -{ - std::map, int> , int>::iterator it4_i; - std::map, double>::iterator it4; - int i, j, k, eq=0, eqp, var, lag, pos_u; - std::map, double> IM; - LinBCG U_linbcg, L_linbcg; - Vec_INT ijL, ijU; - Vec_DP sL, sU; - double d; - - IM.clear(); - it4_i=IM_i.begin(); - int u_size=IM_i.size(); - //mexPrintf("Preconditioner\n"); - double u_pos_u; - while(it4_i!=IM_i.end()) - { - eq= it4_i->first.first.first; - var=it4_i->first.first.second; - lag=it4_i->first.second; - pos_u=it4_i->second; - if(pos_u>=Size) // to eliminate the residual contain in the first (Size) elements of IM - { - //mexPrintf("IM_i[%d, (%d, %d)]=%d val=%f ",eq, var, lag, pos_u, u[pos_u]); - IM[make_pair(eq,var-lag*Size)]+=u[pos_u/*+(periods-1)*u_size*/]; - //mexPrintf(" IM[%d, %d]=%f\n",eq,var-lag*Size, IM[make_pair(eq,var-lag*Size)]); - } - it4_i++; - } - int Size_SparseMatrixRow=IM.size(); - //mexPrintf("Size_SparseMatrixRow+1=%d\n",Size_SparseMatrixRow+1); - int ija_d[Size_SparseMatrixRow+1]; - DP sa_d[Size_SparseMatrixRow+1]; - k=Size; - ija_d[0]=k+1; - j=0; - eqp=-99; - it4=IM.begin(); - while(it4!=IM.end()) - { - eq=it4->first.first; - var=it4->first.second; - u_pos_u=it4->second; - //mexPrintf("IM[eq=%d, var=%d]=%f \n",eq, var, u_pos_u); - if(eq!=eqp) - ija_d[eq+j*Size]=k+1; - if(eq==var) - sa_d[var+j*Size]+=u_pos_u;//u[pos_u+j*u_size]; - else - { - k++; - sa_d[k]+=u_pos_u;//u[pos_u+j*u_size]; - ija_d[k]=var+j*Size; - } - eqp=eq; - it4++; - } - eq=Size;//Size*periods; - ija_d[eq]=k+1; - sa_d[eq]=0; - ija_p.Format(ija_d,Size_SparseMatrixRow+1); - sa_p.Format(sa_d,Size_SparseMatrixRow+1); - /*sprsprt(); - sprs_sprt();*/ - //mexPrintf("ok0\n"); - a.Format(Size,Size); - sprsout(a); - indx.Format(Size); - //mexPrintf("ok1\n"); - /*Mat_DP aa(0.0,Size*periods,Size*periods); - for(k=0;k ,int>, int> IM_i, int* index_vara, int* index_equa, int y_size, double *y, bool print_it, bool cvg, Mat_DP &a, Vec_IO_INT &indx) -{ - // Solve iteratively the systme A*x=b - // A is sparse and rewritten using sa_p and ija_p - mexPrintf("SolveLinear\n"); - mexEvalString("drawnow;"); - std::map, int> , int>::iterator it4_i; - std::map >, int>::iterator it4; - int i, j, k, eq=0, eqp, var, lag, pos_u; - std::map >, int> IM; - clock_t t1 = clock(); - IM.clear(); - it4_i=IM_i.begin(); - int u_size=IM_i.size(); - int Size_SparseMatrixRow=0; - Vec_DP b(Size*periods), x(Size*periods); - int ITMAX=10*Size*periods; - double TOL=1e-9; - int ITOL=1; - int sub_iter; - DP err; - //Vec_DP Err(Size*periods); - - if (iter>0) - mexPrintf("Sim : %f ms\n",(1000.0*(double(clock())-double(time00)))/double(CLOCKS_PER_SEC)); - if (isnan(res1) || isinf(res1)) - { - if (slowc_save<1e-8) - { - mexPrintf("Dynare cannot improve the simulation\n"); - mexEvalString("drawnow;"); - filename+=" stopped"; - mexEvalString("st=fclose('all');clear all;"); - mexErrMsgTxt(filename.c_str()); - } - slowc_save/=2; - mexPrintf("Error: Simulation diverging, trying to correct it using slowc=%f\n",slowc_save); - for (i=0;ifirst.first.first; - var=it4_i->first.first.second; - lag=it4_i->first.second; - pos_u=it4_i->second; - if(pos_u>=Size) // to eliminate the residual contain in the first (Size) elements of IM - { - //mexPrintf("IM[%d, (%d, %d)]=%d\n",eq, var, lag, pos_u); - IM[make_pair(eq,make_pair(var,lag))]=pos_u; - for(j=0;j<=min(y_kmax+y_kmin,periods-1);j++) - { - //mexPrintf("j=%d\n",j); - if(j-y_kmin<=0) - k=j; - else - k=periods+j-y_kmin-y_kmax-1; - if(lag+j<0 || lag+j>min(periods-1,y_kmax+y_kmin)) - { - //mexPrintf("eliminate b[eq+(k)*Size=%d]=u[%d]*y[%d]\n",eq+(k)*Size,pos_u+(k)*u_size,var+(k+1)*y_size); - b[eq+(k)*Size]-=u[pos_u+k*u_size]*y[index_vara[var-lag*Size]+(k+lag+1)*y_size]; - Size_SparseMatrixRow--; - } - - } - } - it4_i++; - } - //mexPrintf("Size_SparseMatrixRow+1=%d\n",Size_SparseMatrixRow+1); - int ija_d[Size_SparseMatrixRow+1]; - DP sa_d[Size_SparseMatrixRow+1]; - k=Size*periods; - ija_d[0]=k+1; - for(j=0;jfirst.first; - var=it4->first.second.first; - lag=it4->first.second.second; - pos_u=it4->second; - /*if(j==1) - mexPrintf("IM[eq=%d, var=%d, lag=%d]=%d val=%f \n",eq, var, lag, pos_u,u[pos_u+j*u_size]);*/ - if(lag+j>=0 && lag+j=0 && j+lag fabs(sx[isamax])) isamax=i; - } - return fabs(sx[isamax]); - } -} - -void LinBCG::sprsax(Vec_I_DP &sa, Vec_I_INT &ija, Vec_I_DP &x, Vec_O_DP &b) -{ - int i,k; - //mexPrintf("sprsax\n"); - int n=x.size(); - /*mexPrintf("n=%d\n",n); - mexPrintf("ija[0]=%d\n",ija[0]);*/ - if (ija[0] != n+1) - mexPrintf("Error: \n sprsax: mismatched vector and matrix\n"); - /*mexPrintf("ok0a\n");*/ - for (i=0;i=thresh && ((i)%(NbRow+1))!=0) - j++; - } - //mexPrintf("j=%d\n",j); - ija_p.Format(j+NbRow+1); - sa_p.Format(j+NbRow+1); - //mexPrintf("nz=%d\n",j+NbRow+1); - for(j=0;j= thresh && i != j) - { - sa_p[++k]=a[j*n+i]; - ija_p[k]=j; - } - } - ija_p[i+1]=k+1; - } -} - -void LinBCG::sprsin(Mat_DP &a, const DP thresh) -{ - int i,j,k=0; - - int n=a.nrows(); - for(i=0;i=thresh && j!=i) - k++; - ija_p.Format(k+n+1); - sa_p.Format(k+n+1); - for (j=0;j= thresh && i != j) { - //if (++k > nmax) nrerror("sprsin: sa and ija too small"); - k++; - sa_p[k]=a[i][j]; - ija_p[k]=j; - } - } - ija_p[i+1]=k+1; - } -} - - -void LinBCG::sprsout(Mat_DP &a) -{ - int i,j,k,l,ke; - double maxi=0; - int N=ija_p[0]-1; - //int total=ija_p[N]; - //mexPrintf("sprsout\n"); - for(i=0;imaxi) - maxi=fabs(sa_p[i]); - int size=int(log10(maxi))+1; - if(size<7) - { - sprintf(fmt, "%d", size+8); - pf= fmt; - pf.insert(0,"% "); - pf.append(".6f "); - pf.copy(fmt,8); - } - else - strcpy(fmt,"%e "); - for(i=0;imaxi) - maxi=fabs(sa_p[i]); - int size=int(log10(maxi))+1; - if(size<7) - { - sprintf(fmt, "%d", size+8); - pf= fmt; - pf.insert(0,"% "); - pf1=pf2=pf; - pf.append(".6f "); - pf1.append("s "); - pf2.append("d "); - pf.copy(fmt,8); - pf1.copy(fmts,8); - pf2.copy(fmtd,8); - } - else - strcpy(fmt,"%e "); - mexPrintf(fmts,"i"); - mexPrintf(fmts,"ija"); - mexPrintf(fmts,"sa"); - if(ijat_p.size()) - { - mexPrintf(fmts,"ijat"); - mexPrintf(fmts,"sat"); - } - mexPrintf("\n"); - for(i=0;i EPS*znrm) - { - dxnrm=fabs(ak)*snrm(p,itol); - err=znrm/fabs(zm1nrm-znrm)*dxnrm; - } - else - { - err=znrm/bnrm; - continue; - } - xnrm=snrm(x,itol); - if (err <= 0.5*xnrm) - err /= xnrm; - else - { - err=znrm/bnrm; - continue; - } - } - //cout << "iter=" << setw(4) << iter+1 << setw(12) << err << endl; - mexPrintf("iter=%d err=%f ak=%f akden=%f\n",iter,err,ak,akden); - if (err <= tol) - break; - } -} - -void LinBCG::BiCGStab(Vec_I_DP &b, Vec_IO_DP &x, const int itol, const DP tol, - const int itmax, int &iter, DP &err, Mat_DP &a, Vec_IO_INT &indx, const int periods) -{ - DP akden,bkden=1.0,bknum,bnrm=0,dxnrm,xnrm,zm1nrm,znrm=0, alpha, omega, beta, omega_num, omega_den, snorm; - const DP EPS=1.0e-14; - int j; - int n=b.size(); - //mexPrintf("n=%d\n",n); - Vec_DP p(n),v(n),r(n),rr(n),z(n),s(n), t(n), phat(n), shat(n); - iter=0; - // Compute the residual - atimes(x,r,0); - for (j=0;j EPS*znrm) - { - dxnrm=fabs(alpha)*snrm(p,itol); - err=znrm/fabs(zm1nrm-znrm)*dxnrm; - } - else - { - err=znrm/bnrm; - continue; - } - xnrm=snrm(x,itol); - if (err <= 0.5*xnrm) - err /= xnrm; - else - { - err=znrm/bnrm; - continue; - } - } - //cout << "iter=" << setw(4) << iter+1 << setw(12) << err << endl; - mexPrintf("iter=%d err=%f alpha=%f akden=%f\n",iter,err,alpha,akden); - mexEvalString("drawnow;"); - if (err <= tol) - break; - } -} - - -void LinBCG::sprs_col_index() -{ - int i, j, k, begin, end; - int nze=sa_p.size(); - ijat_p.Format(nze); - sat_p.Format(nze); - begin=ija_p[0]; - int n=begin-1; - //mexPrintf("n=%d\n",n); - map, double> list; - map, double>::iterator list_it; - for(j=0;jfirst.first=%d, list_it->first.second=%d, list_it->second=%f\n",list_it->first.first, list_it->first.second, list_it->second); - while(col!=list_it->first.first) - { - //while(col!=list_it->first.first - col++; - ijat_p[col]=cur_pos; - } - ijat_p[cur_pos]=list_it->first.second; - sat_p[cur_pos++]=list_it->second; - list_it++; - } -} - - -void LinBCG::sprs_swap_line_copy(map, double> &list, int &pos_2_blck, int begin, int end) -{ - int i, j, init_pos; - for(i=begin;i, double> &list, int &pos_2_blck, int LS, int LD) -{ - int j; - bool OK=false, OK1=true; - int init_pos=pos_2_blck; - for(j=ija_p[LS];j=LS && OK1) - { - OK1=false; - if(sa_p[LS]!=0.0) - { - //mexPrintf("list[%d,%d]=%f\n",pos_2_blck,LS,sa_p[LS]); - list[make_pair(pos_2_blck++,LS)]=sa_p[LS]; - } - } - //mexPrintf("list[%d,%d]=%f\n",pos_2_blck,ija_p[j],sa_p[j]); - list[make_pair(pos_2_blck++,ija_p[j])]=sa_p[j]; - } - } - if(OK1) - { - if(sa_p[LS]!=0.0) - { - //mexPrintf("list[%d,%d]=%f\n",pos_2_blck,LS,sa_p[LS]); - list[make_pair(pos_2_blck++,LS)]=sa_p[LS]; - } - } - if(!OK) - { - list[make_pair(LD,init_pos)]=0.0; - //mexPrintf("not found list[%d,%d]=%f\n",LD,init_pos,0.0); - } -} - -void LinBCG::sprs_swap_line(int L0, int L1) -{ - int i,j,k, pos_2_blck, init_pos; - map, double> list; - map, double>::iterator list_it; - int n=ija_p[0]-1; - if(L0>L1) - { - i=L0; - L0=L1; - L1=i; - } - else if(L0==L1) - return; - else if(L0>=n || L1>=n) - { - mexPrintf("out of range in sprs_swap_line (L0=%d and L1=%d)\n",L0, L1); - mexEvalString("drawnow;"); - mexEvalString("st=fclose('all');clear all;"); - filename+=" stopped"; - mexErrMsgTxt(filename.c_str()); - } - int nze=sa_p.size(); - bool OK, OK1; - int nze0=0, nze1=0; - pos_2_blck=n+1; - - sprs_swap_line_copy(list, pos_2_blck, 0, L0); - - //mexPrintf("L1=>L0\n"); - - sprs_swap_line_exchange(list, pos_2_blck, L1, L0); - - sprs_swap_line_copy(list, pos_2_blck, L0+1, L1); - - //mexPrintf("L0=>L1\n"); - - sprs_swap_line_exchange(list, pos_2_blck, L0, L1); - - sprs_swap_line_copy(list, pos_2_blck, L1+1, n); - - sa_p.Format(list.size()+1); - ija_p.Format(list.size()+1); - //mexPrintf("list.size()+1=%d\n",list.size()+1); - list_it=list.begin(); - while(list_it!=list.end()) - { - //mexPrintf("sa_p[list_it->first.first=%d]=list_it->second=%f\n",list_it->first.first,list_it->second); - sa_p[list_it->first.first]=list_it->second; - //mexPrintf("ija_p[list_it->first.first=%d]=list_it->first.second=%d\n",list_it->first.first,list_it->first.second); - ija_p[list_it->first.first]=list_it->first.second; - list_it++; - } - ija_p[n]=list.size()+1; - sa_p[n]=0; - sprs_col_index(); -} - - - -void LinBCG::sprsludcmp(Mat_DP &a, Vec_O_INT &indx, DP &d) -{ - const DP TINY=1.0e-20; - int i,imax,j,k, ii, ik, begin_ija, end_ija, begin_ijat, end_ijat, dum1, dum2; - DP big,dum,sum,temp; - Vec_DP saa_p(sa_p); - Vec_INT ijaa_p(ija_p); - int nze=sa_p.size(); - int n=ija_p[0]-1; - Vec_DP vv(n); - d=1.0; - - n=a.nrows(); - //a=(double**)mxMalloc(n*n*sizeof(double)); - //mexPrintf("sprsludcmp\n"); - - for (i=0;i big) big=temp; - } - //mexPrintf("\n"); - if(big == 0.0) - { - mexPrintf("Singular Matrix in routine sprsludcmp\n"); - mexEvalString("st=fclose('all');clear all;"); - mexErrMsgTxt("End of simulate"); - } - vv[i]=1.0/big; - } - /*mexPrintf("First Step\n"); - mexPrintf("a.ncols()=%d\n",a.ncols()); - mexPrintf("a.nrows()=%d\n",a.nrows());*/ - /*for(i=0;ibig) - big=temp; - if(big==0.0) - { - mexPrintf("Singular Matrix in routine sprsludcmp\n"); - mexErrMsgTxt("End of simulate"); - } - vv[i]=1.0/big; - }*/ - /*sprs_col_index(); // create a col-index compact storage - for (j=0;jijat_p[begin_ijat]) - begin_ijat++; - else - sum -= sa_p[begin_ija++]*sat_p[begin_ijat++]; - } - sat_p[i]=sum; - } - big=0.0; - for (;iijat_p[begin_ijat]) - begin_ijat++; - else - sum -= sa_p[begin_ija++]*sat_p[begin_ijat++]; - } - sat_p[i]=sum; - if((dum=vv[ija_p[i]]*fabs(sum)) >= big) - { - big=dum; - mexPrintf("imax=i=%d\n",i); - imax=ija_p[i]; - } - } - if (j != imax) - { - sprs_swap_line(j,imax); - //for (k=0;kj) - sa_p[i] *= dum; - } - } -}*/ - for (j=0;j= big) - { - big=dum; - imax=i; - } - } - //mexPrintf("ok1\n"); - if (j != imax) - { - for (k=0;k=0;k--) - { - for (i=n-1;i>=0;i--) - { - sum=b[i+k*n]; - for (j=i+1;j=0;k--) - { - for (i=n-1;i>=0;i--) - { - sum=b[i+k*n]; - for (j=i+1;j=0;i--) - { - sum=b[i]; - for (j=i+1;j (LU)'.x=b <=> U'L'x=b <=> U'.y=b with x solution of L'x=y - int i,ii,ip,j; - DP sum; - int n=a.nrows(); - ii=0; - // Solving U'.y=b - // U' is a lower triangular matrix with element on the main diagonal differnet from 1 - // Implementation of a foreward substitution to solve this system - for (i=0;i=0;i--) - { - sum=b[i]; - for (j=i+1;j. - */ - -#ifndef LINBCG_HH_INCLUDED -#define LINBCG_HH_INCLUDED - -#include -#include -#include -#include -#include -#include -#include -#include "mex.h" - -using namespace std; - -const int debile=10; -const double tol=1e-10; - -typedef double DP; - - -template -class NRVec { -private: - int nn; // size of array. upper index is nn-1 - T *v; -public: - NRVec(); - explicit NRVec(int n); // Zero-based array - NRVec(const T &a, int n); //initialize to constant value - NRVec(const T *a, int n); // Initialize to array - NRVec(const NRVec &rhs); // Copy constructor - NRVec & operator=(const NRVec &rhs); //assignment - NRVec & operator=(const T &a); //assign a to every element - // NRVec & operator=(const T* rhs); - inline T & operator[](const int i); //i'th element - inline const T & operator[](const int i) const; - inline int size() const; - inline void Format(T* rhs, int n); - inline void Format(const NRVec rhs); - inline void Format(int n); - inline void Print(); - ~NRVec(); -}; - -template -NRVec::NRVec() : nn(0), v(0) {} - -template -NRVec::NRVec(int n) : nn(n), v(/*new T[n]*/(T*)mxMalloc(sizeof(T)*n)) {} - -template -NRVec::NRVec(const T& a, int n) : nn(n), v(/*new T[n]*/(T*)mxMalloc(sizeof(T)*n)) -{ - for(int i=0; i -NRVec::NRVec(const T *a, int n) : nn(n), v(/*new T[n]*/(T*)mxMalloc(sizeof(T)*n)) -{ - for(int i=0; i -NRVec::NRVec(const NRVec &rhs) : nn(rhs.nn), v(/*new T[nn]*/(T*)mxMalloc(sizeof(T)*rhs.nn)) -{ - for(int i=0; i -void NRVec::Format(T* rhs, int n) -{ - if(v) - mxFree(v); - v=NULL; - nn=n; - v=(T*)mxMalloc(sizeof(T)*n); - for(int i=0; i -void NRVec::Format(const NRVec rhs) -{ - if(v) - mxFree(v); - v=NULL; - nn=rhs.nn; - v=(T*)mxMalloc(sizeof(T)*nn); - for(int i=0; i -void NRVec::Format(int n) -{ - if(v) - mxFree(v); - v=NULL; - nn=n; - v=(T*)mxMalloc(sizeof(T)*n); -} - - -template -NRVec & NRVec::operator=(const NRVec &rhs) -// postcondition: normal assignment via copying has been performed; -// if vector and rhs were different sizes, vector -// has been resized to match the size of rhs -{ - if (this != &rhs) - { - if (nn != rhs.nn) - { - if (v != 0) - mxFree(v); - nn=rhs.nn; - v= (T*)mxMalloc(sizeof(T)*nn); - } - for (int i=0; i -void NRVec::Print() -{ - int i; - for(i=0;i -NRVec & NRVec::operator=(const T* rhs) -// postcondition: normal assignment via copying has been performed; -// if vector and rhs were different sizes, vector -// has been resized to match the size of rhs -{ - if (this != &rhs) - { - if (v != 0) - mxFree(v); - v= (T*)mxMalloc(sizeof(T)*nn); - } - for (int i=0; i -NRVec & NRVec::operator=(const T &a) //assign a to every element -{ - for (int i=0; i -inline T & NRVec::operator[](const int i) //subscripting -{ - if(i>=nn || i<0) - mexPrintf("Error: out of range in Vect[] operator\n"); - return v[i]; -} - -template -inline const T & NRVec::operator[](const int i) const //subscripting -{ - if(i>=nn || i<0) - mexPrintf("Error: out of range in Vect[] operator\n"); - return v[i]; -} - -template -inline int NRVec::size() const -{ - return nn; -} - -extern double *u; -template -NRVec::~NRVec() -{ - if (v != 0) - mxFree(v); -} - - -template -class NRMat { -private: - int nn; - int mm; - T **v; -public: - NRMat(); - NRMat(int n, int m); // Zero-based array - NRMat(const T &a, int n, int m); //Initialize to constant - NRMat(const T *a, int n, int m); // Initialize to array - NRMat(const NRMat &rhs); // Copy constructor - NRMat & operator=(const NRMat &rhs); //assignment - NRMat & operator=(const T &a); //assign a to every element - inline T* operator[](const int i); //subscripting: pointer to row i - inline const T* operator[](const int i) const; - inline int nrows() const; - inline int ncols() const; - inline void Format(const int n,const int m); - void Print(); - ~NRMat(); -}; - -template -NRMat::NRMat() : nn(0), mm(0), v(0) {} - -template -NRMat::NRMat(int n, int m) : - nn(n), - mm(m), - v((T**)mxMalloc(sizeof(T*)*n)) -{ - v[0] = /*new T[m*n]*/(T*)mxMalloc(sizeof(T)*m*n); - for (int i=1; i< n; i++) - v[i] = v[i-1] + m; -} - -template -NRMat::NRMat(const T &a, int n, int m) : nn(n), mm(m), v(/*new T*[n]*/(T**)mxMalloc(sizeof(T*)*n)) -{ - int i,j; - v[0] = /*new T[m*n]*/(T*)mxMalloc(sizeof(T)*m*n); - for (i=1; i< n; i++) - v[i] = v[i-1] + m; - for (i=0; i< n; i++) - for (j=0; j -NRMat::NRMat(const T *a, int n, int m) : nn(n), mm(m), v(/*new T*[n]*/(T**)mxMalloc(sizeof(T*)*n)) -{ - int i,j; - v[0] = /*new T[m*n]*/(T*)mxMalloc(sizeof(T)*m*n); - for (i=1; i< n; i++) - v[i] = v[i-1] + m; - for (i=0; i< n; i++) - for (j=0; j -NRMat::NRMat(const NRMat &rhs) : nn(rhs.nn), mm(rhs.mm), v(/*new T*[nn]*/(T**)mxMalloc(sizeof(T*)*nn)) -{ - int i,j; - v[0] = /*new T[mm*nn]*/(T*)mxMalloc(sizeof(T)*mm*nn); - for (i=1; i< nn; i++) - v[i] = v[i-1] + mm; - for (i=0; i< nn; i++) - for (j=0; j -NRMat & NRMat::operator=(const NRMat &rhs) -// postcondition: normal assignment via copying has been performed; -// if matrix and rhs were different sizes, matrix -// has been resized to match the size of rhs -{ - if (this != &rhs) { - int i,j; - if (nn != rhs.nn || mm != rhs.mm) { - if (v != 0) - { - mxFree(v[0]); - mxFree(v); - /*delete[] (v[0]); - delete[] (v);*/ - } - nn=rhs.nn; - mm=rhs.mm; - //v = new T*[nn]; - v = (T**)mxMalloc(sizeof(T*)*nn); - v[0] = /*new T[mm*nn]*/(T*)mxMalloc(sizeof(T)*mm*nn); - } - for (i=1; i< nn; i++) - v[i] = v[i-1] + mm; - for (i=0; i< nn; i++) - for (j=0; j -NRMat & NRMat::operator=(const T &a) //assign a to every element -{ - for (int i=0; i< nn; i++) - for (int j=0; j -inline T* NRMat::operator[](const int i) //subscripting: pointer to row i -{ - if(i>=nn || i<0) - mexPrintf("Error: out of range in Mat[] operator\n"); - return v[i]; -} - -template -inline const T* NRMat::operator[](const int i) const -{ - if(i>=nn || i<0) - mexPrintf("Error: out of range in Mat[] operator\n"); - return v[i]; -} - -template -inline int NRMat::nrows() const -{ - return nn; -} - -template -inline int NRMat::ncols() const -{ - return mm; -} - -template -inline void NRMat::Format(const int n,const int m) -{ - if (v != 0) - { - /*delete[] (v[0]); - delete[] (v);*/ - mxFree(v[0]); - mxFree(v); - } - nn=n; - mm=m; - v = (T**)mxMalloc(sizeof(T*)*n);//new T*[n]; - v[0] = (T*)mxMalloc(sizeof(T)*m*n);//new T[m*n]; - for (int i=1; i< n; i++) - v[i] = v[i-1] + m; -} - - - -template -NRMat::~NRMat() -{ - if (v != 0) - { - mxFree(v[0]); - mxFree(v); - /*delete[] (v[0]); - delete[] (v);*/ - } -} - - -template -void NRMat::Print() -{ - int i,j; - mexPrintf("nn=%d, mm=%d\n",nn,mm); - for(i=0;i Vec_I_INT; -typedef NRVec Vec_INT, Vec_O_INT, Vec_IO_INT; -typedef const NRVec Vec_I_DP; -typedef NRVec Vec_DP, Vec_O_DP, Vec_IO_DP; - -typedef const NRMat Mat_I_DP; -typedef NRMat Mat_DP, Mat_O_DP, Mat_IO_DP; - - - - -class LinBCG -{ - Vec_INT ija_p, ijat_p; - Vec_DP sa_p, sat_p; - private: - - - clock_t time00; - string filename; - double res1, res2, slowc, slowc_save, *ya, *direction, max_res; - int iter; - - void sprs_swap_line_copy(map, double> &list, int &pos_2_blck, int begin, int end); - void sprs_swap_line_exchange(map, double> &list, int &pos_2_blck, int LS, int LD); - - public: - LinBCG(); - void SolveLinear(int periods, int y_kmin, int y_kmax, int Size, std::map ,int>, int> IM, int* index_vara, int* index_equa, int y_size, double* y, bool print_it, bool cvg, Mat_DP &a, Vec_IO_INT &indx); - void Preconditioner(int periods, int y_kmin, int y_kmax, int Size, std::map ,int>, int> IM, int* index_vara, int* index_equa, int y_size, double* y, bool print_it, int type, Mat_O_DP &a, Vec_O_INT &indx); - void asolve(Vec_I_DP &b, Vec_O_DP &x, const int itrnsp, Mat_DP &a, Vec_IO_INT &indx, const int periods); - void atimes(Vec_I_DP &x, Vec_O_DP &r, const int itrnsp); - DP snrm(Vec_I_DP &sx, const int itol); - void sprsax(Vec_I_DP &sa, Vec_I_INT &ija, Vec_I_DP &x, Vec_O_DP &b); - void sprstx(Vec_I_DP &sa, Vec_I_INT &ija, Vec_I_DP &x, Vec_O_DP &b); - void sprsin(double *a, int NbRow, int NbCol, const DP thresh); - void sprsin(Vec_DP s, Vec_INT ij); - void sprsin(DP *s, int *ij, int size); - void sprsin(Mat_DP &a, const DP thresh); - void sprsout(Mat_DP &a); - void BiCG(Vec_I_DP &b, Vec_IO_DP &x, const int itol, const DP tol, - const int itmax, int &iter, DP &err, Mat_DP &a, Vec_IO_INT &indx, const int periods); - void BiCGStab(Vec_I_DP &b, Vec_IO_DP &x, const int itol, const DP tol, - const int itmax, int &iter, DP &err, Mat_DP &a, Vec_IO_INT &indx, const int periods); - void sprsprt(); - void sprs_sprt(); - void sprs_col_index(); - void sprs_swap_line(int L0, int L1); - void sprsludcmp(Mat_DP &a, Vec_O_INT &indx, DP &d); - void lubksb_blck(Mat_I_DP &a, Vec_I_INT &indx, Vec_IO_DP &b, const int periods); - void lubksb_blck_trp(Mat_I_DP &a, Vec_I_INT &indx, Vec_IO_DP &b, const int periods); - void lubksb(Mat_I_DP &a, Vec_I_INT &indx, Vec_IO_DP &b); - void lubksb_trp(Mat_I_DP &a, Vec_I_INT &indx, Vec_IO_DP &b); - void sprs_LU(Vec_O_DP &sL, Vec_O_INT &ijL, Vec_O_DP &sU, Vec_O_INT &ijU); - void Initialize(string filename_arg, double res1_arg, double res2_arg, double max_res_arg, double slowc_arg, double *ya_arg, double *direction_arg, int iter_arg); -}; - - -#endif diff --git a/mex/sources/simulate/simulate.cc b/mex/sources/simulate/simulate.cc index 931f16ef3..d176900f6 100644 --- a/mex/sources/simulate/simulate.cc +++ b/mex/sources/simulate/simulate.cc @@ -28,7 +28,6 @@ #include "simulate.hh" #include "Interpreter.hh" #include "Mem_Mngr.hh" -#include "linbcg.hh"