void terminal_moveAllUp(uint8_t li_to_cl) { for (size_t y = 0; y < VGA_HEIGHT - li_to_cl; y++) { for (size_t x = 0; x < VGA_WIDTH; x++) { terminal_buffer[makeIndex(x, y)] = terminal_buffer[makeIndex(x, y+li_to_cl)]; terminal_putentryat(' ', terminal_color, x, y+li_to_cl); } } terminal_row -= li_to_cl; }
void RemoveEntryCommand::redo() { freeEntry(); const QModelIndex index = makeIndex(); m_isExpanded = m_view->isExpanded(index); m_entry = m_view->removeEntry(index); }
bool ThresholdPressure::hasRegionBarrier(int r1 , int r2) const { std::pair<int,int> indexPair = makeIndex(r1,r2); if (m_pressureTable.find( indexPair ) == m_pressureTable.end()) return false; else return true; }
void StableFluid3D::advectPressure(double dt) { double xStep = width / (double)resX; double yStep = height / (double)resY; double zStep = depth / (double)resZ; double fOffsetsX[3] = { 0, yStep * 0.5, zStep * 0.5 }; double fOffsetsY[3] = { xStep * 0.5, 0, zStep * 0.5 }; double fOffsetsZ[3] = { xStep * 0.5, yStep * 0.5, 0 }; for (int i = 0; i < resX; ++i) { for (int j = 0; j < resY; ++j) { for (int k = 0; k < resZ; ++k) { vec3d pos; pos[0] = i*xStep; pos[1] = j*yStep; pos[2] = k*zStep; vec3d v = linearSamp(pos, velocities); pressure[makeIndex(i, j, k)] = linearSamp(pos - v * dt, tempPressure); } } } }
void BeaconIterator::makeIndex(Beacon *beacon, bool isDFS, bool isFirst) { if (isFirst) { beaconIndex.clear(); curIdx = -1; } beaconIndex.push_back(beacon); if (isDFS) { // DFS. recursive for (size_t i = 0; i < beacon->childrenSize(); i++) { makeIndex(beacon->childAt(i), isDFS, false/*isFirst*/); } } else { // BFS int cur = 0; Beacon *curBeacon; while(cur < (int)beaconIndex.size()) { curBeacon = beaconIndex[cur]; for (size_t i = 0; i < curBeacon->childrenSize(); i++) { beaconIndex.push_back(curBeacon->childAt(i)); } cur++; } } }
void ModifyPropertyCommand::undo() { Q_ASSERT(m_view); // Save current text in m_after for redo() m_after = m_view->getCurrentValue(m_property); // Reset text to m_before m_view->changeValue(makeIndex(), m_property, m_before); }
void ModifyPropertyCommand::redo() { // Prevent execution from within QUndoStack::push if (m_after.isNull()) return; // Bring back text before undo Q_ASSERT(m_view); m_view->changeValue(makeIndex(), m_property, m_after); }
bool ThresholdPressure::hasThresholdPressure(int r1 , int r2) const { std::pair<int,int> indexPair = makeIndex(r1,r2); auto iter = m_pressureTable.find( indexPair ); if (iter == m_pressureTable.end()) return false; else { auto pair_pair = *iter; auto value_pair = pair_pair.second; return value_pair.first; } }
void RemoveEntryCommand::undo() { if (m_entry == 0) { m_entry->restore(); Q_ASSERT(m_view != 0); const QModelIndex index = makeIndex(); m_view->setExpanded(index, m_isExpanded); m_view->setCurrentIndex(index); freeEntry(); } }
double caplet_lmm(const Date& todaysDate_, const Date& settlementDate_, const Date& maturity_, Rate spot_, Rate strike, Rate Numeraire, //zero-coupon bond //Volatility volatility double correl, double a, double b, double c, double d ) { //SavedSettings backup; const Size size = 10; #if defined(QL_USE_INDEXED_COUPON) const Real tolerance = 1e-5; #else const Real tolerance = 1e-12; #endif boost::shared_ptr<IborIndex> index = makeIndex(); boost::shared_ptr<LiborForwardModelProcess> process(new LiborForwardModelProcess(size, index)); // set-up pricing engine const boost::shared_ptr<OptionletVolatilityStructure> capVolCurve = makeCapVolCurve(Settings::instance().evaluationDate()); Array variances = LfmHullWhiteParameterization(process, capVolCurve).covariance(0.0).diagonal(); boost::shared_ptr<LmVolatilityModel> volaModel(new LmFixedVolatilityModel(Sqrt(variances),process->fixingTimes())); boost::shared_ptr<LmCorrelationModel> corrModel(new LmExponentialCorrelationModel(size, correl)); boost::shared_ptr<AffineModel> model(new LiborForwardModel(process, volaModel, corrModel)); const Handle<YieldTermStructure> termStructure = process->index()->forwardingTermStructure(); boost::shared_ptr<AnalyticCapFloorEngine> engine1(new AnalyticCapFloorEngine(model, termStructure)); boost::shared_ptr<Cap> cap1(new Cap(process->cashFlows(),std::vector<Rate>(size, strike))); cap1->setPricingEngine(engine1); return cap1->NPV(); }
double ThresholdPressure::getThresholdPressure(int r1 , int r2) const { std::pair<int,int> indexPair = makeIndex(r1,r2); auto iter = m_pressureTable.find( indexPair ); if (iter == m_pressureTable.end()) return 0.0; else { auto pair_pair = *iter; auto value_pair = pair_pair.second; bool valid = value_pair.first; double value = value_pair.second; if (valid) return value; else { std::string msg = "The THPRES value for regions " + std::to_string(r1) + " and " + std::to_string(r2) + " has not been initialized. Using 0.0"; throw std::invalid_argument(msg); return 0; } } }
double StableFluid3D::linearSamp(double x, double y, double z, Vector& v) const { x *= width / (double)resX; y *= height / (double)resY; z *= depth / (double)resZ; x = (x < 0 ? 0 : (x > width ? width : x)); y = (y < 0 ? 0 : (y > height ? height : y)); z = (z < 0 ? 0 : (z > depth ? depth : z)); int x0 = (int)(x == width ? width - 1 : x); double xAlpha = x - x0; int y0 = (int)(y == height ? height - 1 : y); double yAlpha = y - y0; int z0 = (int)(z == depth ? depth - 1 : z); double zAlpha = z - z0; double dx = (1 - xAlpha) * v[makeIndex(x0, y0, z0)] + xAlpha * v[makeIndex(x0 + 1, y0, z0)]; double dy = (1 - yAlpha) * v[makeIndex(x0, y0, z0)] + yAlpha * v[makeIndex(x0, y0 + 1, z0)]; double dz = (1 - zAlpha) * v[makeIndex(x0, y0, z0)] + zAlpha * v[makeIndex(x0, y0, z0 + 1)]; return (dx + dy + dz) / 3.0; }
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) { const size_t index = makeIndex(x, y); terminal_buffer[index] = make_vgaentry(c, color); }
void ThresholdPressure::addPair(int r1 , int r2 , const std::pair<bool , double>& valuePair) { std::pair<int,int> indexPair = makeIndex(r1,r2); m_pressureTable[indexPair] = valuePair; }
void StableFluid3D::projectVelocity(double dt) { setBoundary(); Vector divergence(vel.Length()); // fill in velocities double dx = width / (double)resX; double dy = height / (double)resY; double dz = depth / (double)resZ; for (int i = 0; i < resX; ++i) { for (int j = 0; j < resY; ++j) { for (int k = 0; k < resZ; ++k) { int index = makeIndex(i, j, k); double div = (tempVelocities[0][i + 1][j][k] - tempVelocities[0][i][j][k]) / dx + (tempVelocities[1][i][j + 1][k] - tempVelocities[1][i][j][k]) / dy + (tempVelocities[2][i][j][k + 1] - tempVelocities[2][i][j][k]) / dz; vel[index] = div; divergence[index] = div; } } } // vel *= density / dt; bool useOldPCG = false; if (useOldPCG) { int k = 0; Vector laplacianTimesPressure(pressure.Length()); laplacian.Multiply(pressure, laplacianTimesPressure); r[0] = vel - laplacianTimesPressure; double error = r[0].Magnitude2(); while ((std::sqrt(error) > PCG_EPS) && (k < PCG_MAXITER)) { //cerr << "PCG iteration " << k << ", error: " << std::sqrt(error) << endl; ConjugateGradient(preconditioner, z[k % 2], r[k % 2], 0.0001, 500.0); ++k; int i1 = (k - 1) % 2; int i2 = k % 2; if (k == 1) { p = z[0]; } else { double beta = (InnerProduct(r[i1], z[i1])) / (InnerProduct(r[i2], z[i2])); p *= beta; p += z[i1]; } Vector laplacianTimesP(p.Length()); laplacian.Multiply(p, laplacianTimesP); double alpha = (InnerProduct(r[i1], z[i1])) / (InnerProduct(p, laplacianTimesP)); pressure += alpha * p; r[i2] = r[i1] - alpha * (laplacianTimesP); error = r[i2].Magnitude2(); } if (std::sqrt(error) > PCG_EPS) { std::cout << "end preconditioned conj grad " << "error: " << error << std::endl; } } else { p = Vector(vel.Length()); r[0] = divergence; //ApplyPreconditioner(r[0], z[0]); double sigma = InnerProduct(r[0], z[0]); int k = 0; double error = r[0].Magnitude2(); while (k < PCG_MAXITER) { ++k; Vector s(vel.Length()); s = z[0]; int k = 0; laplacian.Multiply(s, z[0]); double rho = 1.0; double alpha = rho / InnerProduct(s, z[0]); p += alpha * s; r[0] -= alpha * z[0]; error = r[0].Magnitude2(); if (error < PCG_EPS * PCG_EPS) { pressure = p; break; } //ApplyPreconditioner(r[0], z[0]); double sigmaNew = InnerProduct(r[0], z[0]); double beta = sigmaNew / rho; s = z[0] + beta * s; sigma = sigmaNew; } Vector laplacianTimesPressure(pressure.Length()); laplacian.Multiply(pressure, laplacianTimesPressure); r[0] = vel - laplacianTimesPressure; error = r[0].Magnitude2(); while ((std::sqrt(error) > PCG_EPS) && (k < PCG_MAXITER)) { //cerr << "PCG iteration " << k << ", error: " << std::sqrt(error) << endl; ConjugateGradient(preconditioner, z[k % 2], r[k % 2], 0.0001, 500.0); ++k; int i1 = (k - 1) % 2; int i2 = k % 2; if (k == 1) { p = z[0]; } else { double beta = (InnerProduct(r[i1], z[i1])) / (InnerProduct(r[i2], z[i2])); p *= beta; p += z[i1]; } Vector laplacianTimesP(p.Length()); laplacian.Multiply(p, laplacianTimesP); double alpha = (InnerProduct(r[i1], z[i1])) / (InnerProduct(p, laplacianTimesP)); pressure += alpha * p; r[i2] = r[i1] - alpha * (laplacianTimesP); error = r[i2].Magnitude2(); } if (std::sqrt(error) > PCG_EPS) { std::cout << "end preconditioned conj grad " << "error: " << error << std::endl; } } /* mathlib::Vector PCGr, PCGd, PCGq, PCGs; PCGr.Resize(resX * resY * resZ); PCGd.Resize(resX * resY * resZ); PCGq.Resize(resX * resY * resZ); PCGs.Resize(resX * resY * resZ); int iter = 0; PCGr = vel - laplacian * pressure; mathlib::ConjugateGradient(preconditioner, PCGd, PCGr); double deltaNew = mathlib::InnerProduct(PCGr, PCGd); double errBound = PCG_EPS * PCG_EPS * deltaNew; while ((iter < PCG_MAXITER) && (deltaNew > errBound)) { if (!(iter % 3)) cerr << "iteration " << iter << ", error: " << deltaNew << endl; PCGq = laplacian * PCGd; double alpha = deltaNew / (mathlib::InnerProduct(PCGd, PCGq)); pressure += alpha * PCGd; if ((iter % 10) == 0) PCGr = vel - laplacian * pressure; else PCGr -= alpha * PCGq; mathlib::ConjugateGradient(preconditioner, PCGs, PCGr); double deltaOld = deltaNew; deltaNew = mathlib::InnerProduct(PCGr, PCGs); double beta = deltaNew / deltaOld; PCGd *= beta; PCGd += PCGs; ++iter; } cout << "end preconditioned conj grad "<< "error: " << deltaNew << endl; */ std::cout << "dt:" << dt << "\t density:" << density << std::endl; // dt = 1.0; for (int i = 0; i < resX; ++i) { for (int j = 0; j < resY; ++j) { for (int k = 0; k < resZ; ++k) { int i1 = makeIndex(i, j, k); int i2; double deltaPressure; i2 = (i == 0 ? i1 : i1 - 1); velocities[0][i][j][k] = tempVelocities[0][i][j][k] - dt* 0.5 * (pressure[i1] - pressure[i2]) / dx; deltaPressure = pressure[i1] - pressure[i2]; i2 = (j == 0 ? i1 : i1 - resX); velocities[1][i][j][k] = tempVelocities[1][i][j][k] - dt * 0.5 * (pressure[i1] - pressure[i2]) / dy; deltaPressure = pressure[i1] - pressure[i2]; i2 = (k == 0 ? i1 : i1 - resY * resX); velocities[2][i][j][k] = tempVelocities[2][i][j][k] - dt * 0.5 * (pressure[i1] - pressure[i2]) / dz; deltaPressure = pressure[i1] - pressure[i2]; } } } }
void StableFluid3D::initPoisson() { clearPoisson(); int n = resX * resY * resZ; for (int i = 0; i < 2; ++i) { r.push_back(Vector(n)); z.push_back(Vector(n)); } p.Resize(n); SparseMatrix hTranspose; SparseMatrix h; laplacian = SparseMatrix(n, n); hTranspose = SparseMatrix(n, n); h = SparseMatrix(n, n); preconditioner = SparseMatrix(n, n); pressure.Resize(n); tempPressure.Resize(n); vel.Resize(n); ones.Resize(n); for (int i = 0; i < n; ++i) { ones[i] = 1; } double rdxSqr = (resX * resX) / (width * width); double rdySqr = (resY * resY) / (height * height); double rdzSqr = (resZ * resZ) / (depth * depth); // initialize values for the laplacian matrix for (int i = 0; i < resX; ++i) { for (int j = 0; j < resY; ++j) { for (int k = 0; k < resZ; ++k) { int row = makeIndex(i, j, k); int x1 = (i == (resX - 1) ? row : row + 1); int x2 = (i == 0 ? row : row - 1); int y1 = (j == (resY - 1) ? row : row + resX); int y2 = (j == 0 ? row : row - resX); int z1 = (k == (resZ - 1) ? row : row + resY * resX); int z2 = (k == 0 ? row : row - resY * resX); laplacian[row][row] += 6 * (rdxSqr + rdySqr + rdzSqr); laplacian[row][x1] -= rdxSqr; laplacian[row][x2] -= rdxSqr; laplacian[row][y1] -= rdySqr; laplacian[row][y2] -= rdySqr; laplacian[row][z1] -= rdzSqr; laplacian[row][z2] -= rdzSqr; } } } // perform Cholesky preconditioning // decompose laplacian into H * H^T SparseVector::iterator iter; hTranspose = laplacian.Transpose(); SparseMatrix& A = hTranspose; for (int k = 0; k < n; ++k) { SparseVector::iterator aik(A[k]); aik.seek(k); if (aik.getIndex() != k) { assert(0); } double divisor = std::sqrt(*aik); *aik = divisor; aik++; while (aik) { *aik /= divisor; aik++; } SparseVector::iterator ajk(A[k]); ajk.seek(k + 1); while (ajk) { int j = ajk.getIndex(); aik.reset(); aik.seek(j); SparseVector::iterator aij(A[j]); aij.seek(j); while (aij && aik) { int i = aij.getIndex(); if (aik.getIndex() == i) { *aij -= (*aik) * (*ajk); aik++; } aij++; if (aij) { i = aij.getIndex(); while (aik && aik.getIndex() < i) { aik++; } } } ajk++; } } h = hTranspose.Transpose(); // cerr<<"Sparsity of laplacian is " << laplacian.Sparsity() << endl; preconditioner = h.FastMultiplyTranspose(); int maxIndex = resX * resY * resZ; // for (int i = 0; i < maxIndex; ++i) // preconditioner[i][i] = 1; // cerr<<"Sparsity of preconditioner is " << preconditioner.Sparsity() << endl; }