//! Annuity at time 0 double LmmVanillaSwapPricer::annuity0(const VanillaSwap& vanillaSwap, const std::vector<double>& liborsInitValue) const { assert(pLMMTenorStructure_->get_horizon()+1 == liborsInitValue.size()); assert(pLMMTenorStructure_->get_horizon() >= vanillaSwap.get_indexEnd()); // if not cannot price this swap; size_t horizon = pLMMTenorStructure_->get_horizon(); const double & fixedLegdelta_T = vanillaSwap.get_fixedLegTenorType().YearFraction(); const double & floatingLegdelta_T = vanillaSwap.get_floatingLegTenorType().YearFraction(); //! ZC[i] = P(T_0,T_i) std::vector<double> ZC(vanillaSwap.get_indexEnd()+1); ZC[0] = 1.0; for(size_t i=1; i<ZC.size(); ++i) { size_t indexLibor = i-1; //double deltaT = vanillaSwap.get_DeltaTFloatLeg(indexLibor); // YY bug double deltaT = floatingLegdelta_T; ZC[i] = ZC[i-1]/(1+deltaT*liborsInitValue[indexLibor]); } double price = 0.0; const std::vector<LMM::Index>& fixedLegPaymentIndexSchedule = vanillaSwap.get_fixedLegPaymentIndexSchedule(); for(size_t itr = 0; itr < fixedLegPaymentIndexSchedule.size(); ++itr) { size_t fixedLegPaymentIndex = fixedLegPaymentIndexSchedule[itr]; double delta_T = vanillaSwap.get_DeltaTFixedLeg(itr); //std::cout << "numeraire[indexValuationDate]/numeraire[fixedLegPaymentIndex] = " << numeraire[indexValuationDate]/numeraire[fixedLegPaymentIndex] << std::endl; price += delta_T*ZC[fixedLegPaymentIndex]; } return price; }
double LmmVanillaSwapPricer::swapRate_Analytical(const VanillaSwap& vanillaSwap, const std::vector<double>& liborsInitValue) const { assert(pLMMTenorStructure_->get_horizon()+1 == liborsInitValue.size()); assert(pLMMTenorStructure_->get_horizon() >= vanillaSwap.get_indexEnd()); // if not cannot price this swap; size_t horizon = pLMMTenorStructure_->get_horizon(); const double & fixedLegdelta_T = vanillaSwap.get_fixedLegTenorType().YearFraction(); const double & floatingLegdelta_T = vanillaSwap.get_floatingLegTenorType().YearFraction(); //! ZC[i] = P(T_0,T_i) std::vector<double> ZC(vanillaSwap.get_indexEnd()+1); ZC[0] = 1.0; for(size_t i=1; i<ZC.size(); ++i) { size_t indexLibor = i-1; //double deltaT = vanillaSwap.get_DeltaTFloatLeg(indexLibor); // YY bug double deltaT = floatingLegdelta_T; ZC[i] = ZC[i-1]/(1+deltaT*liborsInitValue[indexLibor]); } //! pvFloatingLeg: 1 - 1 double pvFloatingLegValue = ZC[vanillaSwap.get_indexStart()] - ZC[vanillaSwap.get_indexEnd()]; //! pvAnnuity double pvAnnuity = annuity0(vanillaSwap, liborsInitValue); //! swapRate return pvFloatingLegValue / pvAnnuity; }
double LmmVanillaSwapPricer::swapNPV_Analytical_2(const VanillaSwap& vanillaSwap, const std::vector<double>& liborsInitValue) const // initLibor[i] = L_i[T_0] { assert(pLMMTenorStructure_->get_horizon()+1 == liborsInitValue.size()); assert(pLMMTenorStructure_->get_horizon() >= vanillaSwap.get_indexEnd()); // if not cannot price this swap; size_t horizon = pLMMTenorStructure_->get_horizon(); const double & floatingLegdelta_T = vanillaSwap.get_floatingLegTenorType().YearFraction(); const double & fixedLegdelta_T = vanillaSwap.get_fixedLegTenorType().YearFraction(); //! ZC[i] = P(T_0,T_i) std::vector<double> ZC(horizon+2); ZC[0] = 1.0; for(size_t i=1; i<ZC.size(); ++i) { ZC[i] = ZC[i-1]/(1+floatingLegdelta_T*liborsInitValue[i-1]); } //! numeraire std::vector<double> numeraire(ZC.size() ); // determinisitc IR for(size_t i=0; i<ZC.size(); ++i) { numeraire[i] = 1.0/ZC[i]; } //! pvFloatingLeg: 1 - 1 const std::vector<LMM::Index>& floatingLegPaymentIndexSchedule = vanillaSwap.get_floatingLegPaymentIndexSchedule(); //size_t floatingLegTenorLmmTenorRatio = vanillaSwap.get_floatingLegTenorLmmTenorRatio(); LMM::Index indexFloatingLegStart = floatingLegPaymentIndexSchedule.front(); LMM::Index indexFloatingLegEnd = floatingLegPaymentIndexSchedule.back(); double pvFloatingLegValue = ZC[indexFloatingLegStart-1] - ZC[indexFloatingLegEnd]; ////! pvFloatingLeg: exact //double pvFloatingLeg = 0.0; //const std::vector<LMM::Index>& floatingLegPaymentIndexSchedule = vanillaSwap.get_floatingLegPaymentIndexSchedule(); //for(size_t itr = 0; itr < floatingLegPaymentIndexSchedule.size(); ++itr) //{ // //! At time T_{i+1}, pay: L_i(T_i) // size_t floatingLegPaymentIndex = floatingLegPaymentIndexSchedule[itr]; // = i+1 // size_t indexLibor = floatingLegPaymentIndex-1; // =i, because : floatingTenor = lmmTenor // pvFloatingLeg += floatingLegdelta_T*initLibor[indexLibor]*ZC[floatingLegPaymentIndex]; //} LMM::Index indexValuationDate = 0; double pvFixedLegValue = pvFixedLeg(indexValuationDate,vanillaSwap,numeraire); return pvFloatingLegValue - pvFixedLegValue; }
void pVGRID(float *xyz) { MULTIGRID *mg; ELEMENT *e; VERTEX *v; int i; mg = GetCurrentMultigrid(); ClearVertexMarkers(mg); SURFACE_LOOP_BEGIN(mg, e) for (i = 0; i < CORNERS_OF_ELEM(e); i++) { v = MYVERTEX(CORNER(e, i)); if (USED(v)) continue; SETUSED(v, 1); *xyz++ = XC(v); *xyz++ = YC(v); *xyz++ = ZC(v); } SURFACE_LOOP_END }
static INT TecplotCommand (INT argc, char **argv) { INT i,j,k,v; /* counters etc. */ INT counter; /* for formatting output */ char item[1024],it[256]; /* item buffers */ INT ic=0; /* item length */ VECTOR *vc; /* a vector pointer */ ELEMENT *el; /* an element pointer */ MULTIGRID *mg; /* our multigrid */ char filename[NAMESIZE]; /* file name for output file */ PFILE *pf; /* the output file pointer */ INT nv; /* number of variables (eval functions) */ EVALUES *ev[MAXVARIABLES]; /* pointers to eval function descriptors */ char ev_name[MAXVARIABLES][NAMESIZE]; /* names for eval functions */ char s[NAMESIZE]; /* name of eval proc */ char zonename[NAMESIZE+7] = ""; /* name for zone (initialized to empty string) */ INT numNodes; /* number of data points */ INT numElements; /* number of elements */ INT gnumNodes; /* number of data points globally */ INT gnumElements; /* number of elements globallay */ PreprocessingProcPtr pre; /* pointer to prepare function */ ElementEvalProcPtr eval; /* pointer to evaluation function */ DOUBLE *CornersCoord[MAX_CORNERS_OF_ELEM]; /* pointers to coordinates */ DOUBLE LocalCoord[DIM]; /* is one of the corners local coordinates */ DOUBLE local[DIM]; /* local coordinate in DOUBLE */ DOUBLE value; /* returned by user eval proc */ INT oe,on; INT saveGeometry; /* save geometry flag */ /* get current multigrid */ mg = GetCurrentMultigrid(); if (mg==NULL) { PrintErrorMessage('W',"tecplot","no multigrid open\n"); return (OKCODE); } /* scan options */ nv = 0; saveGeometry = 0; for(i=1; i<argc; i++) { switch(argv[i][0]) { case 'e' : /* read eval proc */ if (nv>=MAXVARIABLES) { PrintErrorMessage('E',"tecplot","too many variables specified\n"); break; } sscanf(argv[i],"e %s", s); ev[nv] = GetElementValueEvalProc(s); if (ev[nv]==NULL) { PrintErrorMessageF('E',"tecplot","could not find eval proc %s\n",s); break; } if (sscanf(argv[i+1],"s %s", s) == 1) { strcpy(ev_name[nv],s); i++; } else strcpy(ev_name[nv],ev[nv]->v.name); nv++; break; case 'z' : sscanf(argv[i],"z %s", zonename+3); memcpy(zonename, "T=\"", 3); memcpy(zonename+strlen(zonename), "\", \0", 4); break; case 'g' : sscanf(argv[i],"g %d", &saveGeometry); if (saveGeometry<0) saveGeometry=0; if (saveGeometry>1) saveGeometry=1; break; } } if (nv==0) UserWrite("tecplot: no variables given, printing mesh data only\n"); /* get file name and open output file */ if (sscanf(argv[0],expandfmt(CONCAT3(" tecplot %",NAMELENSTR,"[ -~]")),filename)!=1) { PrintErrorMessage('E',"tecplot","could not read name of logfile"); return(PARAMERRORCODE); } pf = pfile_open(filename); if (pf==NULL) return(PARAMERRORCODE); /********************************/ /* TITLE */ /********************************/ ic = 0; sprintf(it,"TITLE = \"UG TECPLOT OUTPUT\"\n"); strcpy(item+ic,it); ic+=strlen(it); sprintf(it,"VARIABLES = \"X\", \"Y\""); strcpy(item+ic,it); ic+=strlen(it); if (DIM==3) { sprintf(it,", \"Z\""); strcpy(item+ic,it); ic+=strlen(it); } for (i=0; i<nv; i++) { sprintf(it,", \"%s\"",ev[i]->v.name); strcpy(item+ic,it); ic+=strlen(it); } sprintf(it,"\n"); strcpy(item+ic,it); ic+=strlen(it); pfile_master_puts(pf,item); ic=0; /********************************/ /* compute sizes */ /********************************/ /* clear VCFLAG on all levels */ for (k=0; k<=TOPLEVEL(mg); k++) for (vc=FIRSTVECTOR(GRID_ON_LEVEL(mg,k)); vc!=NULL; vc=SUCCVC(vc)) SETVCFLAG(vc,0); /* run thru all levels of elements and set index */ numNodes = numElements = 0; for (k=0; k<=TOPLEVEL(mg); k++) for (el=FIRSTELEMENT(GRID_ON_LEVEL(mg,k)); el!=NULL; el=SUCCE(el)) { if (!EstimateHere(el)) continue; /* process finest level elements only */ numElements++; /* increase element counter */ for (i=0; i<CORNERS_OF_ELEM(el); i++) { vc = NVECTOR(CORNER(el,i)); if (VCFLAG(vc)) continue; /* we have this one already */ VINDEX(vc) = ++numNodes; /* number of data points, begins with 1 ! */ SETVCFLAG(vc,1); /* tag vector as visited */ } } #ifdef ModelP gnumNodes = TPL_GlobalSumINT(numNodes); gnumElements = TPL_GlobalSumINT(numElements); on=get_offset(numNodes); oe=get_offset(numElements); /* clear VCFLAG on all levels */ for (k=0; k<=TOPLEVEL(mg); k++) for (vc=FIRSTVECTOR(GRID_ON_LEVEL(mg,k)); vc!=NULL; vc=SUCCVC(vc)) SETVCFLAG(vc,0); /* number in unique way */ for (k=0; k<=TOPLEVEL(mg); k++) for (el=FIRSTELEMENT(GRID_ON_LEVEL(mg,k)); el!=NULL; el=SUCCE(el)) { if (!EstimateHere(el)) continue; /* process finest level elements only */ for (i=0; i<CORNERS_OF_ELEM(el); i++) { vc = NVECTOR(CORNER(el,i)); if (VCFLAG(vc)) continue; /* we have this one already */ VINDEX(vc) += on; /* add offset */ SETVCFLAG(vc,1); /* tag vector as visited */ } } #else gnumNodes = numNodes; gnumElements = numElements; oe=on=0; #endif /********************************/ /* write ZONE data */ /* uses FEPOINT for data */ /* uses QUADRILATERAL in 2D */ /* and BRICK in 3D */ /********************************/ /* write zone record header */ if (DIM==2) sprintf(it,"ZONE %sN=%d, E=%d, F=FEPOINT, ET=QUADRILATERAL\n", zonename, gnumNodes,gnumElements); if (DIM==3) sprintf(it,"ZONE %sN=%d, E=%d, F=FEPOINT, ET=BRICK\n", zonename, gnumNodes,gnumElements); strcpy(item+ic,it); ic+=strlen(it); pfile_master_puts(pf,item); ic=0; /* write data in FEPOINT format, i.e. all variables of a node per line*/ for (k=0; k<=TOPLEVEL(mg); k++) for (vc=FIRSTVECTOR(GRID_ON_LEVEL(mg,k)); vc!=NULL; vc=SUCCVC(vc)) SETVCFLAG(vc,0); /* clear all flags */ counter=0; for (k=0; k<=TOPLEVEL(mg); k++) for (el=FIRSTELEMENT(GRID_ON_LEVEL(mg,k)); el!=NULL; el=SUCCE(el)) { if (!EstimateHere(el)) continue; /* process finest level elements only */ for (i=0; i<CORNERS_OF_ELEM(el); i++) CornersCoord[i] = CVECT(MYVERTEX(CORNER(el,i))); /* x,y,z of corners */ for (i=0; i<CORNERS_OF_ELEM(el); i++) { vc = NVECTOR(CORNER(el,i)); if (VCFLAG(vc)) continue; /* we have this one alre ady */ SETVCFLAG(vc,1); /* tag vector as visited */ sprintf(it,"%g",(double)XC(MYVERTEX(CORNER(el,i)))); strcpy(item+ic,it); ic+=strlen(it); sprintf(it," %g",(double)YC(MYVERTEX(CORNER(el,i)))); strcpy(item+ic,it); ic+=strlen(it); if (DIM == 3) { sprintf(it," %g",(double)ZC(MYVERTEX(CORNER(el,i)))); strcpy(item+ic,it); ic+=strlen(it); } /* now all the user variables */ /* get local coordinate of corner */ LocalCornerCoordinates(DIM,TAG(el),i,local); for (j=0; j<DIM; j++) LocalCoord[j] = local[j]; for (v=0; v<nv; v++) { pre = ev[v]->PreprocessProc; eval = ev[v]->EvalProc; /* execute prepare function */ /* This is not really equivalent to the FEBLOCK-version sinc we call "pre" more often than there. D.Werner */ if (pre!=NULL) pre(ev_name[v],mg); /* call eval function */ value = eval(el,(const DOUBLE **)CornersCoord,LocalCoord); sprintf(it," %g",value); strcpy(item+ic,it); ic+=strlen(it); } sprintf(it,"\n"); strcpy(item+ic,it); ic+=strlen(it); pfile_tagged_puts(pf,item,counter+on); ic=0; counter++; } } pfile_sync(pf); /* end of segment */ sprintf(it,"\n"); strcpy(item+ic,it); ic+=strlen(it); pfile_master_puts(pf,item); ic=0; /* finally write the connectivity list */ counter=0; for (k=0; k<=TOPLEVEL(mg); k++) for (el=FIRSTELEMENT(GRID_ON_LEVEL(mg,k)); el!=NULL; el=SUCCE(el)) { if (!EstimateHere(el)) continue; /* process finest level elements only */ switch(DIM) { case 2 : switch(TAG(el)) { case TRIANGLE : sprintf(it,"%d %d %d %d\n", VINDEX(NVECTOR(CORNER(el,0))), VINDEX(NVECTOR(CORNER(el,1))), VINDEX(NVECTOR(CORNER(el,2))), VINDEX(NVECTOR(CORNER(el,2))) ); break; case QUADRILATERAL : sprintf(it,"%d %d %d %d\n", VINDEX(NVECTOR(CORNER(el,0))), VINDEX(NVECTOR(CORNER(el,1))), VINDEX(NVECTOR(CORNER(el,2))), VINDEX(NVECTOR(CORNER(el,3))) ); break; default : UserWriteF("tecplot: unknown 2D element type with tag(el) = %d detected. Aborting further processing of command tecplot\n", TAG(el)); return CMDERRORCODE; break; } break; case 3 : switch(TAG(el)) { case HEXAHEDRON : sprintf(it,"%d %d %d %d " "%d %d %d %d\n", VINDEX(NVECTOR(CORNER(el,0))), VINDEX(NVECTOR(CORNER(el,1))), VINDEX(NVECTOR(CORNER(el,2))), VINDEX(NVECTOR(CORNER(el,3))), VINDEX(NVECTOR(CORNER(el,4))), VINDEX(NVECTOR(CORNER(el,5))), VINDEX(NVECTOR(CORNER(el,6))), VINDEX(NVECTOR(CORNER(el,7))) ); break; case TETRAHEDRON : sprintf(it,"%d %d %d %d " "%d %d %d %d\n", VINDEX(NVECTOR(CORNER(el,0))), VINDEX(NVECTOR(CORNER(el,1))), VINDEX(NVECTOR(CORNER(el,2))), VINDEX(NVECTOR(CORNER(el,2))), VINDEX(NVECTOR(CORNER(el,3))), VINDEX(NVECTOR(CORNER(el,3))), VINDEX(NVECTOR(CORNER(el,3))), VINDEX(NVECTOR(CORNER(el,3))) ); break; case PYRAMID : sprintf(it,"%d %d %d %d " "%d %d %d %d\n", VINDEX(NVECTOR(CORNER(el,0))), VINDEX(NVECTOR(CORNER(el,1))), VINDEX(NVECTOR(CORNER(el,2))), VINDEX(NVECTOR(CORNER(el,3))), VINDEX(NVECTOR(CORNER(el,4))), VINDEX(NVECTOR(CORNER(el,4))), VINDEX(NVECTOR(CORNER(el,4))), VINDEX(NVECTOR(CORNER(el,4))) ); break; case PRISM : sprintf(it,"%d %d %d %d " "%d %d %d %d\n", VINDEX(NVECTOR(CORNER(el,0))), VINDEX(NVECTOR(CORNER(el,1))), VINDEX(NVECTOR(CORNER(el,2))), VINDEX(NVECTOR(CORNER(el,2))), VINDEX(NVECTOR(CORNER(el,3))), VINDEX(NVECTOR(CORNER(el,4))), VINDEX(NVECTOR(CORNER(el,5))), VINDEX(NVECTOR(CORNER(el,5))) ); break; default : UserWriteF("tecplot: unknown 3D element type with tag(el) = %d detected. Aborting further processing of command tecplot\n", TAG(el)); return CMDERRORCODE; break; } break; } strcpy(item+ic,it); ic+=strlen(it); pfile_tagged_puts(pf,item,counter+oe); ic=0; counter++; } pfile_sync(pf); /* end of segment */ /********************************/ /* GEOMETRY */ /* we will do this later, since */ /* domain interface will change */ /********************************/ pfile_close(pf); return(OKCODE); }