cln::cl_N HighPrecisionComplexPolynom::EvalPoly(cln::cl_N* Poly, int Maxpow, cln::cl_N Valu) { int pow; cln::cl_N xpow, sum; sum = As(cln::cl_N)(complex(ZERO,ZERO)); xpow = As(cln::cl_N)(complex(ONE,ZERO)); for(pow = 0; pow < Maxpow+1; pow++) { sum = sum+xpow*Poly[pow]; xpow = xpow*Valu; } return sum; }
ControlledLeds newControlledLed(Led led) { struct LedGroup group = (struct LedGroup){ &led, 1 }; return newControlledLedGroup(&group); } ControlledLeds newControlledLedGroup(LedGroup group) { if (group == NULL) return Invalid(ControlledLeds); _ControlledLeds leds = kalloc(sizeof(struct _ControlledLeds)); if (!leds) return Invalid(ControlledLeds); LedList *underlying = addNewLedsToList(group); if (!underlying) { free(leds); return Invalid(ControlledLeds); } leds->mask = 0; leds->leds = underlying; leds->count = group->count; leds->next = NULL; ControlledLeds result = As(ControlledLeds, leds); controlLeds(result, LedsDisabled); LL_APPEND(controlled_leds, leds); return result; } static void cleanUnderlyingLeds(ControlledLeds leds) { LedList elem = NULL, tmp = NULL; LL_FOREACH_SAFE(underlying_leds, elem, tmp) { for (int i = 0; i < LEDS->count; i++) { if (elem == LEDS->leds[i]) { reduceRefcount(elem); } } } }
static ProcessBase initializeProcessInternal(void *stackPointer) { PCB process = kalloc(sizeof(struct PCB)); if (!process) return Invalid(ProcessBase); process->stackPointer = stackPointer; process->extra = NULL; return As(ProcessBase, process); }
Button newButton(Pin pin, ButtonType flags) { _Button button = kalloc(sizeof(struct _Button)); if (!button) return Invalid(Button); if (!occupyPin(pin, PinButtonInput)) { free(button); return Invalid(Button); } button->pin = pin; button->flags = flags; button->status = 0; initButton(pin, flags); return As(Button, button); }
ProcessBase createProcessBase(ProcessEntryPoint entryPoint, void *processArgument, uint16_t stackSize) { MockProcess process = malloc(sizeof(struct MockProcess)); if (!process) return Invalid(ProcessBase); process->entryPoint = entryPoint; process->processArgument = processArgument; process->stackSize = stackSize; process->destroyed = FALSE; process->extra = NULL; process->scheduled = 0; process->stackTop = malloc(stackSize); process->stackPointer = process->stackTop - INITIAL_STACK_SIZE; return As(ProcessBase, process); }
void VerifyBackedByImageBrush( const ComPtr<ICanvasBrushInternal>& brushInternal, ComPtr<ID2D1Image>* outTarget = nullptr) // Optionally retrieve the target image { auto d2dBrush = brushInternal->GetD2DBrush(); ComPtr<ID2D1ImageBrush> imageBrush; ThrowIfFailed(d2dBrush.As(&imageBrush)); if (outTarget) { ComPtr<ID2D1Image> target; imageBrush->GetImage(&target); *outTarget = target; } }
void VerifyBackedByBitmapBrush( const ComPtr<ICanvasBrushInternal>& brushInternal, ComPtr<ID2D1Image>* outTarget = nullptr) // Optionally retrieve the target bitmap { auto d2dBrush = brushInternal->GetD2DBrush(); ComPtr<ID2D1BitmapBrush1> bitmapBrush; ThrowIfFailed(d2dBrush.As(&bitmapBrush)); if (outTarget) { ComPtr<ID2D1Bitmap> target; bitmapBrush->GetBitmap(&target); *outTarget = target; } }
/** * This describes the basic use of Eigen::AutoDiffScalar */ void basic_use_autodiff_scalar(){ std::cout << "== basic_use_autodiff_scalar() ==" << std::endl; typedef Eigen::AutoDiffScalar<Eigen::VectorXd> AScalar; // AScalar stores a scalar and a derivative vector. // Instantiate an AutoDiffScalar variable with a normal Scalar double s = 0.3; AScalar As(s); // Get the value from the Instance std::cout << "value: " << As.value() << std::endl; // The derivative vector As.derivatives(); // gives you a reference of // the contained derivative vector // Resize the derivative vector As.derivatives().resize(2); /** * Important note: * All ActiveScalars which are used in one computation must have * either a common derivative vector length or a zero-length * derivative vector. */ // Set the initial derivative vector As.derivatives() = Eigen::VectorXd::Unit(2,0); std::cout << "Derivative vector : " << As.derivatives().transpose() << std::endl; // Instantiate another AScalar AScalar Ab(4); Ab.derivatives() = Eigen::VectorXd::Unit(2,1); // Do the most simple calculation AScalar Ac = As * Ab; std::cout << "Result/Ac.value()" << Ac.value() << std::endl; std::cout << "Gradient: " << Ac.derivatives().transpose() << std::endl; }
int main(void) { const int N = 101; mcon::Vector<double> As(N); for ( int A = 0; A < N; ++A ) { double alpha = 0.0; if ( A <= 21 ) { } else if ( A < 50 ) { alpha = 0.5842 * pow( A-21 , 0.4) + 0.07886 * (A-21); } else { alpha = 0.1102 * (A-8.7); } As[A] = alpha; } mfio::Csv::Write("Kaiser.csv", As); return 0; }
int main(int argc, char** argv) { int rv = MPI_Init(&argc, &argv); assert(rv == MPI_SUCCESS); int size; rv = MPI_Comm_size(MPI_COMM_WORLD, &size); assert(rv == MPI_SUCCESS); int rank; rv = MPI_Comm_rank(MPI_COMM_WORLD, &rank); assert(rv == MPI_SUCCESS); MPI_Status status; assert(size == 2); assert(M % 2 == 0); double start_time = MPI_Wtime(); std::vector<Value> A(M + 1); std::vector<Value> B(M + 1); std::vector<Value> C(M + 1); std::vector<Value> F(M + 1); A[0] = Value(0.0, 0.0); // Not used. for (int m = 1; m <= M - 1; m++) { A[m] = Value(d / h / h, D / h / h); } A[M] = Value(1.0, 1.0); B[0] = Value(1.0, 1.0); for (int m = 1; m <= M - 1; m++) { B[m] = Value(-2.0 * d / h / h - 1.0 / tau, -2.0 * D / h / h - 1.0 / tau); } B[M] = Value(-1.0, -1.0); C[0] = Value(-1.0, -1.0); for (int m = 1; m <= M - 1; m++) { C[m] = Value(d / h / h, D / h / h); } C[M] = Value(0.0, 0.0); // Not used. std::vector<Value> As(M + 1); std::vector<Value> Bs(M + 1); std::vector<Value> Cs(M + 1); std::vector<Value> Fs(M + 1); As[0] = Value(0.0, 0.0); for (int m = 1; m <= M; m++) { As[m] = -A[m] * A[m - 1] / B[m - 1]; } Bs[0] = B[0] - C[0] * A[1] / B[1]; for (int m = 1; m <= M - 1; m++) { Bs[m] = B[m] - A[m] * C[m - 1] / B[m - 1] - C[m] * A[m + 1] / B[m + 1]; } Bs[M] = B[M] - A[M] * C[M - 1] / B[M - 1]; for (int m = 0; m <= M - 1; m++) { Cs[m] = -C[m] * C[m + 1] / B[m + 1]; } Cs[M] = Value(0.0, 0.0); std::vector<Value> prev_values(M + 1); std::vector<Value> curr_values(M + 1); InitializeValues(curr_values); std::vector<Value> P(M + 1); std::vector<Value> Q(M + 1); for (int n = 0; n < N; n++) { prev_values.swap(curr_values); F[0] = Value(0.0, 0.0); for (int m = 1; m <= M - 1; m++) { if (m % 2 == rank) { F[m].u = -prev_values[m].u / tau - f(prev_values[m]); F[m].v = -prev_values[m].v / tau - g(prev_values[m]); } } F[M] = Value(0.0, 0.0); for (int m = 1; m <= M - 1; m++) { if (m % 2 == rank) { rv = MPI_Send(&F[m].u, 1, MPI_DOUBLE, (rank + 1) % 2, m, MPI_COMM_WORLD); assert(rv == MPI_SUCCESS); rv = MPI_Send(&F[m].v, 1, MPI_DOUBLE, (rank + 1) % 2, m, MPI_COMM_WORLD); assert(rv == MPI_SUCCESS); } else { rv = MPI_Recv(&F[m].u, 1, MPI_DOUBLE, (rank + 1) % 2, m, MPI_COMM_WORLD, &status); assert(rv == MPI_SUCCESS); rv = MPI_Recv(&F[m].v, 1, MPI_DOUBLE, (rank + 1) % 2, m, MPI_COMM_WORLD, &status); assert(rv == MPI_SUCCESS); } } Fs[0] = F[0] - C[0] / B[1] * F[1]; for (int m = 1; m <= M - 1; m++) { if (m % 2 == rank) { Fs[m] = F[m] - A[m] / B[m - 1] * F[m - 1] - C[m] / B[m + 1] * F[m + 1]; } } Fs[M] = F[M] - A[M] / B[M - 1] * F[M - 1]; if (rank == 0) { P[0] = -Cs[0] / Bs[0]; Q[0] = Fs[0] / Bs[0]; } else { P[1] = -Cs[1] / Bs[1]; Q[1] = Fs[1] / Bs[1]; } for (int m = 2; m <= M; m++) { if (m % 2 == rank) { P[m] = -Cs[m] / (As[m] * P[m - 2] + Bs[m]); Q[m] = (Fs[m] - As[m] * Q[m - 2]) / (As[m] * P[m - 2] + Bs[m]); } } if (M % 2 == rank) { curr_values[M] = Q[M]; } else { curr_values[M - 1] = Q[M - 1]; } for (int m = M - 2; m >= 0; m--) { if (m % 2 == rank) { curr_values[m] = P[m] * curr_values[m + 2] + Q[m]; } } } for (int m = 0; m <= M; m++) { if (m % 2 == rank) { rv = MPI_Send(&curr_values[m].u, 1, MPI_DOUBLE, (rank + 1) % 2, m, MPI_COMM_WORLD); assert(rv == MPI_SUCCESS); rv = MPI_Send(&curr_values[m].v, 1, MPI_DOUBLE, (rank + 1) % 2, m, MPI_COMM_WORLD); assert(rv == MPI_SUCCESS); } else { rv = MPI_Recv(&curr_values[m].u, 1, MPI_DOUBLE, (rank + 1) % 2, m, MPI_COMM_WORLD, &status); assert(rv == MPI_SUCCESS); rv = MPI_Recv(&curr_values[m].v, 1, MPI_DOUBLE, (rank + 1) % 2, m, MPI_COMM_WORLD, &status); assert(rv == MPI_SUCCESS); } } if (rank == 0) { printf("%lf\n", MPI_Wtime() - start_time); /* for (int m = 0; m < M; m++) { double coord = X * m / M; printf("%lf %lf %lf\n", coord, curr_values[m].u, curr_values[m].v); } */ } rv = MPI_Finalize(); assert(rv == MPI_SUCCESS); return EXIT_SUCCESS; }
// // Find the complex roots of a complex polynomial Poly // The order is Maxpow // The result will be in the array Root // void HighPrecisionComplexPolynom::Polyrootc(cln::cl_N* Poly, int Maxpow, cln::cl_N* Root) { int ord, pow, fnd, pov, maxp; cln::cl_N poly[1+Maxpow], polc[1+Maxpow], coef[1+Maxpow], coen[1+Maxpow]; // Put coefficients in an array for(pow = 0; pow < Maxpow+1; pow++) { poly[pow] = As(cln::cl_N)(Poly[pow]); coef[pow] = As(cln::cl_N)(Poly[pow]); } for(pow = 0; pow < Maxpow+1; pow++) { polc[pow] = As(cln::cl_N)(complex(ZERO,ZERO)); } polc[0] = As(cln::cl_N)(complex(ONE,ZERO)); fnd = -1; // Loop for finding all roots for(ord = 0; ord < Maxpow; ord++) { fnd++; pov = Maxpow-fnd; if(fnd < Maxpow) { if(LogLevel>4) { printf(" root number: %d\n",fnd+1); } if((ord%2 == 1) && (Maxpow%2 == 0) && (false)) { Root[fnd] = As(cln::cl_N)(conjugate(Root[fnd-1])); cln::cl_N val0 = EvalPoly(poly, Maxpow, Root[fnd]); if(LogLevel>3) { printf("root = %f +i*%f\n", double_approx(realpart(Root[fnd])), double_approx(imagpart(Root[fnd]))); printf("value at root: %f +i*%f\n", double_approx(realpart(val0)), double_approx(imagpart(val0))); } } else { Root[fnd] = Lasolv(poly,pov, complex(ONE,ONE), 150); } for(pow = Maxpow; pow > 0; pow--) { polc[pow] = polc[pow-1]-Root[fnd]*polc[pow]; } polc[0] = -Root[fnd]*polc[pow]; // Divide the polynomial by the root maxp = Maxpow-fnd-1; coen[maxp] = coef[maxp+1]; for(pow = maxp-1; pow > -1; pow--) { coen[pow] = coef[pow+1]+Root[fnd]*coen[pow+1]; } for(pow = 0; pow < maxp+1; pow++) { coef[pow] = coen[pow]; poly[pow] = coef[pow]; } } else { break; } } // Compare input with product of root factors for(pow = 0; pow < Maxpow+1; pow++) { polc[pow] = Poly[pow]-poly[0]*polc[pow]; } if(LogLevel>4) { printf("control polynomial should be close to zero:\n"); for(pow = 0; pow < Maxpow+1; pow++) { printf(" x^{%d}\n",pow); printf("%1.15f +i*%1.15f\n",double_approx(realpart(polc[pow])), double_approx(imagpart(polc[pow]))); } } }
// // Find a root of a complex polynomial by Laguerre iteration. // // The polynomial is Poly // The order is Maxpow // // The precision: Digit cln::cl_N HighPrecisionComplexPolynom::Lasolv(cln::cl_N* Poly, int Maxpow, cln::cl_N root, int itemax) { int pow, ite; root = complex(ZERO,ZERO); cln::cl_F angl, small = As(cln::cl_F)(expt(cln::cl_float(0.1,clnDIGIT),DIGIT/2)); cln::cl_N dif1[Maxpow], dif2[Maxpow-1]; cln::cl_N val0, val, val1, val2, denp, denm, las1, las2, sqrv; // cln::cl_N root; for(pow = 0; pow < Maxpow; pow++) dif1[pow] = (pow+1)*Poly[pow+1]; for(pow = 0; pow < Maxpow-1; pow++) dif2[pow] = (pow+1)*dif1[pow+1]; // The maximal allowed number of iterations is set here; // this can be chosen larger, but 100 usually suffices // root = As(cln::cl_N)(complex(ZERO,ZERO)); val0 = EvalPoly(Poly,Maxpow,root); // Iteration for(ite = 0; ite < itemax; ite++) { val = val0; val1 = EvalPoly(dif1,Maxpow-1,root); val2 = EvalPoly(dif2,Maxpow-2,root); sqrv = (Maxpow-1)*((Maxpow-1)*val1*val1-Maxpow*val0*val2); angl = HALF*cln::cl_float(phase(sqrv),clnDIGIT); sqrv = sqrt(abs(sqrv))*complex(cos(angl),sin(angl)); denp = val1+sqrv; denm = val1-sqrv; if(denp == complex(ZERO,ZERO)) root = root-Maxpow*val0/denm; else { if(denm == complex(ZERO,ZERO)) root = root-Maxpow*val0/denp; else { las1 = -Maxpow*val0/denp; las2 = -Maxpow*val0/denm; if(realpart(las1*conjugate(las1)) < realpart(las2*conjugate(las2))) root = root+las1; else root = root+las2; } } // Look whether the root is good enough val0 = EvalPoly(Poly,Maxpow,root); if(abs(val0) == ZERO || (abs(val0) < small) && abs(val0/val) > 0.7) { if(LogLevel>4) { printf("Laguerre iterations: %d\n", ite); printf("root = %f +i* %f\n", double_approx(realpart(root)), double_approx(imagpart(root))); printf("value at root: %f +i* %f\n", double_approx(realpart(val0)), double_approx(imagpart(val0))); } break; } } if(ite >= itemax) { printf("Laguerre iteration did not converge\n"); exit(5); } return root; }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // void scalarGeneralExchange::manipulateScalarField(volScalarField& explicitEulerSource, volScalarField& implicitEulerSource, int speciesID) const { // reset Scalar field explicitEulerSource.internalField() = 0.0; implicitEulerSource.internalField() = 0.0; if(speciesID>=0 && particleSpeciesValue_[speciesID]<0.0) //skip if species is not active return; //Set the names of the exchange fields word fieldName; word partDatName; word partFluxName; word partTransCoeffName; word partFluidName; scalar transportParameter; // realloc the arrays to pull particle data if(speciesID<0) //this is the temperature - always pull from LIGGGHTS { fieldName = tempFieldName_; partDatName = partTempName_; partFluxName = partHeatFluxName_; partTransCoeffName = partHeatTransCoeffName_; partFluidName = partHeatFluidName_; transportParameter = lambda_; allocateMyArrays(0.0); particleCloud_.dataExchangeM().getData(partDatName,"scalar-atom", partDat_); } else { fieldName = eulerianFieldNames_[speciesID]; partDatName = partSpeciesNames_[speciesID]; partFluxName = partSpeciesFluxNames_[speciesID]; partTransCoeffName = partSpeciesTransCoeffNames_[speciesID]; partFluidName = partSpeciesFluidNames_[speciesID]; transportParameter = DMolecular_[speciesID]; allocateMyArrays(0.0); if(particleSpeciesValue_[speciesID]>ALARGECONCENTRATION) particleCloud_.dataExchangeM().getData(partDatName,"scalar-atom", partDat_); } if (scaleDia_ > 1) Info << typeName << " using scale = " << scaleDia_ << endl; else if (particleCloud_.cg() > 1) { scaleDia_=particleCloud_.cg(); Info << typeName << " using scale from liggghts cg = " << scaleDia_ << endl; } //============================== // get references const volScalarField& voidfraction_(particleCloud_.mesh().lookupObject<volScalarField> (voidfractionFieldName_)); // ref to voidfraction field const volVectorField& U_(particleCloud_.mesh().lookupObject<volVectorField> (velFieldName_)); const volScalarField& fluidScalarField_(particleCloud_.mesh().lookupObject<volScalarField> (fieldName)); // ref to scalar field const volScalarField& nufField = forceSubM(0).nuField(); //============================== // calc La based heat flux vector position(0,0,0); scalar voidfraction(1); vector Ufluid(0,0,0); scalar fluidValue(0); label cellI=0; vector Us(0,0,0); vector Ur(0,0,0); scalar dscaled(0); scalar nuf(0); scalar magUr(0); scalar As(0); scalar Rep(0); scalar Pr(0); scalar sDth(scaleDia_*scaleDia_*scaleDia_); interpolationCellPoint<scalar> voidfractionInterpolator_(voidfraction_); interpolationCellPoint<vector> UInterpolator_(U_); interpolationCellPoint<scalar> fluidScalarFieldInterpolator_(fluidScalarField_); #include "setupProbeModel.H" for(int index = 0;index < particleCloud_.numberOfParticles(); ++index) { cellI = particleCloud_.cellIDs()[index][0]; if(cellI >= 0) { if(forceSubM(0).interpolation()) { position = particleCloud_.position(index); voidfraction = voidfractionInterpolator_.interpolate(position,cellI); Ufluid = UInterpolator_.interpolate(position,cellI); fluidValue = fluidScalarFieldInterpolator_.interpolate(position,cellI); }else { voidfraction = voidfraction_[cellI]; Ufluid = U_[cellI]; fluidValue = fluidScalarField_[cellI]; } // calc relative velocity Us = particleCloud_.velocity(index); Ur = Ufluid-Us; magUr = mag(Ur); dscaled = 2*particleCloud_.radius(index)/scaleDia_; As = dscaled*dscaled*M_PI*sDth; nuf = nufField[cellI]; Rep = dscaled*magUr/nuf; if(speciesID<0) //have temperature Pr = Prandtl_; else Pr = max(SMALL,nuf/transportParameter); //This is Sc for species scalar alpha = transportParameter*(this->*Nusselt)(Rep,Pr,voidfraction)/(dscaled); // calc convective heat flux [W] scalar areaTimesTransferCoefficient = alpha * As; scalar tmpPartFlux = areaTimesTransferCoefficient * (fluidValue - partDat_[index][0]); partDatFlux_[index][0] = tmpPartFlux; // split implicit/explicit contribution forceSubM(0).explicitCorrScalar( partDatTmpImpl_[index][0], partDatTmpExpl_[index][0], areaTimesTransferCoefficient, fluidValue, fluidScalarField_[cellI], partDat_[index][0], forceSubM(0).verbose() ); if(validPartTransCoeff_) partDatTransCoeff_[index][0] = alpha; if(validPartFluid_) partDatFluid_[index][0] = fluidValue; if( forceSubM(0).verbose()) { Pout << "fieldName = " << fieldName << endl; Pout << "partTransCoeffName = " << partTransCoeffName << endl; Pout << "index = " <<index << endl; Pout << "partFlux = " << tmpPartFlux << endl; Pout << "magUr = " << magUr << endl; Pout << "As = " << As << endl; Pout << "r = " << particleCloud_.radius(index) << endl; Pout << "dscaled = " << dscaled << endl; Pout << "nuf = " << nuf << endl; Pout << "Rep = " << Rep << endl; Pout << "Pr/Sc = " << Pr << endl; Pout << "Nup/Shp = " << (this->*Nusselt)(Rep,Pr,voidfraction) << endl; Pout << "partDatTransCoeff: " << partDatTransCoeff_[index][0] << endl; Pout << "voidfraction = " << voidfraction << endl; Pout << "partDat_[index][0] = " << partDat_[index][0] << endl ; Pout << "fluidValue = " << fluidValue << endl ; } //Set value fields and write the probe if(probeIt_) { #include "setupProbeModelfields.H" vValues.append(Ur); sValues.append((this->*Nusselt)(Rep,Pr,voidfraction)); sValues.append(Rep); particleCloud_.probeM().writeProbe(index, sValues, vValues); } } } //Handle explicit and implicit source terms on the Euler side //these are simple summations! particleCloud_.averagingM().setScalarSum ( explicitEulerSource, partDatTmpExpl_, particleCloud_.particleWeights(), NULL ); particleCloud_.averagingM().setScalarSum ( implicitEulerSource, partDatTmpImpl_, particleCloud_.particleWeights(), NULL ); // scale with the cell volume to get (total) volume-specific source explicitEulerSource.internalField() /= -explicitEulerSource.mesh().V(); implicitEulerSource.internalField() /= -implicitEulerSource.mesh().V(); // limit explicit source term scalar explicitEulerSourceInCell; forAll(explicitEulerSource,cellI) { explicitEulerSourceInCell = explicitEulerSource[cellI]; if(mag(explicitEulerSourceInCell) > maxSource_ ) { explicitEulerSource[cellI] = sign(explicitEulerSourceInCell) * maxSource_; } }