int main(int argc, char** argv) { //------------------------------------------------------------------------------ // Assimilando dados do arquivo externo //------------------------------------------------------------------------------ Volumes v1; LeituraDadosProblema (v1); std :: vector <Real> xFronteiras (v1.NVOL()+1), //localizações fronteiras xCentro (v1.NVOL()), //localizações centros DistCentro (v1.NVOL()+1), //distâncias entre centros DistFace (v1.NVOL()); //distância entre fronteiras adjacentes GeracaoMalha (xFronteiras, xCentro, DistCentro, DistFace, v1); std :: vector <Real> Ap(v1.NVOL()), Ae(v1.NVOL()), Aw(v1.NVOL()), Sp(v1.NVOL()); CalculaCoeficientes (DistFace, DistCentro, Ap, Ae, Aw, Sp); for (int i=0; i<v1.NVOL(); i++) { std :: cout << "Ae[" << i << "]=" << Ap[i] << std :: endl; } }
int main() { // Primary Operators AnnihilationOperator A1(0); // 1st freedom NumberOperator N1(0); IdentityOperator Id1(0); AnnihilationOperator A2(1); // 2nd freedom NumberOperator N2(1); IdentityOperator Id2(1); SigmaPlus Sp(2); // 3rd freedom IdentityOperator Id3(2); Operator Sm = Sp.hc(); // Hermitian conjugate Operator Ac1 = A1.hc(); Operator Ac2 = A2.hc(); // Hamiltonian double E = 20.0; double chi = 0.4; double omega = -0.7; double eta = 0.001; Complex I(0.0,1.0); Operator H = (E*I)*(Ac1-A1) + (0.5*chi*I)*(Ac1*Ac1*A2 - A1*A1*Ac2) + omega*Sp*Sm + (eta*I)*(A2*Sp-Ac2*Sm); // Lindblad operators double gamma1 = 1.0; double gamma2 = 1.0; double kappa = 0.1; const int nL = 3; Operator L[nL]={sqrt(2*gamma1)*A1,sqrt(2*gamma2)*A2,sqrt(2*kappa)*Sm}; // Initial state State phi1(50,FIELD); // see paper Section 4.2 State phi2(50,FIELD); State phi3(2,SPIN); State stateList[3] = {phi1,phi2,phi3}; State psiIni(3,stateList); // Trajectory double dt = 0.01; // basic time step int numdts = 100; // time interval between outputs = numdts*dt int numsteps = 5; // total integration time = numsteps*numdts*dt int nOfMovingFreedoms = 2; double epsilon = 0.01; // cutoff probability int nPad = 2; // pad size //ACG gen(38388389); // random number generator with seed //ComplexNormal rndm(&gen); // Complex Gaussian random numbers ComplexNormalTest rndm(899101); // Simple portable random number generator AdaptiveStep stepper(psiIni, H, nL, L); // see paper Section 5 // Output const int nOfOut = 3; Operator outlist[nOfOut]={ Sp*A2*Sm*Sp, Sm*Sp*A2*Sm, A2 }; char *flist[nOfOut]={"X1.out","X2.out","A2.out"}; int pipe[] = {1,5,9,11}; // controls standard output (see `onespin.cc') // Simulate one trajectory (for several trajectories see `onespin.cc') Trajectory traj(psiIni, dt, stepper, &rndm); // see paper Section 5 traj.plotExp( nOfOut, outlist, flist, pipe, numdts, numsteps, nOfMovingFreedoms, epsilon, nPad ); }
Spectrum SeparableBSSRDF::Sample_Sp(const Scene &scene, Float sample1, const Point2f &sample2, MemoryArena &arena, SurfaceInteraction *pi, Float *pdf) const { ProfilePhase pp(Prof::BSSRDFEvaluation); // Choose projection axis for BSSRDF sampling Vector3f axis; if (sample1 < .25f) { axis = ss; sample1 *= 4; } else if (sample1 < .5f) { axis = ts; sample1 = (sample1 - .25f) * 4; } else { axis = Vector3f(ns); sample1 = (sample1 - .5f) * 2; } // Choose spectral channel for BSSRDF sampling int ch = Clamp((int)(sample1 * Spectrum::nSamples), 0, Spectrum::nSamples - 1); sample1 = sample1 * Spectrum::nSamples - ch; // Sample BSSRDF profile in polar coordinates Float r = Sample_Sr(ch, sample2.x); if (r < 0) return Spectrum(0.f); Float phi = 2 * Pi * sample2.y; // Compute BSSRDF profile bounds and intersection height Float rMax = Sample_Sr(ch, 0.999f); if (r > rMax) return Spectrum(0.f); Float l = 2 * std::sqrt(rMax * rMax - r * r); // Compute BSSRDF sampling ray segment Vector3f v0, v1; CoordinateSystem(axis, &v0, &v1); Interaction base; base.p = po.p + r * (v0 * std::cos(phi) + v1 * std::sin(phi)) - l * axis * 0.5f; base.time = po.time; Point3f target = base.p + l * axis; // Intersect BSSRDF sampling ray against the scene geometry struct IntersectionChain { SurfaceInteraction si; IntersectionChain *next; }; IntersectionChain *chain = ARENA_ALLOC(arena, IntersectionChain)(), *ptr = chain; int nFound = 0; while (true) { if (!scene.Intersect(base.SpawnRayTo(target), &ptr->si)) break; base = ptr->si; // Append admissible intersection to _IntersectionChain_ if (ptr->si.primitive->GetMaterial() == material) { IntersectionChain *next = ARENA_ALLOC(arena, IntersectionChain)(); ptr->next = next; ptr = next; nFound++; } } // Randomly choose one of several intersections during BSSRDF sampling if (nFound == 0) return Spectrum(0.0f); int selected = Clamp((int)(sample1 * nFound), 0, nFound - 1); while (selected-- > 0) chain = chain->next; *pi = chain->si; // Compute sample PDF and return the spatial BSSRDF term $\Sp$ *pdf = Pdf_Sp(*pi) / nFound; return Sp(*pi); }
double CompareFunctions(FUNC_COMPARE_METHOD nMethod, CFuncDescFile *pFile1, int nFile1FuncNdx, CFuncDescFile *pFile2, int nFile2FuncNdx, BOOL bBuildEditScript) { CFuncDesc *pFunction1; CFuncDesc *pFunction2; CStringArray zFunc1; CStringArray zFunc2; double nRetVal = 0; CString strTemp; m_bEditScriptValid = FALSE; m_EditScript.RemoveAll(); if ((pFile1 == NULL) || (pFile2 == NULL)) return nRetVal; pFunction1 = pFile1->GetFunc(nFile1FuncNdx); pFunction2 = pFile2->GetFunc(nFile2FuncNdx); if ((pFunction1 == NULL) || (pFunction2 == NULL)) return nRetVal; pFunction1->ExportToDiff(zFunc1); pFunction2->ExportToDiff(zFunc2); if ((zFunc1.GetSize() == 0) || (zFunc2.GetSize() == 0)) return nRetVal; if ((bBuildEditScript) && (nMethod == FCM_DYNPROG_XDROP)) { // Note: XDROP Method currently doesn't support building // of edit scripts, so if caller wants to build an // edit script and has selected this method, replace // it with the next best method that does support // edit scripts: nMethod = FCM_DYNPROG_GREEDY; } switch (nMethod) { case FCM_DYNPROG_XDROP: { // // The following algorithm comes from the following source: // "A Greedy Algorithm for Aligning DNA Sequences" // Zheng Zhang, Scott Schwartz, Lukas Wagner, and Webb Miller // Journal of Computational Biology // Volume 7, Numbers 1/2, 2000 // Mary Ann Liebert, Inc. // Pp. 203-214 // // p. 205 : Figure 2 : "A dynamic-programming X-drop algorithm" // // T' <- T <- S(0,0) <- 0 // k <- L <- U <- 0 // repeat { // k <- k + 1 // for i <- ceiling(L) to floor(U)+1 in steps of 1/2 do { // j <- k - i // if i is an integer then { // S(i, j) <- Max of: // S(i-1/2, j-1/2) + mat/2 if L <= i-1/2 <= U and a(i) = b(j) // S(i-1/2, j-1/2) + mis/2 if L <= i-1/2 <= U and a(i) != b(j) // S(i, j-1) + ind if i <= U // S(i-1, j) + ind if L <= i-1 // } else { // S(i, j) <- S(i-1/2, j-1/2) // + mat/2 if a(i+1/2) = b(j+1/2) // + mis/2 if a(i+1/2) != b(j+1/2) // } // T' <- max{T', S(i, j)} // if S(i, j) < (T - X) then S(i, j) <- -oo // } // L <- min{i : S(i, k-i) > -oo } // U <- max{i : S(i, k-i) > -oo } // L <- max{L, k+1-N} <<< Should be: L <- max{L, k+1/2-N} // U <- min{U, M-1} <<< Should be: U <- min(U, M-1/2} // T <- T' // } until L > U+1 // report T' // // Given: // arrays: a(1..M), b(1..N) containing the two strings to compare // mat : >0 : Weighting of a match // mis : <0 : Weighting of a mismatch // ind : <0 : Weighting of an insertion/deletion // X = Clipping level : If scoring falls more than X below the // best computed score thus far, then we don't consider // additional extensions for that alignment. Should // be >= 0 or -1 for infinity. // // Returning: // T' = Composite similarity score // // For programmatic efficiency, all S indexes have been changed from // 0, 1/2 to even/odd and the i loop runs as integers of even/odd // instead of 1/2 increments: // // Testing has also been done and it has been proven that the order of // the two arrays has no effect on the outcome. // // In the following, we will define oo as DBL_MAX and -oo as -DBL_MAX. // // To hold to the non-Generalized Greedy Algorithm requirements, set // ind = mis - mat/2 // CStringArray &a = zFunc1; CStringArray &b = zFunc2; double Tp, T; double **S; int i, j, k, L, U; double nTemp; int M = a.GetSize(); int N = b.GetSize(); const double mat = 2; const double mis = -2; const double ind = -3; const double X = -1; // Allocate Memory: S = new double*[((M+1)*2)]; if (S == NULL) { AfxThrowMemoryException(); return nRetVal; } for (i=0; i<((M+1)*2); i++) { S[i] = new double[((N+1)*2)]; if (S[i] == NULL) AfxThrowMemoryException(); } // Initialize: for (i=0; i<((M+1)*2); i++) { for (j=0; j<((N+1)*2); j++) { S[i][j] = -DBL_MAX; } } // Algorithm: Tp = T = S[0][0] = 0; k = L = U = 0; do { k = k + 2; for (i = L+((L & 0x1) ? 1 : 0); i <= (U - ((U & 0x1) ? 1 : 0) + 2); i++) { j = k - i; ASSERT(i >= 0); ASSERT(i < ((M+1)*2)); ASSERT(j >= 0); ASSERT(j < ((N+1)*2)); if ((i&1) == 0) { nTemp = -DBL_MAX; if ((L <= (i-1)) && ((i-1) <= U)) { // TODO : Improve A/B Comparison: if (a.GetAt((i/2)-1).CompareNoCase(b.GetAt((j/2)-1)) == 0) { nTemp = __max(nTemp, S[i-1][j-1] + mat/2); } else { nTemp = __max(nTemp, S[i-1][j-1] + mis/2); } } if (i <= U) { nTemp = __max(nTemp, S[i][j-2] + ind); } if (L <= (i-2)) { nTemp = __max(nTemp, S[i-2][j] + ind); } S[i][j] = nTemp; } else { // TODO : Improve A/B Comparison: if (a.GetAt(((i+1)/2)-1).CompareNoCase(b.GetAt(((j+1)/2)-1)) == 0) { S[i][j] = S[i-1][j-1] + mat/2; } else { S[i][j] = S[i-1][j-1] + mis/2; } } Tp = __max(Tp, S[i][j]); if ((X>=0) && (S[i][j] < (T-X))) S[i][j] = -DBL_MAX; } for (L = 0; L < ((M+1)*2); L++) { if ((k - L) < 0) continue; if ((k - L) >= ((N+1)*2)) continue; if (S[L][k-L] > -DBL_MAX) break; } if (L == ((M+1)*2)) L=INT_MAX; for (U=((M+1)*2)-1; U>=0; U--) { if ((k - U) < 0) continue; if ((k - U) >= ((N+1)*2)) continue; if (S[U][k-U] > -DBL_MAX) break; } if (U == -1) U=INT_MIN; L = __max(L, k + 1 - (N*2)); U = __min(U, (M*2) - 1); T = Tp; } while (L <= U+2); // If the two PrimaryLabels at the function's address don't match, decrement match by 1*mat. // This helps if there are two are more functions that the user has labeled that // are identical except for the labels: if (pFile1->GetPrimaryLabel(pFunction1->GetMainAddress()).CompareNoCase( pFile2->GetPrimaryLabel(pFunction2->GetMainAddress())) != 0) Tp = __max(0, Tp - mat); // Normalize it: nRetVal = Tp/(__max(M,N)*mat); // Deallocate Memory: for (i=0; i<((M+1)*2); i++) delete[] (S[i]); delete[] S; } break; case FCM_DYNPROG_GREEDY: { // // The following algorithm comes from the following source: // "A Greedy Algorithm for Aligning DNA Sequences" // Zheng Zhang, Scott Schwartz, Lukas Wagner, and Webb Miller // Journal of Computational Biology // Volume 7, Numbers 1/2, 2000 // Mary Ann Liebert, Inc. // Pp. 203-214 // // p. 209 : Figure 4 : "Greedy algorithm that is equivalent to the algorithm // of Figure 2 if ind = mis - mat/2." // // i <- 0 // while i < min{M, N} and a(i+1) = b(i+1) do i <- i + 1 // R(0, 0) <- i // T' <- T[0] <- S'(i+i, 0) // d <- L <- U <- 0 // repeat // d <- d + 1 // d' <- d - floor( (X + mat/2)/(mat - mis) ) - 1 // for k <- L - 1 to U + 1 do // i <- max of: // R(d-1, k-1) + 1 if L < k // R(d-1, k) + 1 if L <= k <= U // R(d-1, k+1) if k < U // j <- i - k // if i > -oo and S'(i+j, d) >= T[d'] - X then // while i<M, j<N, and a(i+1) = b(j+1) do // i <- i + 1; j <- j + 1 // R(d, k) <- i // T' <- max{T', S'(i+j, d)} // else R(d, k) <- -oo // T[d] <- T' // L <- min{k : R(d, k) > -oo} // U <- max{k : R(d, k) > -oo} // L <- max{L, max{k : R(d, k) = N + k} + 2} <<< Should be: L <- max{L, max{k : R(d, k) = N + k} + 1} // U <- min{U, min{k : R(d, k) = M } - 2} <<< Should be: U <- min{U, min{k : R(d, k) = M } - 1} // until L > U + 2 // report T' // // Given: // arrays: a(1..M), b(1..N) containing the two strings to compare // mat : >0 : Weighting of a match // mis : <0 : Weighting of a mismatch // ind : <0 : Weighting of an insertion/deletion // (Satisfying: ind = mis - mat/2) // X = Clipping level : If scoring falls more than X below the // best computed score thus far, then we don't consider // additional extensions for that alignment. Should // be >= 0 or -1 for infinity. // S'(i+j, d) = (i+j)*mat/2 - d*(mat-mis) // or S'(k, d) = k*mat/2 - d*(mat-mis) // // Returning: // T' = Composite similarity score // // Testing has also been done and it has been proven that the order of // the two arrays has no effect on the outcome. // // In the following it will be assumed that the number of mismatches // (i.e. array bounds) can't exceed M+N since the absolute maximum // edit script to go from an array of M objects to an array of N // objects is to perform M deletions and N insertions. However, // these differences can be tracked either forward or backward, so // the memory will be allocated for the full search field. // // We are also going to define -oo as being -2 since no index can be // lower than 0. The reason for the -2 instead of -1 is to allow // for the i=R(d,k)+1 calculations to still be below 0. That is // to say so that -oo + 1 = -oo // CStringArray &a = zFunc1; CStringArray &b = zFunc2; double Tp; // T' = Overall max for entire comparison double Tpp; // T'' = Overall max for current d value double *T; double nTemp; int **Rm; int *Rvisitmin; // Minimum k-index of R visited for a particular d (for speed) int *Rvisitmax; // Maximum k-index of R visited for a particular k (for speed) int i, j, k, L, U; int d, dp; int dbest, kbest; int M = a.GetSize(); int N = b.GetSize(); const int dmax = ((M+N)*2)+1; const int kmax = (M+N+1); const int rksize = (kmax*2)+1; const double mat = 2; const double mis = -2; const double X = -1; const int floored_d_offset = (int)((X+(mat/2))/(mat-mis)); #define Sp(x, y) ((double)((x)*(mat/2) - ((y)*(mat-mis)))) #define R(x, y) (Rm[(x)][(y)+kmax]) // Allocate Memory: T = new double[dmax]; if (T == NULL) { AfxThrowMemoryException(); return nRetVal; } Rvisitmin = new int[dmax]; if (Rvisitmin == NULL) { AfxThrowMemoryException(); delete T; return nRetVal; } Rvisitmax = new int[dmax]; if (Rvisitmax == NULL) { AfxThrowMemoryException(); delete T; delete Rvisitmin; return nRetVal; } Rm = new int*[dmax]; if (Rm == NULL) { AfxThrowMemoryException(); delete T; delete Rvisitmin; delete Rvisitmax; return nRetVal; } for (i=0; i<dmax; i++) { Rm[i] = new int[rksize]; if (Rm[i] == NULL) AfxThrowMemoryException(); } // Initialize: for (i=0; i<dmax; i++) { T[i] = 0; Rvisitmin[i] = kmax+1; Rvisitmax[i] = -kmax-1; for (j=0; j<rksize; j++) { Rm[i][j] = -2; } } // Algorithm: i=0; // TODO : Improve A/B Comparison: while ((i<__min(M, N)) && (a.GetAt(i).CompareNoCase(b.GetAt(i)) == 0)) i++; R(0, 0) = i; dbest = kbest = 0; Tp = T[0] = Sp(i+i, 0); d = L = U = 0; Rvisitmin[0] = 0; Rvisitmax[0] = 0; /* printf("\n"); */ if ((i != M) || (i != N)) { do { d++; dp = d - floored_d_offset - 1; Tpp = -DBL_MAX; for (k=(L-1); k<=(U+1); k++) { ASSERT(d > 0); ASSERT(d < dmax); ASSERT(abs(k) <= kmax); i = -2; if (L < k) i = __max(i, R(d-1, k-1)+1); if ((L <= k) && (k <= U)) i = __max(i, R(d-1, k)+1); if (k < U) i = __max(i, R(d-1, k+1)); j = i - k; if ((i >= 0) && (j >= 0) && ((X<0) || (Sp(i+j, d) >= (((dp >= 0) ? T[dp] : 0) - X)))) { // TODO : Improve A/B Comparison: while ((i<M) && (j<N) && (a.GetAt(i).CompareNoCase(b.GetAt(j)) == 0)) { i++; j++; } R(d, k) = i; if (Rvisitmin[d] > k) Rvisitmin[d] = k; if (Rvisitmax[d] < k) Rvisitmax[d] = k; nTemp = Sp(i+j, d); Tp = __max(Tp, nTemp); /* printf("d=%2ld : k=%2ld, i=%2ld, j=%2ld, M=%2ld, N=%2ld, T=%2ld, Tp=%2ld, Tpp=%2ld", d, k, i, j, M, N, (int)nTemp, (int)Tp, (int)Tpp); */ if (nTemp > Tpp) { Tpp = nTemp; /* printf(" * Best (%2ld)", (int)Tpp); */ dbest = d; kbest = k; // Account for hitting the max M or N boundaries: if ((i != M) || (j != N)) { if (j > N) { kbest++; /* printf(" >>>>>> k++ j shift"); */ } else { if (i > M) { kbest--; /* printf(" <<<<<< k-- i shift"); */ } } } } /* printf("\n"); */ } else { R(d, k) = -2; if (Rvisitmin[d] == k) Rvisitmin[d]++; if (Rvisitmax[d] >= k) Rvisitmax[d] = k-1; } } T[d] = Tp; L = Rvisitmin[d]; U = Rvisitmax[d]; for (k=Rvisitmax[d]; k>=Rvisitmin[d]; k--) if (R(d, k) == (N+k)) break; if (k<Rvisitmin[d]) k = INT_MIN; L = __max(L, k+1); for (k=Rvisitmin[d]; k<=Rvisitmax[d]; k++) if (R(d, k) == M) break; if (k>Rvisitmax[d]) k = INT_MAX; U = __min(U, k-1); } while (L <= U+2); } // If the two PrimaryLabels at the function's address don't match, decrement match by 1*mat. // This helps if there are two are more functions that the user has labeled that // are identical except for the labels: if (pFile1->GetPrimaryLabel(pFunction1->GetMainAddress()).CompareNoCase( pFile2->GetPrimaryLabel(pFunction2->GetMainAddress())) != 0) Tp = __max(0, Tp - mat); // Normalize it: nRetVal = Tp/(__max(M,N)*mat); // Build Edit Script: if (bBuildEditScript) { int last_i, last_j; int cur_i, cur_j; int k_rest; if (dbest > 0) { m_EditScript.SetSize(dbest); k = kbest; last_i = M+1; last_j = N+1; /* printf("\n%s with %s:\n", LPCTSTR(pFunction1->GetMainName()), LPCTSTR(pFunction2->GetMainName())); */ for (d=dbest-1; d>=0; d--) { i = __max((R(d, k-1) + 1), __max((R(d, k) + 1), (R(d, k+1)))); /* printf("(%3ld, %3ld) : %3ld(%5ld), %3ld(%5ld), %3ld(%5ld) :", d, k, (R(d, k-1) + 1), (int)Sp((R(d, k-1))*2-k+1, d), (R(d, k) + 1), (int)Sp((R(d, k))*2-k, d), (R(d, k+1)), (int)Sp((R(d, k+1))*2-k-1, d)); for (j=Rvisitmin[dbest-1]; j<=Rvisitmax[dbest-1]; j++) { if (j == k-1) printf("("); else printf(" "); if (R(d,j)<0) printf(" "); else printf("%3ld", R(d, j)); if (j == k+1) printf(")"); else printf(" "); } printf("\n"); */ j = i-k; if (i == (R(d, k-1) + 1)) { strTemp.Format("%ld>%ld", i-1, j); cur_i = i-1; cur_j = j; k--; k_rest = 1; } else { if (i == (R(d, k+1))) { strTemp.Format("%ld<%ld", i, j-1); cur_i = i; cur_j = j-1; k++; k_rest = -1; } else { // if (i == (R(d, k) + 1)) strTemp.Format("%ld-%ld", i-1, j-1); cur_i = i-1; cur_j = j-1; // k=k; k_rest = 0; } } m_EditScript.SetAt(d, strTemp); // The following test is needed since our insertion/deletion indexes are // one greater than the stored i and/or j values from the R matrix. // It is possible that the previous comparison added some extra // entries to the R matrix than what was really needed. This will // cause extra erroneous entries to appear in the edit script. // However, since the indexes should be always increasing, we simply // filter out extra entries added to the end that don't satisfy // this condition: if ((k_rest == 0) && ((cur_i == last_i) && (cur_j == last_j))) { m_EditScript.RemoveAt(d); } last_i = cur_i; last_j = cur_j; } } // Note: if the two are identical, array stays empty: m_bEditScriptValid = TRUE; } // Deallocate Memory: delete[] T; delete[] Rvisitmin; delete[] Rvisitmax; for (i=0; i<dmax; i++) delete[] (Rm[i]); delete[] Rm; } break; } return nRetVal; }