/// Calculate emission time for a given detector (L1, t2) /// and TOF when Emode==Elastic double ModeratorTzero::CalculateT0elastic(const double &tof, const double &L12, double &E1, mu::Parser &parser) { double t0_curr, t0_next; t0_curr = m_tolTOF; // current iteration emission time t0_next = 0.0; // next iteration emission time, initialized to zero size_t iiter(0); // current iteration number // iterate until convergence in t0 reached while (std::fabs(t0_curr - t0_next) >= m_tolTOF && iiter < m_niter) { t0_curr = t0_next; double v1 = L12 / (tof - t0_curr); // v1 = v2 = v since emode is elastic E1 = m_convfactor * v1 * v1; // Energy in meV if v1 in meter/microsecond t0_next = parser.Eval(); iiter++; } return t0_next; }
Handle_ptr LambdaCommand::execute( Context &ctx, Environment *env, Handle_ptr expr) { MCAssertValidInstance(); assert( 0 != env && 0 != ctx.eval && 0 != expr); Handle_ptr args = expr->cdr(); if (!isList( args->car())) { throw TypeException( "list", __FILE__, __LINE__); } // collect argument names and check if argument specification is valid std::list<Handle_ptr> argumentList; if (!eq( args->car(), ctx.NIL)) { std::back_insert_iterator<std::list<Handle_ptr> > iiter( argumentList); copyList( args->car(), iiter); std::list<Handle_ptr>::iterator pos; for (pos=argumentList.begin(); pos!=argumentList.end(); ++pos) { if (!(*pos)->hasType( Handle::ntSYMBOL)) { throw TypeException( "symbol", __FILE__, __LINE__); } } } else { // empty argument list std::cerr << "lambda: empty argument list" << std::endl; } #if defined( DEBUG) && DEBUG > 3 std::cerr << "create closure with " << argumentList.size() << " arguments." << std::endl; std::cerr << " body: "; printList( args->cdr(), std::cerr); std::cerr << std::endl; std::cerr << " args: "; std::list<Handle_ptr>::iterator pos; for (pos=argumentList.begin(); pos!=argumentList.end(); ++pos) std::cerr << *(*pos) << ' '; std::cerr << std::endl; #endif return ctx.factory->makeClosure( argumentList, env, args->cdr()); }
void IntensityForce::ComputeIntensityForce(ParticleSystem* system) { ParticleSubjectArray& shapes = system->GetSubjects(); const ParticleSubject& meanSubject = system->GetMeanSubject(); const int nSubj = shapes.size(); const int nPoints = shapes[0].GetNumberOfPoints(); const int nRadius = 2; const int nAttrsPerPoint = ::pow(2*nRadius+1, __Dim); const int nAttrs = nPoints * nAttrsPerPoint; DoubleImageVector warpedImages(nSubj); VectorImageVector gradImages(nSubj); double* attrs = new double[nSubj * nAttrs]; double* gradAttrs = new double[nSubj * nAttrs * __Dim]; double* force = new double[nSubj * nPoints * __Dim]; // first create a warped image into the mean transform space // second create a gradient vector image per subject // third extract attributes (features) for (int i = 0; i < nSubj; i++) { ParticleSubject& subject = shapes[i]; ParticleBSpline bspline; bspline.SetReferenceImage(m_ImageContext->GetLabel(i)); bspline.EstimateTransform(meanSubject, subject); FieldTransformType::Pointer fieldTransform = bspline.GetTransform(); CompositeTransformType::Pointer transform = CompositeTransformType::New(); transform->AddTransform(fieldTransform); subject.m_InverseTransform = transform; warpedImages[i] = bspline.WarpImage(m_ImageContext->GetDoubleImage(i)); GradientFilterType::Pointer grad = GradientFilterType::New(); grad->SetInput(warpedImages[i]); grad->Update(); gradImages[i] = grad->GetOutput(); // extract attributes DoubleImage::SizeType radius; radius.Fill(nRadius); DoubleImageNeighborhoodIteratorType iiter(radius, warpedImages[i], warpedImages[i]->GetBufferedRegion()); VectorImageNeighborhoodIteratorType giter(radius, gradImages[i], gradImages[i]->GetBufferedRegion()); DoubleImage::IndexType idx; #pragma omp parallel for for (int j = 0; j < nPoints; j++) { Particle& par = subject.m_Particles[j]; fordim(k) { idx[k] = round(par.y[k]); } iiter.SetLocation(idx); giter.SetLocation(idx); double* jAttrs = &attrs[i * nAttrs + j * nAttrsPerPoint]; double* jAttrsGrad = &gradAttrs[(i * nAttrs + j * nAttrsPerPoint) * __Dim]; const int numAttrs = iiter.Size(); assert(numAttrs == nAttrsPerPoint); for (int k = 0; k < nAttrsPerPoint; k++) { double pixel = iiter.GetPixel(k); VectorType grad = giter.GetPixel(k); jAttrs[k] = pixel; for (int m = 0; m < __Dim; m++) { jAttrsGrad[m] = grad[m]; } } } } // column sum double* sumAttrs = new double[nPoints * nAttrsPerPoint]; #pragma omp parallel for for (int j = 0; j < nAttrs; j++) { sumAttrs[j] = 0; for (int i = 0; i < nSubj; i++) { sumAttrs[j] += attrs[i * nAttrs + j]; } } // compute mean differences for (int i = 0; i < nSubj; i++) { #pragma omp parallel for for (int j = 0; j < nAttrs; j++) { attrs[i * nAttrs + j] -= (sumAttrs[j] / nSubj); } } // compute force direction memset(force, 0, nSubj * nPoints * __Dim * sizeof(double)); for (int i = 0; i < nSubj; i++) { #pragma omp parallel for for (int j = 0; j < nPoints; j++) { double* forcePtr = &force[(i * nPoints + j) * __Dim]; for (int k = 0; k < nAttrsPerPoint; k++) { double attr = attrs[i * nAttrs + j * nAttrsPerPoint]; double* gradAttrPtr = &gradAttrs[(i * nAttrs + j * nAttrsPerPoint + k) * __Dim]; for (int m = 0; m < __Dim; m++) { forcePtr[m] += attr * gradAttrPtr[m]; } } } } // compute force at subject space for (int i = 0; i < nSubj; i++) { CompositeTransformType::Pointer transform = shapes[i].m_InverseTransform; for (int j = 0; j < nPoints; j++) { double* forcePtr = &force[(i * nPoints + j) * __Dim]; CompositeTransformType::InputPointType x; fordim(k) { x[k] = shapes[i][j].x[k]; } CompositeTransformType::JacobianType jac; jac.set_size(__Dim, __Dim); transform->ComputeInverseJacobianWithRespectToPosition(x, jac); fordim(k) { double ff = 0; if (__Dim == 3) { ff = jac[0][k]*forcePtr[k] + jac[1][k]*forcePtr[k] + jac[2][k]*forcePtr[k]; } else if (__Dim == 2) { ff = jac[0][k]*forcePtr[k] + jac[1][k]*forcePtr[k]; } forcePtr[k] = ff; } } } for (int i = 0; i < nSubj; i++) { ParticleSubject& subj = shapes[i]; for (int j = 0; j < nPoints; j++) { Particle& par = subj[j]; VNLVector ff(__Dim); fordim (k) { const int forceIdx = (i * nPoints + j) * __Dim; ff[k] = force[forceIdx + k]; } ff.normalize(); par.SubForce(ff.data_block(), m_Coeff); } } delete[] force; delete[] attrs; delete[] gradAttrs; }