/* __bt Bogdanov-Takens Curve September 30, 1997 */ #include "common.h" #include "corefunc.h" #include "stagen.h" #include "_util.h" #include "_linalg.h" #include "__bt.h" #include "_conti.h" /* continuer's (i.e. generator's) data */ /* Numbers of visible name classes; the order must be the same as in /names section */ #define CL_TIME 1 #define CL_PHASE 2 #define CL_PAR 3 #define CL_EIGV 4 #define CL_TEST 5 #define CL_UTEST 6 #define _eps staData->eps #define _dx staData->dx Local(StagenDataPtr) stagenData=NULL; /* global copy of Starter's parameter passed by content */ Local(__bt_DataPtr) staData=NULL; /* global copy of our own data area */ Local(StdLinAlgDataPtr) staFunc=NULL; /* global copy of pointer standard linear algebra struct */ Local(UtilitiesDataPtr) staUtil=NULL; /* global copy of pointer to utility functions struct */ Local(Int2Ptr) active=NULL; /* index of active parameter(s) */ Local(Int2) nappend; /* number of appended user functions */ Local(Int2) npar,nphase; /* dims of par and phase spaces */ Local(Vector) x0=NULL; /* approximate first point */ Local(Vector) x1=NULL; /* current point */ Local(Vector) v0=NULL; /* approximate tangent vector at first point */ Local(FloatHiPtr) X=NULL; /* current set of phases used by Decoder */ Local(FloatHiPtr) P=NULL; /* current set of params used by Decoder */ Local(FloatHiPtr) E=NULL; /* current set of eigenvalues */ Local(VAR) zero=0; #include "_symdiff.c" /* Working space for defining and test functions */ Local(Matrix) A,B,C,C1,C2,AA,BB,BP,BT,BP1,BT1,BP2,BT2,D,DE,G; Local(Vector) v1,vn,xp,xp1,V,W,F,Vp,L,VV,VB1,VB2,V1,W0,W1,Q,PP1,PP2; Local(Vector) TV,K1,K2,K3,OK1,OK2,V12,V22,W12,W22,PIVOT,RL,OLD; Local(Vector) Re=NULL,Im=NULL; Local(VAR) test[4],dx,ddx; #define BifVector stagenData->BifDataOutPtr #define ActiveUTestFuns staData->ActiveUserTest #define nUTest stagenData->udFuncNum /* Initialize function common for Starter and Decoder */ Local(Int2) Init(StagenDataPtr stagenptr) { Int2 i,m; /* Set local pointers to data area and functions array */ stagenData=stagenptr; staData=(__bt_DataPtr)stagenptr->StaPtr; /* Use standard linear algebra */ staFunc=(StdLinAlgDataPtr)stagenptr->StdLinAlg; staUtil=(UtilitiesDataPtr)stagenptr->Utilities; /* Get dimensions of Phase and Par spaces */ npar=*stagenptr->ClassDim[CL_PAR]; nphase=*stagenptr->ClassDim[CL_PHASE]; /* Call udf SetInitPoint function, if any */ if (!stagenData->ContDataGenLen && FuncParActive(staData->SetInitPoint)) { /* there is UDF to set InitPoint */ Int2 Index_X,Index_P; Index_X=stagenptr->ParIndex(staData,X); Index_P=stagenptr->ParIndex(staData,P); FuncParCall(staData->SetInitPoint)(staData); stagenptr->UpdatePar(Index_X); stagenptr->UpdatePar(Index_P); } /* Do all the checks related to active parameters and find them */ for (i=0,nappend=0; iCheckParameters(staData->Active,npar,active,3+nappend)) { active=MemFree(active); return 1; } /* Checks related to analytical/numerical differentiation of RHS */ if (!stagenData->Der1 && staData->dx <= 0) { staUtil->ErrMessage("Analytical Jacoby matrix is undefined.\nRecompile system or set positive 'Increment'"); active=MemFree(active); return 2; } if (!stagenData->Der2 && staData->dx <= 0) { staUtil->ErrMessage("Analytical Hessian matrix is undefined.\nRecompile system or set positive 'Increment'"); active=MemFree(active); return 3; } /* Create a copy parameters */ m=nphase*(nphase-1)/2; if (!P) { P=MemNew(sizeof(FloatHi)*npar); MemCopy(P,staData->P,sizeof(FloatHi)*npar); /* Allocate memory for copy of phase vars used by Decoder */ X=MemNew(sizeof(FloatHi)*nphase); /* Allocate memory for eigenvalues */ E=MemNew(sizeof(FloatHi)*2*nphase); Re=staFunc->CreateVector(nphase); Im=staFunc->CreateVector(nphase); /* Allocate memory for working space used in defining and test functions */ A=staFunc->CreateMatrix(nphase,nphase); B=staFunc->CreateMatrix(nphase,nphase); BP=staFunc->CreateMatrix(nphase+1,nphase+1); BT=staFunc->CreateMatrix(nphase+1,nphase+1); V1=staFunc->CreateVector(nphase); W0=staFunc->CreateVector(nphase); W1=staFunc->CreateVector(nphase); VB1=staFunc->CreateVector(nphase+1); VB2=staFunc->CreateVector(nphase+1); VV=staFunc->CreateVector(2*nphase); Q=staFunc->CreateVector(nphase); PP1=staFunc->CreateVector(nphase+1); PP2=staFunc->CreateVector(nphase+1); TV=staFunc->CreateVector(nphase+3); AA=staFunc->CreateMatrix(nphase,nphase); BB=staFunc->CreateMatrix(2*nphase,2*nphase); C=staFunc->CreateMatrix(nphase+nappend,nphase+3+nappend); C1=staFunc->CreateMatrix(nphase,nphase+3+nappend); C2=staFunc->CreateMatrix(2,nphase+3+nappend); x0=staFunc->CreateVector(nphase+3+nappend); x1=staFunc->CreateVector(nphase+3+nappend); v0=staFunc->CreateVector(nphase+3+nappend); v1=staFunc->CreateVector(2*nphase); vn=staFunc->CreateVector(nphase); xp=staFunc->CreateVector(nphase+3+nappend); xp1=staFunc->CreateVector(nphase+3+nappend); V=staFunc->CreateVector(nphase); W=staFunc->CreateVector(nphase); F=staFunc->CreateVector(nphase); L=staFunc->CreateVector(nphase); Vp=staFunc->CreateVector(nphase+3+nappend); BifVector=MemNew(sizeof(FloatHi)*(4*nphase+2)); D=staFunc->CreateMatrix(nphase+2+nappend,nphase+3+nappend); DE=staFunc->CreateMatrix(nphase+3+nappend,nphase+3+nappend); if (nphase >= 4 && staData->ActiveSing[3]) { /* Hopf-BT */ BP1=staFunc->CreateMatrix(m+1,m+1); BT1=staFunc->CreateMatrix(m+1,m+1); BP2=staFunc->CreateMatrix(m+2,m+2); BT2=staFunc->CreateMatrix(m+2,m+2); G=staFunc->CreateMatrix(m,m); K1=staFunc->CreateVector(m+2); K2=staFunc->CreateVector(m+2); K3=staFunc->CreateVector(m+2); OK1=staFunc->CreateVector(m+2); OK2=staFunc->CreateVector(m+2); V12=staFunc->CreateVector(m+2); V22=staFunc->CreateVector(m+2); W12=staFunc->CreateVector(m+2); W22=staFunc->CreateVector(m+2); PIVOT=staFunc->CreateVector(m); RL=staFunc->CreateVector(m+1); OLD=staFunc->CreateVector(m+1); } } Der2num=ind2_(nphase+npar-1,nphase+npar-1)+1; Der3num=ind3_(nphase+npar-1,nphase+npar-1,nphase+npar-1)+1; dx=_dx; ddx=pow(dx,3.0/4.0); ((ContDataPtr)stagenptr->GenPtr)->ActiveSing=staData->ActiveSing; ((ContDataPtr)stagenptr->GenPtr)->ActiveUserTest=staData->ActiveUserTest; ((ContDataPtr)stagenptr->GenPtr)->TypeTestFuns=staData->TypeTestFunc; ((ContDataPtr)stagenptr->GenPtr)->ZeroTestFuns=staData->ZeroTestFunc; return 0; } Local(Int2) JacRHS (Vector x, Matrix J); Local(void) Term (Boolean OK) { Int2 m; m=nphase*(nphase-1)/2; stagenData->BifDataOutLen=0; /* saving global vectors V1,W1 used in defining functions */ if (OK&&P&&(stagenData->Reason==RF_CLEAN)) { stagenData->ContDataStaLen=2*nphase*sizeof(FloatHi); stagenData->ContDataStaPtr=MemNew(stagenData->ContDataStaLen); MemCopy(stagenData->ContDataStaPtr,V1,(nphase)*sizeof(FloatHi)); MemCopy(stagenData->ContDataStaPtr+(nphase),W1,(nphase)*sizeof(FloatHi)); stagenData->BifDataOutLen=0; stagenData->ProcessPoint(stagenData->ProcFuncNum, x1, "Last point"); } else { stagenData->ContDataStaLen=0; stagenData->BifDataOutLen=0; } x0=staFunc->DestroyVector(x0); x1=staFunc->DestroyVector(x1); v0=staFunc->DestroyVector(v0); active=MemFree(active); if (P) { P=MemFree(P); X=MemFree(X); E=MemFree(E); Re=staFunc->DestroyVector(Re); Im=staFunc->DestroyVector(Im); A=staFunc->DestroyMatrix(A); B=staFunc->DestroyMatrix(B); BP=staFunc->DestroyMatrix(BP); BT=staFunc->DestroyMatrix(BT); V1=staFunc->DestroyVector(V1); W0=staFunc->DestroyVector(W0); W1=staFunc->DestroyVector(W1); Q=staFunc->DestroyVector(Q); PP1=staFunc->DestroyVector(PP1); PP2=staFunc->DestroyVector(PP2); TV=staFunc->DestroyVector(TV); AA=staFunc->DestroyMatrix(AA); BB=staFunc->DestroyMatrix(BB); VV=staFunc->DestroyVector(VV); VB1=staFunc->DestroyVector(VB1); VB2=staFunc->DestroyVector(VB2); C=staFunc->DestroyMatrix(C); C1=staFunc->DestroyMatrix(C1); C2=staFunc->DestroyMatrix(C2); vn=staFunc->DestroyVector(vn); xp=staFunc->DestroyVector(xp); xp1=staFunc->DestroyVector(xp1); v1=staFunc->DestroyVector(v1); V=staFunc->DestroyVector(V); W=staFunc->DestroyVector(W); F=staFunc->DestroyVector(F); Vp=staFunc->DestroyVector(Vp); L=staFunc->DestroyVector(L); BifVector=MemFree(BifVector); D=staFunc->DestroyMatrix(D); DE=staFunc->DestroyMatrix(DE); if (nphase >= 4 && staData->ActiveSing[3]) { BP1=staFunc->DestroyMatrix(BP1); BT1=staFunc->DestroyMatrix(BT1); BP2=staFunc->DestroyMatrix(BP2); BT2=staFunc->DestroyMatrix(BT2); G=staFunc->DestroyMatrix(G); K1=staFunc->DestroyVector(K1); K2=staFunc->DestroyVector(K2); K3=staFunc->DestroyVector(K3); OK1=staFunc->DestroyVector(OK1); OK2=staFunc->DestroyVector(OK2); V12=staFunc->DestroyVector(V12); V22=staFunc->DestroyVector(V22); W12=staFunc->DestroyVector(W12); W22=staFunc->DestroyVector(W22); PIVOT=staFunc->DestroyVector(PIVOT); RL=staFunc->DestroyVector(RL); OLD=staFunc->DestroyVector(OLD); } } } Entry(Int2) bt_JacDefBogdanov (Vector x, Matrix J); Entry(Int2) bt_DefBogdanov (Vector x, Vector J); /************/ /* Starters */ /* bt -> bt Starter */ Entry(Int2) bt_Starter(StagenDataPtr stagenptr) { Int2 i,j,k,kp1,Ret,m,p,q,r,s; Vector v1,v2; Matrix D,DE; VAR max1,max2, alpha, beta, t; if (!stagenptr) { /* this is request to terminate */ Term(TRUE); return 0; } if (Init(stagenptr)) return 1; /* initialize local variables */ m=nphase*(nphase-1)/2; /* Create the first point for generator */ if (stagenptr->ContDataGenLen) { /* "Extention" */ MemCopy(V1,stagenptr->ContDataStaPtr,sizeof(FloatHi)*(nphase)); MemCopy(W1,stagenptr->ContDataStaPtr+(nphase),sizeof(FloatHi)*(nphase)); MemFree(stagenptr->ContDataStaPtr); stagenptr->ContDataStaLen=0; MemCopy(x0,staData->X,sizeof(FloatHi)*nphase); for (i=0; i<3+nappend; i++) x0[nphase+i]=staData->P[active[i]]; } else { v1=staFunc->CreateVector(nphase+3+nappend); v2=staFunc->CreateVector(nphase+3+nappend); D=staFunc->CreateMatrix(nphase+2+nappend,nphase+3+nappend); DE=staFunc->CreateMatrix(nphase+3+nappend,nphase+3+nappend); MemCopy(x0,staData->X,sizeof(FloatHi)*nphase); for (i=0; i<3+nappend; i++) x0[nphase+i]=staData->P[active[i]]; JacRHS(x0,C1); staFunc->CopyMatrixBlock(C1,0,0,A,0,0,nphase,nphase); staFunc->ClearMatrix(BP); staFunc->CopyMatrixBlock(A,0,0,BP,0,0,nphase,nphase); for (i=0; i t) { p = i; q = j; t = fabs(BP[i][j]); } } } if (k!=q) { /* switch colom k with colom q */ for (i=0;i<=nphase;i++) { t=BP[i][k]; BP[i][k]=BP[i][q]; BP[i][q]=t; } } if (k!=p) { /* switch row k with row p */ for (i=0;i<=nphase;i++) { t=BP[k][i]; BP[k][i]=BP[p][i]; BP[p][i]=t; } } if (fabs(BP[k][k]) == 0.0) { staUtil->ErrMessage("Matrix has rank defect at least two"); Term(FALSE); break; } kp1=k+1; for (i=kp1;i=1) */ if (stagenData->BifDataInOk) { MemCopy(V1,stagenData->BifDataInPtr,sizeof(FloatHi)*nphase); staFunc->NormalizeVector(V1); } else { staFunc->ClearVector(V1); V1[p]=1.0; } staFunc->ClearVector(W1); W1[q]=1.0; /* perform one adaptation step for V1 and W1 */ staFunc->ClearMatrix(BP); staFunc->CopyMatrixBlock(C1,0,0,BP,0,0,nphase,nphase); for (i=0; iCopyMatrix(BP,BT); Ret=staFunc->Decomp(BT); if (Ret != 0) { goto final; } staFunc->ClearVector(VB1); VB1[nphase]=1.0; staFunc->Solve(BT,VB1); staFunc->CopyVectorElements(VB1,0,V1,0,nphase); staFunc->NormalizeVector(V1); staFunc->TransposeMatrix(BP,BT); Ret=staFunc->Decomp(BT); if (Ret != 0) { goto final; } staFunc->ClearVector(PP1); PP1[nphase]=1.0; staFunc->Solve(BT,PP1); staFunc->CopyVectorElements(PP1,0,W1,0,nphase); staFunc->NormalizeVector(W1); final: if (Ret) { v1=staFunc->DestroyVector(v1); v2=staFunc->DestroyVector(v2); D=staFunc->DestroyMatrix(D); DE=staFunc->DestroyMatrix(DE); Term(FALSE); return 1; } /* Compute a tangent vector to the BT curve */ bt_DefBogdanov(x0,v0); Ret=bt_JacDefBogdanov(x0,D); for (i=0; iAppendMatrixVector(D,v1,DE); Ret=staFunc->Decomp(DE); if (Ret == 0) { v2[nphase+2+nappend]=stagenData->Forward ? 1 : -1; staFunc->Solve(DE,v2); staFunc->CopyVector(v2,v0); break; }; v1[i]=0.0; }; if (Ret) { staUtil->ErrMessage ("Unable to find a tangent vector to the Bogdanov-Takens curve"); v1=staFunc->DestroyVector(v1); v2=staFunc->DestroyVector(v2); D=staFunc->DestroyMatrix(D); DE=staFunc->DestroyMatrix(DE); Term(FALSE); return 1; } staFunc->NormalizeVector(v0); v1=staFunc->DestroyVector(v1); v2=staFunc->DestroyVector(v2); D=staFunc->DestroyMatrix(D); DE=staFunc->DestroyMatrix(DE); } if (nphase >= 4 && staData->ActiveSing[3]) { if (JacRHS(x0,C1)) return 1; staFunc->CopyMatrixBlock(C1,0,0,A,0,0,nphase,nphase); staFunc->ClearMatrix(BP2); i=-1; for (p=1;pClearVector(V12); staFunc->ClearVector(V22); V12[m-2]=1.0; V22[m-1]=1.0; /* searching two rows with maximal pivots */ staFunc->ClearMatrix(G); staFunc->CopyMatrixBlock(BP2,0,0,G,0,0,m,m); staFunc->ClearVector(PIVOT); PIVOT[m-1]=1.0; for (k=0;k<=m-2;k++) { kp1=k+1; p=k; for (i=kp1;i<=m-1;i++) { if (fabs(G[i][k]) > fabs(G[p][k])) p=i; } PIVOT[k]=p; if (p!=k) PIVOT[m-1]=-PIVOT[m-1]; t=G[p][k]; G[p][k]=G[k][k]; G[k][k]=t; if (t==0.0) continue; for (i=kp1;i<=m-1;i++) G[i][k]=-G[i][k]/t; for (j=kp1;j<=m-1;j++) { t=G[p][j]; G[p][j]=G[k][j]; G[k][j]=t; if (t != 0.0) for (i=kp1;i<=m-1;i++) G[i][j] += G[i][k]*t; } } staFunc->ClearVector(W12); for (i=0;iClearVector(W12); staFunc->ClearVector(W22); W12[p]=1.0; W22[q]=1.0; /* second bordering to prepare for g22 as test function H-BT */ staFunc->ClearVector(K1); staFunc->ClearVector(K2); staFunc->CopyMatrix(BP2,BT2); K1[m]=1.0; K2[m+1]=1.0; staFunc->Decomp(BT2); staFunc->Solve(BT2,K1); staFunc->Solve(BT2,K2); staFunc->CopyVector(K1,OK1); staFunc->CopyVector(K1,K3); staFunc->CopyVector(K2,OK2); max1=(fabs(OK1[m])>fabs(OK2[m]))?fabs(OK1[m]):fabs(OK2[m]); max2=(fabs(OK1[m+1])>fabs(OK2[m+1]))?fabs(OK1[m+1]):fabs(OK2[m+1]); if (max1>max2) { alpha=OK1[m]; beta=OK2[m]; } else { alpha=OK1[m+1]; beta=OK2[m+1]; } staFunc->MultiplyVectorScalar(OK1,-beta,OK1); staFunc->AddScaledVector(OK1,OK2,alpha,K1); staFunc->NormalizeVector(K1); alpha=staFunc->ScalProd(V12,K1); if (alpha<0) { for (i=0;iCopyVector(K1,V12); staFunc->TransposeMatrix(BP2,BT2); staFunc->ClearVector(K1); staFunc->ClearVector(K2); K1[m]=1.0; K2[m+1]=1.0; staFunc->Decomp(BT2); staFunc->Solve(BT2,K1); staFunc->Solve(BT2,K2); staFunc->CopyVector(K1,OK1); staFunc->CopyVector(K1,K3); staFunc->CopyVector(K2,OK2); max1=(fabs(OK1[m])>fabs(OK2[m]))?fabs(OK1[m]):fabs(OK2[m]); max2=(fabs(OK1[m+1])>fabs(OK2[m+1]))?fabs(OK1[m+1]):fabs(OK2[m+1]); if (max1>max2) { alpha=OK1[m]; beta=OK2[m]; } else { alpha=OK1[m+1]; beta=OK2[m+1]; } staFunc->MultiplyVectorScalar(OK1,-beta,OK1); staFunc->AddScaledVector(OK1,OK2,alpha,K1); staFunc->NormalizeVector(K1); alpha=staFunc->ScalProd(W12,K1); if (alpha<0) { for (i=0;iCopyVector(K1,W12); /* adapt V22 */ staFunc->ClearMatrix(BP1); staFunc->CopyMatrixBlock(BP2,0,0,BP1,0,0,m,m); for (i=0;iCopyVectorElements(W22,0,RL,0,m+1); Ret=staFunc->Decomp(BP1); if (Ret != 0) { return 1; } staFunc->Solve(BP1,RL); staFunc->NormalizeVector(RL); staFunc->CopyVectorElements(V22,0,OLD,0,m+1); alpha=staFunc->ScalProd(OLD,RL); if (alpha < 0) { for (i=0;iClearVector(V22); staFunc->CopyVectorElements(RL,0,V22,0,m+1); /* adapt W22 */ staFunc->ClearMatrix(BP1); staFunc->CopyMatrixBlock(BP2,0,0,BP1,0,0,m,m); for (i=0;iTransposeMatrix(BP1,BT1); staFunc->ClearVector(RL); staFunc->CopyVectorElements(OLD,0,RL,0,m+1); Ret=staFunc->Decomp(BT1); if (Ret != 0) { return 1; } staFunc->Solve(BT1,RL); staFunc->NormalizeVector(RL); staFunc->CopyVectorElements(W22,0,OLD,0,m+1); alpha=staFunc->ScalProd(OLD,RL); if (alpha<0) { for (i=0;iClearVector(W22); staFunc->CopyVectorElements(RL,0,W22,0,m+1); /* border biproduct matrix with V12,V22,W12 and W22 */ for (i=0;iGenPtr)->X0=x0; ((ContDataPtr)stagenptr->GenPtr)->V0=v0; ((ContDataPtr)stagenptr->GenPtr)->ActiveSing=staData->ActiveSing; ((ContDataPtr)stagenptr->GenPtr)->ActiveUserTest=staData->ActiveUserTest; ((ContDataPtr)stagenptr->GenPtr)->TypeTestFuns=staData->TypeTestFunc; ((ContDataPtr)stagenptr->GenPtr)->ZeroTestFuns=staData->ZeroTestFunc; /* That's all */ return 0; } typedef EntryPtr(Int2,TestFuncPtr,(Vector xx, Vector vv, FloatHiPtr TV)); typedef EntryPtr(Int2,ProcFuncPtr,(Int2 Ind, Vector xx, Vector vv, CharPtr *msg)); /************************/ /* Some common routines */ Local(void) CopyToX(Vector vp) { MemCopy(X,vp,sizeof(FloatHi)*nphase); } Local(void) CopyToP(Vector vp) { Int2 i; for (i=0; i<3+nappend; i++) P[active[i]]=vp[nphase+i]; } Local(void) CopyToE(Vector Re, Vector Im) { MemCopy(E,Re,sizeof(FloatHi)*nphase); MemCopy(E+nphase,Im,sizeof(FloatHi)*nphase); } /***********/ /* Decoder */ Entry(Int2) bt_Decoder(VoidPtr vpoint, FloatHiPtr PNTR indirect, Int2 oper) { switch (oper) { case DECODER_INIT: /* point is a pointer to StagenData structure */ case DECODER_TERM: break; case DECODER_DIM: /* returns number of M-points in given G-point */ return 1; default: /* extract next M-point from G-point pointed by vpoint */ /* vpoint is a Vector */ indirect[CL_TIME]=&zero; indirect[CL_PHASE]=X; CopyToX(vpoint); indirect[CL_PAR]=P; CopyToP(vpoint); indirect[CL_EIGV]=E; CopyToE(Re,Im); indirect[CL_TEST]=test; } return 0; } /***************************/ /* User-defined functions */ Entry(Int2) bt_DefUser (Vector x, Vector y) { Int2 i,j; for (i=j=0; iudFunc)(x,i,&y[j]); j++; } return 0; } /*******/ /* RHS */ Entry(Int2) bt_DefRHS (Vector x, Vector y) { CopyToX(x); CopyToP(x); stagenData->Rhs(X,P,&zero,y); return 0; } /*************************************/ /* Defining function for equilibrium */ Local(Int2) DefEquilib (Vector x, Vector y) { bt_DefRHS(x,y); if (nappend) bt_DefUser(x,y+nphase); return 0; } /********************/ /* Jacobian of RHS */ Local(Int2) JacRHS (Vector x, Matrix Jac) { staFunc->CopyVectorElements(x,0,xp,0,nphase+3+nappend); if (stagenData->Der1) { SymJac(xp,Jac); } else { if (staFunc->Der(bt_DefRHS, nphase, xp, dx, Jac)) return 1; } return 0; } /***************************************************/ /* Jacobian of defining conditions for equilibria */ Local(Int2) JacDefEquilib (Vector x, Matrix Jac) { staFunc->ClearMatrix(Jac); JacRHS(x, Jac); if (nappend) staFunc->Der(bt_DefUser, nappend, x, dx, Jac+nphase); return 0; } /***********************************/ /* Bogdanov-Takens defining conditions */ Entry(Int2) bt_DefBord (Vector x, Vector y) { Int2 i,j,m,p,q,r,s,Ret; if (JacRHS(x,C1)) return 1; /* A,C1,BP,V1,W1,Q - global */ /* creation of biproduct matrix BP2 */ if (nphase >= 4 && staData->ActiveSing[3]) { m=nphase*(nphase-1)/2; staFunc->CopyMatrixBlock(C1,0,0,A,0,0,nphase,nphase); staFunc->ClearMatrix(BP2); i=-1; for (p=1;pClearMatrix(BP); staFunc->CopyMatrixBlock(C1,0,0,BP,0,0,nphase,nphase); /* global V1 and W1 are assumed to be correct for bordering */ for (i=0; iCopyMatrix(BP,BT); Ret=staFunc->Decomp(BT); if (Ret != 0) { return 1; } staFunc->ClearVector(VB1); VB1[nphase]=1.0; staFunc->Solve(BT,VB1); staFunc->CopyVectorElements(VB1,0,Q,0,nphase); /* make global null-vector AQ=0 */ y[0]=VB1[nphase]; VB1[nphase]=0.0; staFunc->Solve(BT,VB1); /* make global generalized vector A(VB1)=Q */ y[1]=VB1[nphase]; return 0; } /***************************************/ /* Jacobian of BT defining condition */ Local(Int2) JacDefBord (Vector x, Matrix Jac) { int i,j,l; VAR S,T,U; staFunc->ClearMatrix(Jac); if (stagenData->Der2) { /* creation of DefBord Jacobian matrix Jac using symbolic second derivatives */ CopyToX(x); CopyToP(x); H=stagenData->Der2(X,P,&zero); /* take global vectors Q and VB1 generated at the computation of DefBord */ /* compute PP1 using the transposed bordered bi-product */ staFunc->TransposeMatrix(BP,BT); staFunc->Decomp(BT); staFunc->ClearVector(PP1); PP1[nphase]=1.0; staFunc->Solve(BT,PP1); staFunc->ClearVector(PP2); staFunc->CopyVectorElements(PP1,0,PP2,0,nphase); staFunc->Solve(BT,PP2); staFunc->CopyMatrix(BP,BT); staFunc->Decomp(BT); /* compute gradient of DefBord */ for (l=0; lDer(bt_DefBord, 2, x, dx, Jac)) return 1; } return(0); } /*************************************/ /* Defining functions for BT curve */ Entry(Int2) bt_DefBogdanov (Vector x, Vector y) { if (DefEquilib(x,y)) return 1; if (bt_DefBord(x,y+nphase+nappend)) return 2; return 0; } /*************************************************/ /* Jacobian of defining functions for BT curve */ Entry(Int2) bt_JacDefBogdanov (Vector x, Matrix Jac) { staFunc->ClearMatrix(Jac); if (JacDefEquilib(x,C)) return 1; staFunc->CopyMatrixBlock(C,0,0,Jac,0,0,nphase+nappend,nphase+3+nappend); if (JacDefBord(x,C2)) return 2; staFunc->CopyMatrixBlock(C2,0,0,Jac,nphase+nappend,0,2,nphase+3+nappend); return 0; } /*********************/ /* Default processor */ Entry(Int2) bt_ProcDefault(Int2 Ind, Vector x, Vector v1, CharPtr *msg) { bt_DefBord (x, xp); /* update of global Q and VB1 */ /* Assert: v1==NULL, msg==NULL, Ind==0 */ MemCopy(x1,x,sizeof(FloatHi)*(nphase+3+nappend)); /* for the last point */ if (!staData->eigcomp) return 0; if (JacRHS(x,C1)) return (1); staFunc->CopyMatrixBlock(C1,0,0,A,0,0,nphase,nphase); staFunc->EigenValues(A,Re,Im); return 0; } /*********************************/ /* Test and processing functions */ Local(Char) buf[80]; /* Test functions */ Entry(Int2) bt_Test3Z(Vector x, Vector v, FloatHiPtr res) { *res=1.0; if (nphase < 3) goto end; VB1[nphase]=0.0; staFunc->MultiplyVectorScalar(VB1,2.0,VB2); staFunc->Solve(BT,VB2); *res=VB2[nphase]; end: test[0]=*res; return 0; } Entry(Int2) bt_Proc3Z(Int2 Ind, Vector x, Vector v, CharPtr *msg) { if (Ind==0) { sprintf(buf,"Triple zero eigenvalue"); } else { sprintf(buf,"Triple zero eigenvalue (?)"); } *msg=buf; return 0; } Entry(Int2) bt_TestZA(Vector x, Vector v, FloatHiPtr res) { Int2 i,j,k; double b20,f3,f4,f5,d; *res=1.0; if (nphase < 2) goto end; /* take global (generalized) null-eigenvectors and normalize them: Q and VB1 are assumed to be correct */ staFunc->CopyVector(Q,V); d=staFunc->ScalProd(V,V); staFunc->NormalizeVector(V); /* null-vector V (A*V=0, =1) */ staFunc->CopyVectorElements(VB1,0,W,0,nphase); staFunc->MultiplyVectorScalar(W,1.0/sqrt(d),W); staFunc->AddScaledVector(W,V,-staFunc->ScalProd(V,W),W); /* generalized null-vector W (A*W=V, =0) */ /* compute the adjoint eigenvectors */ staFunc->CopyMatrixBlock(C1,0,0,A,0,0,nphase,nphase); staFunc->TransposeMatrix(A,B); staFunc->ClearMatrix(BB); staFunc->CopyMatrixBlock(B,0,0,BB,0,0,nphase,nphase); staFunc->CopyMatrixBlock(B,0,0,BB,nphase,nphase,nphase,nphase); for (i=0; iSngSolve(BB,_eps,v1); if (i != 2*nphase-2) { *res=777; return 1; } staFunc->CopyVectorElements(v1,0,W1,0,nphase); /* adjoint null-vector W1 */ staFunc->CopyVectorElements(v1,nphase,W0,0,nphase); /* adjoint generalized null-vector W0 */ d=staFunc->ScalProd(V,W0); staFunc->MultiplyVectorScalar(W1,1.0/d,W1); staFunc->AddScaledVector(W0,W1,-staFunc->ScalProd(W0,W),W0); staFunc->MultiplyVectorScalar(W0,1.0/d,W0); /* compute the coefficient a in the normal form : x'=y; y'=ax^2+bxy */ /* a=b20= */ if (stagenData->Der2) { CopyToX(x); CopyToP(x); H=stagenData->Der2(X,P,&zero); /* The 1st elem must be time! */ for (b20=0,i=0; iClearVector(v0); /* b20= */ staFunc->CopyVectorElements(V,0,v0,0,nphase); bt_DefRHS(x,F); f3=staFunc->ScalProd(W1,F); staFunc->AddScaledVector(x,v0,ddx,x1); bt_DefRHS(x1,F); f4=staFunc->ScalProd(W1,F); staFunc->AddScaledVector(x,v0,-ddx,x1); bt_DefRHS(x1,F); f5=staFunc->ScalProd(W1,F); b20=(f4-2*f3+f5)/ddx/ddx; } *res=b20/2; end: test[1]=*res; return 0; } Entry(Int2) bt_ProcZA(Int2 Ind, Vector x, Vector v, CharPtr *msg) { if (Ind==0) { sprintf(buf,"Degenerate BT: triple equilibrium"); } else { sprintf(buf,"Degenerate BT: triple equilibrium (?)"); } *msg=buf; return 0; } Entry(Int2) bt_TestZB(Vector x, Vector v, FloatHiPtr res) { Int2 i,j,k; double a20,b11,f0,f1,f2,d; *res=1.0; if (nphase < 2) goto end; /* take global (generalized) null-eigenvectors and normalize them */ staFunc->CopyVector(Q,V); d=staFunc->ScalProd(V,V); staFunc->NormalizeVector(V); /* null-vector V (A*V=0, =1) */ staFunc->CopyVectorElements(VB1,0,W,0,nphase); staFunc->MultiplyVectorScalar(W,1.0/sqrt(d),W); staFunc->AddScaledVector(W,V,-staFunc->ScalProd(V,W),W); /* generalized null-vector W (A*W=V, =0) */ /* compute the adjoint eigenvectors */ staFunc->CopyMatrixBlock(C1,0,0,A,0,0,nphase,nphase); staFunc->TransposeMatrix(A,B); staFunc->ClearMatrix(BB); staFunc->CopyMatrixBlock(B,0,0,BB,0,0,nphase,nphase); staFunc->CopyMatrixBlock(B,0,0,BB,nphase,nphase,nphase,nphase); for (i=0; iSngSolve(BB,_eps,v1); if (i != 2*nphase-2) { *res=777; return 1; } staFunc->CopyVectorElements(v1,0,W1,0,nphase); /* adjoint null-vector W1 */ staFunc->CopyVectorElements(v1,nphase,W0,0,nphase); /* adjoint generalized null-vector W0 */ d=staFunc->ScalProd(V,W0); staFunc->MultiplyVectorScalar(W1,1.0/d,W1); staFunc->AddScaledVector(W0,W1,-staFunc->ScalProd(W0,W),W0); staFunc->MultiplyVectorScalar(W0,1.0/d,W0); /* compute the coefficients of the normal form : x'=y; y'=ax^2+bxy */ /* a20=, b11= */ if (stagenData->Der2) { CopyToX(x); CopyToP(x); H=stagenData->Der2(X,P,&zero); /* The 1st elem must be time! */ for (a20=0,b11=0,i=0; iClearVector(v0); /* a20= */ staFunc->CopyVectorElements(V,0,v0,0,nphase); bt_DefRHS(x,F); f0=staFunc->ScalProd(W0,F); staFunc->AddScaledVector(x,v0,ddx,x1); bt_DefRHS(x1,F); f1=staFunc->ScalProd(W0,F); staFunc->AddScaledVector(x,v0,-ddx,x1); bt_DefRHS(x1,F); f2=staFunc->ScalProd(W0,F); a20=(f1-2*f0+f2)/ddx/ddx; /* b11= */ staFunc->AddScaledVector(V,W,1.0,F); staFunc->CopyVectorElements(F,0,v0,0,nphase); staFunc->AddScaledVector(x,v0,0.5*ddx,x1); bt_DefRHS(x1,F); b11=staFunc->ScalProd(W1,F); staFunc->AddScaledVector(x,v0,-0.5*ddx,x1); bt_DefRHS(x1,F); b11+=staFunc->ScalProd(W1,F); staFunc->AddScaledVector(V,W,-1.0,F); staFunc->CopyVectorElements(F,0,v0,0,nphase); staFunc->AddScaledVector(x,v0,0.5*ddx,x1); bt_DefRHS(x1,F); b11-=staFunc->ScalProd(W1,F); staFunc->AddScaledVector(x,v0,-0.5*ddx,x1); bt_DefRHS(x1,F); b11-=staFunc->ScalProd(W1,F); b11/=(ddx*ddx); } *res=a20+b11; end: test[2]=*res; return 0; } Entry(Int2) bt_ProcZB(Int2 Ind, Vector x, Vector v, CharPtr *msg) { if (Ind==0) { sprintf(buf,"Degenerate BT: double equilibrium"); } else { sprintf(buf,"Degenerate BT: double equilibrium (?)"); } *msg=buf; return 0; } Entry(Int2) bt_TestHBT(Vector x, Vector v, FloatHiPtr res) { int m; Int2 Ret; *res=1.0; if (nphase < 4) goto end; m=nphase*(nphase-1)/2; staFunc->ClearVector(K1); staFunc->ClearVector(K2); K1[m]=1.0; K2[m+1]=1.0; staFunc->CopyMatrix(BP2,BT2); Ret=staFunc->Decomp(BT2); if (Ret != 0) { return 1; } staFunc->Solve(BT2,K1); staFunc->Solve(BT2,K2); *res=K2[m+1]; /* g22 = 0 */ end: test[3]=*res; return 0; } Entry(Int2) bt_ProcHBT(Int2 Ind, Vector x, Vector v, CharPtr *msg) { if (Ind==0) { sprintf(buf,"Hopf-BT"); } else { sprintf(buf,"Hopf-BT (?)"); } *msg=buf; return 0; } /**************************************************/ /* Adaptation of the bordering vectors V1 and W1 */ Entry(Int2) bt_Adapter(Vector x, Vector v) { int i,m; Int2 Ret; VAR alpha,beta,max1,max2; m=nphase*(nphase-1)/2; /* global Q is assumed to be correct */ staFunc->CopyVector(Q,V1); staFunc->NormalizeVector(V1); /* global BP is assumed to be correct */ staFunc->TransposeMatrix(BP,BT); staFunc->Decomp(BT); staFunc->ClearVector(VB2); VB2[nphase]=1.0; staFunc->Solve(BT,VB2); staFunc->CopyVectorElements(VB2,0,W1,0,nphase); staFunc->NormalizeVector(W1); /* adapt test function */ if (nphase >= 4 && staData->ActiveSing[3]) { staFunc->ClearVector(K1); staFunc->ClearVector(K2); staFunc->CopyMatrix(BP2,BT2); K1[m]=1.0; K2[m+1]=1.0; staFunc->Decomp(BT2); staFunc->Solve(BT2,K1); staFunc->Solve(BT2,K2); staFunc->CopyVector(K1,OK1); staFunc->CopyVector(K1,K3); staFunc->CopyVector(K2,OK2); max1=(fabs(OK1[m])>fabs(OK2[m]))?fabs(OK1[m]):fabs(OK2[m]); max2=(fabs(OK1[m+1])>fabs(OK2[m+1]))?fabs(OK1[m+1]):fabs(OK2[m+1]); if (max1>max2) { alpha=OK1[m]; beta=OK2[m]; } else { alpha=OK1[m+1]; beta=OK2[m+1]; } staFunc->MultiplyVectorScalar(OK1,-beta,OK1); staFunc->AddScaledVector(OK1,OK2,alpha,K1); staFunc->NormalizeVector(K1); alpha=staFunc->ScalProd(V12,K1); if (alpha<0) { for (i=0;iCopyVector(K1,V12); staFunc->TransposeMatrix(BP2,BT2); staFunc->ClearVector(K1); staFunc->ClearVector(K2); K1[m]=1.0; K2[m+1]=1.0; staFunc->Decomp(BT2); staFunc->Solve(BT2,K1); staFunc->Solve(BT2,K2); staFunc->CopyVector(K1,OK1); staFunc->CopyVector(K1,K3); staFunc->CopyVector(K2,OK2); max1=(fabs(OK1[m])>fabs(OK2[m]))?fabs(OK1[m]):fabs(OK2[m]); max2=(fabs(OK1[m+1])>fabs(OK2[m+1]))?fabs(OK1[m+1]):fabs(OK2[m+1]); if (max1>max2) { alpha=OK1[m]; beta=OK2[m]; } else { alpha=OK1[m+1]; beta=OK2[m+1]; } staFunc->MultiplyVectorScalar(OK1,-beta,OK1); staFunc->AddScaledVector(OK1,OK2,alpha,K1); staFunc->NormalizeVector(K1); alpha=staFunc->ScalProd(W12,K1); if (alpha<0) { for (i=0;iCopyVector(K1,W12); /* adapt V22 */ staFunc->ClearMatrix(BP1); staFunc->CopyMatrixBlock(BP2,0,0,BP1,0,0,m,m); for (i=0;iCopyVectorElements(W22,0,RL,0,m+1); Ret=staFunc->Decomp(BP1); if (Ret != 0) { return 1; } staFunc->Solve(BP1,RL); staFunc->NormalizeVector(RL); staFunc->CopyVectorElements(V22,0,OLD,0,m+1); alpha=staFunc->ScalProd(OLD,RL); if (alpha < 0) { for (i=0;iClearVector(V22); staFunc->CopyVectorElements(RL,0,V22,0,m+1); /* adapt W22 */ staFunc->ClearMatrix(BP1); staFunc->CopyMatrixBlock(BP2,0,0,BP1,0,0,m,m); for (i=0;iTransposeMatrix(BP1,BT1); staFunc->ClearVector(RL); staFunc->CopyVectorElements(V22,0,RL,0,m+1); Ret=staFunc->Decomp(BT1); if (Ret != 0) { return 1; } staFunc->Solve(BT1,RL); staFunc->NormalizeVector(RL); staFunc->CopyVectorElements(W22,0,OLD,0,m+1); alpha=staFunc->ScalProd(OLD,RL); if (alpha<0) { for (i=0;iClearVector(W22); staFunc->CopyVectorElements(RL,0,W22,0,m+1); for (i=0;iCopyMatrix(BP2,BT2); Ret=staFunc->Decomp(BT2); if (Ret != 0) { return 1; } staFunc->ClearVector(K1); K1[m+1]=1.0; staFunc->Solve(BT2,K1); test[3]=K1[m+1]; } return 0; }