int main (void) { IloEnv env; try { IloModel model(env); IloNumVarArray var(env); IloRangeArray con(env); populatebyrow (model, var, con); IloCplex cplex(model); IloNumVarArray ordvar(env, 2); IloNumArray ordpri(env, 2); ordvar[0] = var[1]; ordvar[1] = var[3]; ordpri[0] = 8.0; ordpri[1] = 7.0; cplex.setPriorities (ordvar, ordpri); cplex.setDirection(var[1], IloCplex::BranchUp); cplex.setDirection(var[3], IloCplex::BranchDown); cplex.solve(); env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "Solution value = " << cplex.getObjValue() << endl; IloNumArray vals(env); cplex.getValues(vals, var); env.out() << "Values = " << vals << endl; cplex.getSlacks(vals, con); env.out() << "Slacks = " << vals << endl; cplex.exportModel("mipex3.lp"); cplex.writeOrder("mipex3.ord"); } catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; } catch (...) { cerr << "Unknown exception caught" << endl; } env.end(); return 0; } // END main
int main (int argc, char **argv) { IloEnv env; try { IloModel model(env); IloCplex cplex(env); if ( argc != 2 ) { usage (argv[0]); throw(-1); } IloObjective obj; IloNumVarArray var(env); IloRangeArray rng(env); IloSOS1Array sos1(env); IloSOS2Array sos2(env); cplex.importModel(model, argv[1], obj, var, rng, sos1, sos2); cplex.use(SOSbranch(env, sos1)); cplex.setParam(IloCplex::Param::MIP::Strategy::Search, IloCplex::Traditional); cplex.extract(model); cplex.solve(); IloNumArray vals(env); cplex.getValues(vals, var); env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "Solution value = " << cplex.getObjValue() << endl; env.out() << "Values = " << vals << endl; } catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; } catch (...) { cerr << "Unknown exception caught" << endl; } env.end(); return 0; } // END main
auto parse( int argc_, char** argv_, Option option_, Options ... options_ ) { auto parser = make_parser( [=]( std::string token_, char delimiter_, auto triggered_action_, auto silent_action_, auto tag_, auto integer_ ) { for ( int index = 1; index != argc_; ++index ) if ( 0 == strcmp( argv_[index], token_.c_str() ) ) { if ( index+1 == argc_ ) return 1; std::string str_{ argv_[index+1] }; if ( integer_.value-1 != std::count( str_.begin(), str_.end(), delimiter_ ) ) return 1; for( auto& ch : str_ ) ch = ( ch == delimiter_ ) ? ' ' : ch; std::istringstream iss{ str_ }; typedef typename decltype(tag_)::value_type value_type; std::vector<value_type> vals( integer_.value, 0.0 ); std::copy( std::istream_iterator<value_type>( iss ), std::istream_iterator<value_type>(), vals.begin() ); if ( iss.bad() ) return 1; triggered_action_( vals.data() ); return 0; } silent_action_(); return 0; }, [=]( std::string token_, auto triggered_action_, auto silent_action_, auto tag_ ) { for ( int index = 1; index != argc_; ++index ) if ( 0 == strcmp( argv_[index], token_.c_str() ) ) { typedef typename decltype(tag_)::value_type value_type; if ( index+1 == argc_ ) return 1; value_type val; std::istringstream iss{ std::string{ argv_[index+1] } }; iss >> val; if ( iss.bad() ) return 1; triggered_action_( val ); return 0; } silent_action_(); return 0; },
int main (int argc, char **argv) { IloEnv env; try { IloModel model(env, "example"); IloNumVarArray var(env); IloRangeArray rng(env); populatebycolumn (model, var, rng); IloCplex cplex(model); cplex.setOut(env.getNullStream()); cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Primal); cplex.use(MyCallback(env)); cplex.solve(); env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "Solution value = " << cplex.getObjValue() << endl; IloNumArray vals(env); cplex.getValues(vals, var); env.out() << "Values = " << vals << endl; cplex.getSlacks(vals, rng); env.out() << "Slacks = " << vals << endl; cplex.getDuals(vals, rng); env.out() << "Duals = " << vals << endl; cplex.getReducedCosts(vals, var); env.out() << "Reduced Costs = " << vals << endl; cplex.exportModel("lpex4.lp"); } catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; } catch (...) { cerr << "Unknown exception caught" << endl; } env.end(); return 0; } // END main
int main(int argc, char** argv) { cuInit(0); DevicePtr device; CUresult result = CreateCuDevice(0, &device); ContextPtr context; result = CreateCuContext(device, 0, &context); scanEngine_t engine; scanStatus_t status = scanCreateEngine( "../../src/mgpuscan/globalscan.cubin", &engine); int count = 1<< 19; std::vector<int> vals(count); std::tr1::uniform_int<int> r(0, 15); for(int i(0); i < count; ++i) vals[i] = r(mt19937); DeviceMemPtr deviceMem; result = context->MemAlloc(vals, &deviceMem); uint scanTotal; status = scanArray(engine, deviceMem->Handle(), count, &scanTotal, false); std::vector<int> deviceScan; deviceMem->ToHost(deviceScan); std::vector<int> hostScan(count); for(int i(1); i < count; ++i) { hostScan[i] = hostScan[i - 1] + vals[i - 1]; } scanDestroyEngine(engine); bool success = hostScan == deviceScan; if(success) printf("Global scan success.\n"); else printf("Global scan failure.\n"); return 0; }
void test_parallel_for () { { // 1D test // vector of ints, initialized to zero const int length = 1000; std::vector<int> vals (length, 0); // Increment all the integers via parallel_for parallel_for (0, length, [&](uint64_t i){ vals[i] += 1; }); // Verify that all elements are exactly 1 bool all_one = std::all_of (vals.cbegin(), vals.cend(), [&](int i){ return vals[i] == 1; }); OIIO_CHECK_ASSERT (all_one); } }
bool CPlexSolver::solve() { IloCplex cplex(model); cplex.setParam(IloCplex::MIPEmphasis, CPX_MIPEMPHASIS_FEASIBILITY); if (cplex.solve()) { IloNumArray vals(env); cplex.getValues(vals, vars); // Populate solutionVectors from vals SolutionVector solution(vals.getSize()); for (int i = 0; i < vals.getSize(); i++) { // IloNumArray does not have iterators, so we do it the hard way solution[i] = vals[i]; } solutionVectors.push_back(solution); return true; } else { return false; } }
int main (void) { IloEnv env; try { IloModel model(env); // declaration du nodel IloNumVarArray var(env); // liste des variables de decision IloRangeArray con(env); // liste des contraintes a ajouter a notre probleme IloNumVarArray duals(env); populatebyrow (model, var, con); // remplir les variables avec les valeurs adequat IloCplex cplex(model); cplex.solve(); env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "Solution value = " << cplex.getObjValue() << endl; IloNumArray vals(env); cplex.getValues(vals, var); env.out() << "Values = " << vals << endl; cplex.getSlacks(vals, con); env.out() << "Slacks = " << vals << endl; model.add(var[0] == 0); cplex.solve(); cplex.getValues(vals, var); env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "Solution value = " << cplex.getObjValue() << endl; env.out() << "Values = " << vals << endl; //cplex.exportModel("pl.lp"); } catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; } catch (...) { cerr << "Unknown exception caught" << endl; } env.end(); return 0; }
// Wrapper for elem::HermitianGenDefiniteEig sorts vals ascending ElemEigenSystem HermitianGenEigensolver(const TAMatrix &F, const TAMatrix &S){ // Get mats from TA EMatrix EF = TiledArray::array_to_elem(F, elem::DefaultGrid()); // Fock EMatrix ES = TiledArray::array_to_elem(S, elem::DefaultGrid()); // Overlap // Vec and value storage elem::DistMatrix<double , elem::VR, elem::STAR> vals(elem::DefaultGrid()); EMatrix vecs(elem::DefaultGrid()); // Compute vecs and values elem::mpi::Barrier(elem::mpi::COMM_WORLD); // Compute evals evals sc::Timer tim("Solving HermitianGenEig:"); elem::HermitianGenDefiniteEig(elem::AXBX, elem::LOWER,EF,ES,vals,vecs, elem::ASCENDING); elem::mpi::Barrier(elem::mpi::COMM_WORLD); tim.exit("Solving HermitianGenEig:"); return std::make_pair(vals, vecs); }
void compute_sketch_matrix(sketch_t sketch, const DistMatrixType &A, DistMatrixType &result) { std::vector<size_t> row_idx = sketch.getRowIdx(); std::vector<double> row_val = sketch.getRowValues(); // PI generated by random number gen size_t sketch_size = row_val.size(); mpi_vector_t cols(sketch_size); mpi_vector_t rows(sketch_size); mpi_vector_t vals(sketch_size); for(size_t i = 0; i < sketch_size; ++i) { cols.SetElement(i, i); rows.SetElement(i, row_idx[i]); vals.SetElement(i, row_val[i]); } result = DistMatrixType(result.getnrow(), result.getncol(), rows, cols, vals); }
APCHandle::Pair APCArray::MakePacked(ArrayData* arr, bool unserializeObj) { auto num_elems = arr->size(); auto size = sizeof(APCArray) + sizeof(APCHandle*) * num_elems; auto p = malloc(size); auto ret = new (p) APCArray(static_cast<size_t>(num_elems)); try { size_t i = 0; for (ArrayIter it(arr); !it.end(); it.next()) { auto val = APCHandle::Create(it.secondRef(), false, APCHandleLevel::Inner, unserializeObj); size += val.size; ret->vals()[i++] = val.handle; } assert(i == num_elems); } catch (...) { delete ret; throw; } return {ret->getHandle(), size}; }
ILOUSERCUTCALLBACK4( CtCallback, IloNumVarArray, vars, int**, graph, int, num_cuts, int, max_deep ) { if( current_deep > max_deep ){ //cout << "aa " << current_deep << endl; return; } IloNumArray vals(getEnv()); getValues(vals, vars); IntegerFeasibilityArray feas( getEnv() ); getFeasibilities( feas, vars ); int old_win[vals.getSize()]; memset(old_win, 0, sizeof(old_win)); for(int n=0; n<num_cuts; n++){ static int cont=0; int cnt=0; for( int i = 0; i < 2; ++i ){ IloRange cut( getEnv(), 0, 1 ); if( getCut( vals, vars, CLQ2B, i, feas, cut, graph, old_win ) ){ add(cut); cnt++; } else if( getCut( vals, vars, CLQ2A, i, feas, cut, graph, old_win ) ){ cnt++; add(cut); }else{ //se nao encotrar algum corte, sai } cut.end(); } if(n>1 && cnt==0)break; } feas.end(); vals.end(); }
LinearApproximation LinearApproximation::operator-(const LinearApproximation &t_rhs) const { if (m_numVars != t_rhs.m_numVars) { throw std::runtime_error("Unable to subtract, different number of variables"); } LinearApproximation retval(m_numVars); for (const auto & lhsitr : m_values) { for (const auto & rhsitr : t_rhs.m_values) { std::vector<double> vals(m_numVars, 0); bool match = true; for (size_t i = 0; i < m_numVars; ++i) { if ( lhsitr[i] != rhsitr[i] ) { match = false; break; } else { vals[i] = lhsitr[i]; } } if (match) { double value = lhsitr[m_numVars] - rhsitr[m_numVars]; retval.addVals(vals, value); break; } } } return retval; }
static void solveanddisplay (IloEnv env, IloCplex cplex, IloNumVarArray var, IloRangeArray con) { // Optimize the problem and obtain solution. if ( !cplex.solve() ) { env.error() << "Failed to optimize LP" << endl; throw(-1); } IloNumArray vals(env); env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "Solution value = " << cplex.getObjValue() << endl; cplex.getValues(vals, var); env.out() << "Values = " << vals << endl; cplex.getSlacks(vals, con); env.out() << "Slacks = " << vals << endl; cplex.getDuals(vals, con); env.out() << "Duals = " << vals << endl; cplex.getReducedCosts(vals, var); env.out() << "Reduced Costs = " << vals << endl; } // END solveanddisplay
void dlgUser::SetupVarEditor(int var) { if (var >= 0 && varInfo.Count() > 0) { wxStringTokenizer vals(varInfo.Item(var)); wxString typ=vals.GetNextToken(); if (typ == wxT("bool")) { txtValue->Hide(); chkValue->Show(); } else { chkValue->Hide(); txtValue->Show(); if (typ == wxT("string")) txtValue->SetValidator(wxTextValidator()); else txtValue->SetValidator(numericValidator); } } }
Field<T> volumeIntegrateFunctionObject::integrate(const word& fieldName,T unsetVal) const { const GeometricField<T, fvPatchField, volMesh>& fld = obr_.lookupObject<GeometricField<T, fvPatchField, volMesh> > ( fieldName ); Field<T> vals(1, unsetVal); const fvMesh &mesh=refCast<const fvMesh>(obr_); vals[0] = ( sum ( mesh.V()*fld ) ).value(); forAll(vals,i) { vals[i]*=factor(); } if(verbose()) { Info<< regionString() << " Integral of " << fieldName << " = " << vals[0] << " " << fld.dimensions()*dimensionSet(0,3,0,0,0,0,0) << endl; } // Pstream::listCombineGather(vals, isNotEqOp<T>()); // Pstream::listCombineScatter(vals); return vals; }
bool operator() () { bool passed = true; printf("%s::%s ... ",TOSTRING(isa),name); fflush(stdout); /* create key/value vectors with random numbers */ const size_t N = 10000; std::vector<uint32_t> keys(N); std::vector<uint32_t> vals(N); for (size_t i=0; i<N; i++) { keys[i] = 2*rand(); vals[i] = 2*rand(); } /* create map */ pmap<uint32_t,uint32_t> map; map.init(keys,vals); /* check that all keys are properly mapped */ for (size_t i=0; i<N; i++) { const uint32_t* val = map.lookup(keys[i]); passed &= val && (*val == vals[i]); } /* check that these keys are not in the map */ for (size_t i=0; i<N; i++) { passed &= !map.lookup(keys[i]+1); } /* output if test passed or not */ if (passed) printf("[passed]\n"); else printf("[failed]\n"); return passed; }
void dft_HardSphereA22_Tpetra_Operator<Scalar,MatrixType>:: formA22Matrix () { if (A22Matrix_ == Teuchos::null) { A22Matrix_ = rcp(new MAT(getRangeMap(), 1)); A22Matrix_->setObjectLabel("HardSphereA22::A22Matrix"); } LocalOrdinal numRows = getRangeMap()->getNodeNumElements(); Teuchos::ArrayRCP<const Scalar> vectorValues = densityOnDensityMatrix_->get1dView(); for (LocalOrdinal i=OTLO::zero(); i<numRows; i++) { GlobalOrdinal row = A22Matrix_->getRowMap()->getGlobalElement(i); Scalar value = vectorValues[i]; GlobalOrdinal col = row; Array<GlobalOrdinal> cols(1); Array<MatScalar> vals(1); cols[0] = col; vals[0] = Teuchos::as<MatScalar>(value); A22Matrix_->insertGlobalValues(row, cols, vals); } A22Matrix_->fillComplete(); }
int evalRPN(vector<string> &tokens) { int top = 0; vector<int> vals(tokens.size()); for (auto token : tokens) { if (token == "+") { vals[top - 2] += vals[top - 1]; --top; } else if (token == "-") { vals[top - 2] -= vals[top - 1]; --top; } else if (token == "*") { vals[top - 2] *= vals[top - 1]; --top; } else if (token == "/") { vals[top - 2] /= vals[top - 1]; --top; } else { stringstream ss(token); ss >> vals[top]; ++top; } } return vals[0]; }
void LoaderDatabaseConnection::write( const double * values, unsigned int noOfValues, const std::string & dataProviderName, const std::string & placeName, const std::string & referenceTime, const std::string & validTimeFrom, const std::string & validTimeTo, const std::string & valueParameterName, const std::string & levelParameterName, float levelFrom, float levelTo, int dataVersion, int confidenceCode ) { std::vector<float> vals(values, values + noOfValues); write(&vals[0], noOfValues, dataProviderName, placeName, referenceTime, validTimeFrom, validTimeTo, valueParameterName, levelParameterName, levelFrom, levelTo, dataVersion, confidenceCode); }
int test_main(int, char*[]) { typedef boost::multi_array<double, 3>::size_type size_type; boost::array<size_type,3> sizes = { { 3, 3, 3 } }; int strides[] = { 9, 3, 1 }; size_type num_elements = 27; // Default multi_array constructor { boost::multi_array<double, 3> A; } // Constructor 1, default storage order and allocator { boost::multi_array<double, 3> A(sizes); check_shape(A, &sizes[0], strides, num_elements); double* ptr = 0; boost::multi_array_ref<double,3> B(ptr,sizes); check_shape(B, &sizes[0], strides, num_elements); const double* cptr = ptr; boost::const_multi_array_ref<double,3> C(cptr,sizes); check_shape(C, &sizes[0], strides, num_elements); } // Constructor 1, fortran storage order and user-supplied allocator { typedef boost::multi_array<double, 3, std::allocator<double> >::size_type size_type; size_type num_elements = 27; int col_strides[] = { 1, 3, 9 }; boost::multi_array<double, 3, std::allocator<double> > A(sizes,boost::fortran_storage_order()); check_shape(A, &sizes[0], col_strides, num_elements); double *ptr=0; boost::multi_array_ref<double, 3> B(ptr,sizes,boost::fortran_storage_order()); check_shape(B, &sizes[0], col_strides, num_elements); const double *cptr=ptr; boost::const_multi_array_ref<double, 3> C(cptr,sizes,boost::fortran_storage_order()); check_shape(C, &sizes[0], col_strides, num_elements); } // Constructor 2, default storage order and allocator { typedef boost::multi_array<double, 3>::size_type size_type; size_type num_elements = 27; boost::multi_array<double, 3>::extent_gen extents; boost::multi_array<double, 3> A(extents[3][3][3]); check_shape(A, &sizes[0], strides, num_elements); double *ptr=0; boost::multi_array_ref<double, 3> B(ptr,extents[3][3][3]); check_shape(B, &sizes[0], strides, num_elements); const double *cptr=ptr; boost::const_multi_array_ref<double, 3> C(cptr,extents[3][3][3]); check_shape(C, &sizes[0], strides, num_elements); } // Copy Constructors { typedef boost::multi_array<double, 3>::size_type size_type; size_type num_elements = 27; std::vector<double> vals(27, 4.5); boost::multi_array<double, 3> A(sizes); A.assign(vals.begin(),vals.end()); boost::multi_array<double, 3> B(A); check_shape(B, &sizes[0], strides, num_elements); BOOST_TEST(equal(A, B)); double ptr[27]; boost::multi_array_ref<double, 3> C(ptr,sizes); A.assign(vals.begin(),vals.end()); boost::multi_array_ref<double, 3> D(C); check_shape(D, &sizes[0], strides, num_elements); BOOST_TEST(C.data() == D.data()); const double* cptr = ptr; boost::const_multi_array_ref<double, 3> E(cptr,sizes); boost::const_multi_array_ref<double, 3> F(E); check_shape(F, &sizes[0], strides, num_elements); BOOST_TEST(E.data() == F.data()); } // Conversion construction { typedef boost::multi_array<double, 3>::size_type size_type; size_type num_elements = 27; std::vector<double> vals(27, 4.5); boost::multi_array<double, 3> A(sizes); A.assign(vals.begin(),vals.end()); boost::multi_array_ref<double, 3> B(A); boost::const_multi_array_ref<double, 3> C(A); check_shape(B, &sizes[0], strides, num_elements); check_shape(C, &sizes[0], strides, num_elements); BOOST_TEST(B.data() == A.data()); BOOST_TEST(C.data() == A.data()); double ptr[27]; boost::multi_array_ref<double, 3> D(ptr,sizes); D.assign(vals.begin(),vals.end()); boost::const_multi_array_ref<double, 3> E(D); check_shape(E, &sizes[0], strides, num_elements); BOOST_TEST(E.data() == D.data()); } // Assignment Operator { typedef boost::multi_array<double, 3>::size_type size_type; size_type num_elements = 27; std::vector<double> vals(27, 4.5); boost::multi_array<double, 3> A(sizes), B(sizes); A.assign(vals.begin(),vals.end()); B = A; check_shape(B, &sizes[0], strides, num_elements); BOOST_TEST(equal(A, B)); double ptr1[27]; double ptr2[27]; boost::multi_array_ref<double, 3> C(ptr1,sizes), D(ptr2,sizes); C.assign(vals.begin(),vals.end()); D = C; check_shape(D, &sizes[0], strides, num_elements); BOOST_TEST(equal(C,D)); } // subarray value_type is multi_array { typedef boost::multi_array<double,3> array; typedef array::size_type size_type; size_type num_elements = 27; std::vector<double> vals(num_elements, 4.5); boost::multi_array<double, 3> A(sizes); A.assign(vals.begin(),vals.end()); typedef array::subarray<2>::type subarray; subarray B = A[1]; subarray::value_type C = B[0]; // should comparisons between the types work? BOOST_TEST(equal(A[1][0],C)); BOOST_TEST(equal(B[0],C)); } return boost::exit_success; }
int main (int argc, char **argv) { IloEnv env; try { IloModel model(env); IloCplex cplex(env); if (( argc != 3 ) || ( strchr ("opdbn", argv[2][0]) == NULL ) ) { usage (argv[0]); throw(-1); } switch (argv[2][0]) { case 'o': cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::AutoAlg); break; case 'p': cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Primal); break; case 'd': cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Dual); break; case 'b': cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Barrier); break; case 'n': cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Network); break; default: break; } IloObjective obj; IloNumVarArray var(env); IloRangeArray rng(env); cplex.importModel(model, argv[1], obj, var, rng); cplex.extract(model); if ( !cplex.solve() ) { env.error() << "Failed to optimize LP" << endl; throw(-1); } IloNumArray vals(env); cplex.getValues(vals, var); env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "Solution value = " << cplex.getObjValue() << endl; env.out() << "Solution vector = " << vals << endl; } catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; } catch (...) { cerr << "Unknown exception caught" << endl; } env.end(); return 0; } // END main
int main (int argc, char *argv[]) { ifstream infile; infile.open("Proj3_op.txt"); if(!infile){ cerr << "Unable to open the file\n"; exit(1); } cout << "Before Everything!!!" << "\n"; IloEnv env; IloInt i,j,varCount1,varCount2,varCount3,conCount; //same as “int i;” IloInt k,w,K,W,E,l,P,N,L; IloInt tab, newline, val; //from file char line[2048]; try { N = 9; K = 3; L = 36; W = (IloInt)atoi(argv[1]); IloModel model(env); //set up a model object IloNumVarArray var1(env);// C - primary cout << "Here\n"; IloNumVarArray var2(env);// B - backup cout << "here1\n"; IloNumVarArray var3(env);// = IloNumVarArray(env,W); //declare an array of variable objects, for unknowns IloNumVar W_max(env, 0, W, ILOINT); //var1: c_ijk_w IloRangeArray con(env);// = IloRangeArray(env,N*N + 3*w); //declare an array of constraint objects IloNumArray2 t = IloNumArray2(env,N); //Traffic Demand IloNumArray2 e = IloNumArray2(env,N); //edge matrix //IloObjective obj; //Define the Xijk matrix cout << "Before init xijkl\n"; Xijkl xijkl_m(env, L); for(l=0;l<L;l++){ xijkl_m[l] = Xijk(env, N); for(i=0;i<N;i++){ xijkl_m[l][i] = Xjk(env, N); for(j=0;j<N;j++){ xijkl_m[l][i][j] = IloNumArray(env, K); } } } //reset everything to zero here for(l=0;l<L;l++) for(i=0;i<N;i++) for(j=0;j<N;j++) for(k=0;k<K;k++) xijkl_m[l][i][j][k] = 0; input_Xijkl(xijkl_m); cout<<"bahre\n"; for(i=0;i<N;i++){ t[i] = IloNumArray(env,N); for(j=0;j<N;j++){ if(i == j) t[i][j] = IloNum(0); else if(i != j) t[i][j] = IloNum((i+j+2)%5); } } printf("ikde\n"); //Minimize W_max IloObjective obj=IloMinimize(env); obj.setLinearCoef(W_max, 1.0); cout << "here khali\n"; //Setting var1[] for Demands Constraints for(i=0;i<N;i++) for(j=0;j<N;j++) for(k=0;k<K;k++) for(w=0;w<W;w++) var1.add(IloNumVar(env, 0, 1, ILOINT)); //c_ijk_w variables set. //Setting var2[] for Demands Constraints for(i=0;i<N;i++) for(j=0;j<N;j++) for(k=0;k<K;k++) for(w=0;w<W;w++) var2.add(IloNumVar(env, 0, 1, ILOINT)); //b_ijk_w variables set. for(w = 0;w < W;w++) var3.add(IloNumVar(env, 0, 1, ILOINT)); //Variables for u_w cout<<"variables ready\n"; conCount = 0; for(i=0;i<N;i++) for(j=0;j<N;j++){ con.add(IloRange(env, 2 * t[i][j], 2 * t[i][j])); //varCount1 = 0; for(k=0;k<K;k++) for(w=0;w<W;w++){ con[conCount].setLinearCoef(var1[i*N*W*K+j*W*K+k*W+w],1.0); con[conCount].setLinearCoef(var2[i*N*W*K+j*W*K+k*W+w],1.0); } conCount++; }//Adding Demands Constraints to con cout<<"1st\n"; IloInt z= 0; for(w=0;w<W;w++){ for(l=0;l<L;l++){ con.add(IloRange(env, -IloInfinity, 1)); for(i=0;i<N;i++){ for(j=0;j<N;j++){ for(k=0;k<K;k++){ con[conCount].setLinearCoef(var1[i*N*W*K+j*W*K+k*W+w],xijkl_m[l][i][j][k]); con[conCount].setLinearCoef(var2[i*N*W*K+j*W*K+k*W+w],xijkl_m[l][i][j][k]); } } } conCount++; } } cout<<"2nd\n"; //Adding Wavelength Constraints_1 to con P = N * (N-1) * K; for(w=0;w<W;w++){ con.add(IloRange(env, -IloInfinity, 0)); varCount1 = 0; for(i=0;i<N;i++) for(j=0;j<N;j++) for(k=0;k<K;k++){ con[conCount].setLinearCoef(var1[i*N*W*K+j*W*K+k*W+w],1.0); con[conCount].setLinearCoef(var2[i*N*W*K+j*W*K+k*W+w],1.0); } con[conCount].setLinearCoef(var3[w],-P); conCount++; } cout<<"3rd\n"; for(i=0;i<N;i++) for(j=0;j<N;j++) for(k=0;k<K;k++) for(w=0;w<W;w++){ con.add(IloRange(env, -IloInfinity, 1)); con[conCount].setLinearCoef(var1[i*N*W*K+j*W*K+k*W+w], 1.0); con[conCount++].setLinearCoef(var2[i*N*W*K+j*W*K+k*W+w], 1.0); } varCount3 = 0; for(w=0;w<W;w++){ con.add(IloRange(env, 0, IloInfinity)); con[conCount].setLinearCoef(W_max, 1.0); con[conCount++].setLinearCoef(var3[w], -1.0 * (w+1)); } cout<<"after constraints\n"; //model.add(obj); //add objective function into model model.add(IloMinimize(env,obj)); model.add(con); //add constraints into model IloCplex cplex(model); //create a cplex object and extract the //model to this cplex object // Optimize the problem and obtain solution. if ( !cplex.solve() ) { env.error() << "Failed to optimize LP" << endl; throw(-1); } IloNumArray vals(env); //declare an array to store the outputs //if 2 dimensional: IloNumArray2 vals(env); env.out() << "Solution status = " << cplex.getStatus() << endl; //return the status: Feasible/Optimal/Infeasible/Unbounded/Error/… env.out() << "Solution value = " << cplex.getObjValue() << endl; //return the optimal value for objective function cplex.getValues(vals, var1); //get the variable outputs env.out() << "Values Var1 = " << vals << endl; //env.out() : output stream cplex.getValues(vals, var3); env.out() << "Values Val3 = " << vals << endl; } catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; } catch (...) { cerr << "Unknown exception caught" << endl; } env.end(); //close the CPLEX environment return 0; } // END main
void FactorAdditionOp::execute() { if (_generalFactors.size() == 2) { if (_intendedStorageType == EVAL) { _generalTarget->setStorageType(EVAL); return; } else if (_intendedStorageType == SPARSE) { /* TODO: test this */ #if 0 vector<int> vars = _generalTarget->getOrderedVars(); drwnSparseFactor *fac1 = _A->getSparseFactor(); drwnSparseFactor *fac2 = _B->getSparseFactor(); drwnSparseFactor *target = _generalTarget->getSparseFactor(); map<vector<int>, double> assignmtsA = fac1->getAssignments(); map<vector<int>, double> assignmtsB = fac2->getAssignments(); for (map< vector<int>, double >::iterator mi = assignmtsA.begin(); mi != assignmtsA.end(); mi++) { vector<int> vals(vars.size()); for (int i = 0; i < vars.size(); i++) { vals[_targetToA[i]] = (*mi).first[_targetToA[i]]; } drwnPartialAssignment dpa; target->setValueOf(dpa, (*mi).second); } for (map< vector<int>, double >::iterator mi = assignmtsB.begin(); mi != assignmtsA.end(); mi++) { vector<int> vals(vars.size()); for (int i = 0; i < vars.size(); i++) { vals[_targetToB[i]] = (*mi).first[_targetToB[i]]; } drwnPartialAssignment dpa; target->setValueOf(dpa, target->getValueOf(dpa) + (*mi).second); } #endif return; } /* dealing with dense factors */ drwnTableFactor *target = _generalTarget->getTableFactor(); drwnFactorAdditionOp dfao(target, _generalFactors[0]->getTableFactor(), _generalFactors[1]->getTableFactor()); dfao.execute(); _generalTarget = new drwnGeneralFactor(*target, _generalTarget->THRESHOLD); return; } drwnGeneralFactor *prevSum = _generalFactors.front()->clone(); for (int fi = 1; fi < _generalFactors.size(); fi++) { drwnTableFactor tf(_generalTarget->getUniverse()); drwnGeneralFactor *partSum = (fi == (_generalFactors.size() - 1)) ? _generalTarget : new drwnGeneralFactor(tf, _generalTarget->THRESHOLD); FactorAdditionOp fao(partSum, prevSum, _generalFactors[fi]); fao.execute(); prevSum = partSum; } }
void ClusterProperties::performTask( const unsigned& task_index, const unsigned& current, MultiValue& myvals ) const { std::vector<double> vals( myvals.getNumberOfValues() ); getPropertiesOfNode( current, vals ); if( !doNotCalculateDerivatives() ) getNodePropertyDerivatives( current, myvals ); for(unsigned k=0; k<vals.size(); ++k) myvals.setValue( k, vals[k] ); }
/** * set the values of a repeated field * * @param xp (GPB::Message*) external pointer * @param field field tag number or name * @param index positions (0-based) * @param values new values */ RPB_FUNCTION_VOID_4(METHOD(set_field_values), Rcpp::XPtr<GPB::Message> message, SEXP field, Rcpp::IntegerVector index, SEXP values) { const GPB::FieldDescriptor* field_desc = getFieldDescriptor(message, field); if (!field_desc->is_repeated()) { throw std::range_error("set can only be used on repeated fields"); } const GPB::Reflection* ref = message->GetReflection(); /* we know here that LENGTH(index) == LENGTH(values) */ int n = index.size(); switch (field_desc->type()) { case TYPE_INT32: case TYPE_SINT32: case TYPE_SFIXED32: { for (int i = 0; i < n; i++) { ref->SetRepeatedInt32(message, field_desc, index[i], GET_int32(values, i)); } break; } case TYPE_INT64: case TYPE_SINT64: case TYPE_SFIXED64: { for (int i = 0; i < n; i++) { ref->SetRepeatedInt64(message, field_desc, index[i], GET_int64(values, i)); } break; } case TYPE_UINT32: case TYPE_FIXED32: { for (int i = 0; i < n; i++) { ref->SetRepeatedUInt32(message, field_desc, GET_int(index, i), GET_uint32(values, i)); } break; } case TYPE_UINT64: case TYPE_FIXED64: { for (int i = 0; i < n; i++) { ref->SetRepeatedUInt64(message, field_desc, index[i], GET_uint64(values, i)); } break; } case TYPE_DOUBLE: { for (int i = 0; i < n; i++) { ref->SetRepeatedDouble(message, field_desc, index[i], GET_double(values, i)); } break; } case TYPE_FLOAT: { for (int i = 0; i < n; i++) { ref->SetRepeatedFloat(message, field_desc, index[i], GET_float(values, i)); } break; } case TYPE_BOOL: { for (int i = 0; i < n; i++) { ref->SetRepeatedBool(message, field_desc, index[i], GET_bool(values, i)); } break; } case TYPE_STRING: { for (int i = 0; i < n; i++) { ref->SetRepeatedString(message, field_desc, index[i], GET_stdstring(values, i)); } break; } case TYPE_BYTES: { for (int i = 0; i < n; i++) { ref->SetRepeatedString(message, field_desc, index[i], GET_bytes(values, i)); } break; } case TYPE_ENUM: { CHECK_values_for_enum(field_desc, values); const GPB::EnumDescriptor* enum_desc = field_desc->enum_type(); switch (TYPEOF(values)) { case INTSXP: case REALSXP: case LGLSXP: case RAWSXP: { for (int i = 0; i < n; i++) { int val = GET_int(values, i); ref->SetRepeatedEnum(message, field_desc, i, enum_desc->FindValueByNumber(val)); } break; } case STRSXP: { Rcpp::CharacterVector vals(values); std::string val; for (int i = 0; i < n; i++) { val = vals[i]; const GPB::EnumValueDescriptor* evd = enum_desc->FindValueByName(val); ref->SetRepeatedEnum(message, field_desc, i, evd); } break; } default: throw std::range_error("impossible to convert to a enum"); } break; } case TYPE_MESSAGE: case TYPE_GROUP: { CHECK_messages(field_desc, values); Rcpp::List vals(values); for (int i = 0; i < n; i++) { GPB::Message* mess = GET_MESSAGE_POINTER_FROM_S4(vals[i]); ref->MutableRepeatedMessage(message, field_desc, i)->CopyFrom(*mess); } break; } default: throw std::range_error("unknown type"); } }
Stokhos::SmolyakPseudoSpectralOperator<ordinal_type,value_type,point_compare_type>:: SmolyakPseudoSpectralOperator( const SmolyakBasis<ordinal_type,value_type,coeff_compare_type>& smolyak_basis, bool use_smolyak_apply, bool use_pst, const point_compare_type& point_compare) : use_smolyak(use_smolyak_apply), points(point_compare) { typedef SmolyakBasis<ordinal_type,value_type,coeff_compare_type> smolyak_basis_type; typedef typename smolyak_basis_type::tensor_product_basis_type tensor_product_basis_type; // Generate sparse grid and tensor operators coeff_sz = smolyak_basis.size(); ordinal_type dim = smolyak_basis.dimension(); ordinal_type num_terms = smolyak_basis.getNumSmolyakTerms(); for (ordinal_type i=0; i<num_terms; ++i) { // Get tensor product basis for given term Teuchos::RCP<const tensor_product_basis_type> tp_basis = smolyak_basis.getTensorProductBasis(i); // Get coefficient multi-index defining basis orders multiindex_type coeff_index = tp_basis->getMaxOrders(); // Apply growth rule to cofficient multi-index multiindex_type point_growth_index(dim); for (ordinal_type j=0; j<dim; ++j) { point_growth_index[j] = smolyak_basis.getCoordinateBases()[j]->pointGrowth(coeff_index[j]); } // Build tensor product operator for given index Teuchos::RCP<operator_type> op = Teuchos::rcp(new operator_type(*tp_basis, use_pst, point_growth_index)); if (use_smolyak) operators.push_back(op); // Get smolyak cofficient for given index value_type c = smolyak_basis.getSmolyakCoefficient(i); if (use_smolyak) smolyak_coeffs.push_back(c); // Include points in union over all sets typename operator_type::set_iterator op_set_iterator = op->set_begin(); typename operator_type::set_iterator op_set_end = op->set_end(); for (; op_set_iterator != op_set_end; ++op_set_iterator) { const point_type& point = op_set_iterator->first; value_type w = op_set_iterator->second.first; set_iterator si = points.find(point); if (si == points.end()) points[point] = std::make_pair(c*w,ordinal_type(0)); else si->second.first += c*w; } } // Generate linear ordering of points ordinal_type idx = 0; point_map.resize(points.size()); for (set_iterator si = points.begin(); si != points.end(); ++si) { si->second.second = idx; point_map[idx] = si->first; ++idx; } if (use_smolyak) { // Build gather/scatter maps into global domain/range for each operator gather_maps.resize(operators.size()); scatter_maps.resize(operators.size()); for (ordinal_type i=0; i<operators.size(); i++) { Teuchos::RCP<operator_type> op = operators[i]; gather_maps[i].reserve(op->point_size()); typename operator_type::iterator op_iterator = op->begin(); typename operator_type::iterator op_end = op->end(); for (; op_iterator != op_end; ++op_iterator) { gather_maps[i].push_back(points[*op_iterator].second); } Teuchos::RCP<const tensor_product_basis_type> tp_basis = smolyak_basis.getTensorProductBasis(i); ordinal_type op_coeff_sz = tp_basis->size(); scatter_maps[i].reserve(op_coeff_sz); for (ordinal_type j=0; j<op_coeff_sz; ++j) { scatter_maps[i].push_back(smolyak_basis.index(tp_basis->term(j))); } } } //else { // Generate quadrature operator ordinal_type nqp = points.size(); ordinal_type npc = coeff_sz; qp2pce.reshape(npc,nqp); pce2qp.reshape(nqp,npc); qp2pce.putScalar(1.0); pce2qp.putScalar(1.0); Teuchos::Array<value_type> vals(npc); for (set_iterator si = points.begin(); si != points.end(); ++si) { ordinal_type j = si->second.second; value_type w = si->second.first; point_type point = si->first; smolyak_basis.evaluateBases(point, vals); for (ordinal_type i=0; i<npc; ++i) { qp2pce(i,j) = w*vals[i] / smolyak_basis.norm_squared(i); pce2qp(j,i) = vals[i]; } } //} }
int main(int argc) { IloEnv env; try { IloModel model(env); NumVarMatrix varOutput(env, J + current); NumVar3Matrix varHelp(env, J + current); Range3Matrix cons(env, J + current); for (int j = 0; j <J + current; j++){ varOutput[j] = IloNumVarArray(env, K); varHelp[j] = NumVarMatrix(env, K); cons[j] = RangeMatrix(env, K); for (int k = 0; k < K; k++){ varOutput[j][k] = IloNumVar(env, 0.0, IloInfinity); varHelp[j][k] = IloNumVarArray(env, L); cons[j][k] = IloRangeArray(env, C); for (int l = 0; l < L; l++){ varHelp[j][k][l] = IloNumVar(env, 0.0, IloInfinity); } if (j > current){ cons[j][k][0] = IloRange(env, 0.0, 0.0);//will be used to express equality of varOutput, constraint (0) cons[j][k][1] = IloRange(env, 0.0, IloInfinity);// constraint (1) cons[j][k][2] = IloRange(env, -IloInfinity, T[j] - Tdc - Tblow[j] - Tslack);// constraint (2) cons[j][k][3] = IloRange(env, Tfd[k], Tfd[k]);// constraint (3) cons[j][k][4] = IloRange(env, 0.0, IloInfinity);// constraint (4) cons[j][k][5] = IloRange(env, Tdf[k], IloInfinity);// constraint (5) cons[j][k][6] = IloRange(env, T[j - a[k]] + Tcd, T[j - a[k]] + Tcd);// constraint (6) cons[j][k][7] = IloRange(env, TlossD[k], IloInfinity);// constraint (7) cons[j][k][8] = IloRange(env, TlossF[k], IloInfinity);// constraint (8) } } } populatebynonzero(model, varOutput, varHelp, cons); IloCplex cplex(model); // Optimize the problem and obtain solution. if (!cplex.solve()) { env.error() << "Failed to optimize LP" << endl; throw(-1); } IloNumArray vals(env); IloNumVar val(env); //vars to save output double TimeAvailable[J][K]; double TimeInstances[J][K][L]; double LK103[J][2]; env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "Solution value = " << cplex.getObjValue() << endl; for (int j = current; j < current + J; ++j) { cplex.getValues(vals, varOutput[j]); env.out() << "Seconds for load "<<j<<" = " << vals << endl; /*for (int k = 0; k < K; k++){ TimeAvailable[j][k] = cplex.getValue(varOutput[j][k]); }*/ } for (int j = current; j < current + J; j++){ for (int k = 0; k < K; k++){ cplex.getValues(vals, varHelp[j][k]); env.out() << "Time instances for spoon "<<k<<" in load "<<j<<" = " << vals << endl; /*for (int l = 0; l < L; l++){ TimeInstances[j][k][l] = cplex.getValue(varHelp[j][k][l]); }*/ } } for (int j = current + 2; j < J + current; j++){ LK103[j][0] = TimeInstances[j - 2][0][0]; LK103[j][1] = TimeInstances[j][0][5]; env.out() << "LK103, load " << j << " : " << LK103[j][1]-LK103[j][0] << endl; } /*cplex.getSlacks(vals, cons); env.out() << "Slacks = " << vals << endl; cplex.getDuals(vals, cons); env.out() << "Duals = " << vals << endl; cplex.getReducedCosts(vals, varOutput); env.out() << "Reduced Costs = " << vals << endl;*/ cplex.exportModel("lpex1.lp"); } catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; } catch (...) { cerr << "Unknown exception caught" << endl; } env.end(); cin.get(); return 0; } // END main
int HGRAD_TET_C2_FEM_Test01(const bool verbose) { Teuchos::RCP<std::ostream> outStream; Teuchos::oblackholestream bhs; // outputs nothing if (verbose) outStream = Teuchos::rcp(&std::cout, false); else outStream = Teuchos::rcp(&bhs, false); Teuchos::oblackholestream oldFormatState; oldFormatState.copyfmt(std::cout); typedef typename Kokkos::Impl::is_space<DeviceSpaceType>::host_mirror_space::execution_space HostSpaceType ; *outStream << "DeviceSpace:: "; DeviceSpaceType::print_configuration(*outStream, false); *outStream << "HostSpace:: "; HostSpaceType::print_configuration(*outStream, false); *outStream << "\n" << "===============================================================================\n" << "| |\n" << "| Unit Test (Basis_HGRAD_TET_C2_FEM) |\n" << "| |\n" << "| 1) Conversion of Dof tags into Dof ordinals and back |\n" << "| 2) Basis values for VALUE, GRAD, and Dk operators |\n" << "| |\n" << "| Questions? Contact Pavel Bochev ([email protected]), |\n" << "| Denis Ridzal ([email protected]), |\n" << "| Kara Peterson ([email protected]). |\n" << "| Kyungjoo Kim ([email protected]). |\n" << "| |\n" << "| Intrepid's website: http://trilinos.sandia.gov/packages/intrepid |\n" << "| Trilinos website: http://trilinos.sandia.gov |\n" << "| |\n" << "===============================================================================\n"; typedef Kokkos::DynRankView<ValueType,DeviceSpaceType> DynRankView; typedef Kokkos::DynRankView<ValueType,HostSpaceType> DynRankViewHost; #define ConstructWithLabel(obj, ...) obj(#obj, __VA_ARGS__) const ValueType tol = tolerence(); int errorFlag = 0; // for virtual function, value and point types are declared in the class typedef ValueType outputValueType; typedef ValueType pointValueType; Basis_HGRAD_TET_C2_FEM<DeviceSpaceType,outputValueType,pointValueType> tetBasis; *outStream << "\n" << "===============================================================================\n" << "| TEST 1: Basis creation, exception testing |\n" << "===============================================================================\n"; try{ ordinal_type nthrow = 0, ncatch = 0; #ifdef HAVE_INTREPID2_DEBUG DynRankView ConstructWithLabel(tetNodes, 10, 3); const auto numFields = tetBasis.getCardinality(); const auto numPoints = tetNodes.dimension(0); const auto spaceDim = tetBasis.getBaseCellTopology().getDimension(); DynRankView ConstructWithLabel(vals, numFields, numPoints); DynRankView ConstructWithLabel(vals_vec, numFields, numPoints, 4); { // exception #1: CURL cannot be applied to scalar functions // resize vals to rank-3 container with dimensions (num. points, num. basis functions, arbitrary) INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getValues(vals_vec, tetNodes, OPERATOR_CURL) ); } { // exception #2: DIV cannot be applied to scalar functions // resize vals to rank-2 container with dimensions (num. points, num. basis functions) INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getValues(vals, tetNodes, OPERATOR_DIV) ); } // Exceptions 3-7: all bf tags/bf Ids below are wrong and should cause getDofOrdinal() and // getDofTag() to access invalid array elements thereby causing bounds check exception { // exception #3 INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getDofOrdinal(3,0,0) ); // exception #4 INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getDofOrdinal(1,1,1) ); // exception #5 INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getDofOrdinal(0,4,0) ); // exception #6 INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getDofTag(10) ); // exception #7 INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getDofTag(-1) ); } // Exceptions 8-18 test exception handling with incorrectly dimensioned input/output arrays { // exception #8: input points array must be of rank-2 DynRankView ConstructWithLabel(badPoints1, 4, 5, 3); INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getValues(vals, badPoints1, OPERATOR_VALUE) ); } { // exception #9 dimension 1 in the input point array must equal space dimension of the cell DynRankView ConstructWithLabel(badPoints2, 4, spaceDim - 1); INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getValues(vals, badPoints2, OPERATOR_VALUE) ); } { // exception #10 output values must be of rank-2 for OPERATOR_VALUE DynRankView ConstructWithLabel(badVals1, 4, 3, 1); INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getValues(badVals1, tetNodes, OPERATOR_VALUE) ); } { // exception #11 output values must be of rank-3 for OPERATOR_GRAD DynRankView ConstructWithLabel(badVals2, 4, 3); INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getValues(badVals2, tetNodes, OPERATOR_GRAD) ); // exception #12 output values must be of rank-3 for OPERATOR_D1 INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getValues(badVals2, tetNodes, OPERATOR_D1) ); // exception #13 output values must be of rank-3 for OPERATOR_D2 INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getValues(badVals2, tetNodes, OPERATOR_D2) ); } { // exception #14 incorrect 0th dimension of output array (must equal number of basis functions) DynRankView ConstructWithLabel(badVals3, numFields + 1, numPoints); INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getValues(badVals3, tetNodes, OPERATOR_VALUE) ); } { // exception #15 incorrect 1st dimension of output array (must equal number of points) DynRankView ConstructWithLabel(badVals4, numFields, numPoints + 1); INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getValues(badVals4, tetNodes, OPERATOR_VALUE) ); } { // exception #16: incorrect 2nd dimension of output array (must equal the space dimension) DynRankView ConstructWithLabel(badVals5, numFields, numPoints, spaceDim + 1); INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getValues(badVals5, tetNodes, OPERATOR_GRAD) ); } { // exception #17: incorrect 2nd dimension of output array (must equal D2 cardinality in 2D) DynRankView ConstructWithLabel(badVals6, numFields, numPoints, 40); INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getValues(badVals6, tetNodes, OPERATOR_D1) ); // exception #18: incorrect 2nd dimension of output array (must equal D3 cardinality in 2D) INTREPID2_TEST_ERROR_EXPECTED( tetBasis.getValues(badVals6, tetNodes, OPERATOR_D2) ); } #endif // Check if number of thrown exceptions matches the one we expect if (nthrow != ncatch) { errorFlag++; *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n"; *outStream << "# of catch ("<< ncatch << ") is different from # of throw (" << nthrow << ")\n"; } } catch (std::logic_error err) { *outStream << "UNEXPECTED ERROR !!! ----------------------------------------------------------\n"; *outStream << err.what() << '\n'; *outStream << "-------------------------------------------------------------------------------" << "\n\n"; errorFlag = -1000; }; *outStream << "\n" << "===============================================================================\n" << "| TEST 2: correctness of tag to enum and enum to tag lookups |\n" << "===============================================================================\n"; try{ const auto numFields = tetBasis.getCardinality(); const auto allTags = tetBasis.getAllDofTags(); // Loop over all tags, lookup the associated dof enumeration and then lookup the tag again const auto dofTagSize = allTags.dimension(0); for (auto i = 0; i < dofTagSize; ++i) { const auto bfOrd = tetBasis.getDofOrdinal(allTags(i,0), allTags(i,1), allTags(i,2)); const auto myTag = tetBasis.getDofTag(bfOrd); if( !( (myTag(0) == allTags(i,0)) && (myTag(1) == allTags(i,1)) && (myTag(2) == allTags(i,2)) && (myTag(3) == allTags(i,3)) ) ) { errorFlag++; *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n"; *outStream << " getDofOrdinal( {" << allTags(i,0) << ", " << allTags(i,1) << ", " << allTags(i,2) << ", " << allTags(i,3) << "}) = " << bfOrd <<" but \n"; *outStream << " getDofTag(" << bfOrd << ") = { " << myTag(0) << ", " << myTag(1) << ", " << myTag(2) << ", " << myTag(3) << "}\n"; } } // Now do the same but loop over basis functions for( auto bfOrd = 0; bfOrd < numFields; bfOrd++) { const auto myTag = tetBasis.getDofTag(bfOrd); const auto myBfOrd = tetBasis.getDofOrdinal(myTag(0), myTag(1), myTag(2)); if( bfOrd != myBfOrd) { errorFlag++; *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n"; *outStream << " getDofTag(" << bfOrd << ") = { " << myTag(0) << ", " << myTag(1) << ", " << myTag(2) << ", " << myTag(3) << "} but getDofOrdinal({" << myTag(0) << ", " << myTag(1) << ", " << myTag(2) << ", " << myTag(3) << "} ) = " << myBfOrd << "\n"; } } } catch (std::logic_error err){ *outStream << err.what() << "\n\n"; errorFlag = -1000; }; *outStream << "\n" << "===============================================================================\n" << "| TEST 3: correctness of basis function values |\n" << "===============================================================================\n"; outStream -> precision(20); // VALUE: in (F,P) format const ValueType basisValues[] = { 1.00000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.00000, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 1.00000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.00000, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 1.00000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.00000, 0, \ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.00000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ 1.00000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.00000, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 1.00000 }; // GRAD and D1: in (F,P,D) format const ValueType basisGrads[] = { -3.00000, -3.00000, -3.00000, 1.00000, 1.00000, 1.00000, 1.00000, \ 1.00000, 1.00000, 1.00000, 1.00000, 1.00000, -1.00000, -1.00000, \ -1.00000, 1.00000, 1.00000, 1.00000, -1.00000, -1.00000, -1.00000, \ -1.00000, -1.00000, -1.00000, 1.00000, 1.00000, 1.00000, 1.00000, \ 1.00000, 1.00000, -1.00000, 0, 0, 3.00000, 0, 0, -1.00000, 0, 0, \ -1.00000, 0, 0, 1.00000, 0, 0, 1.00000, 0, 0, -1.00000, 0, 0, \ -1.00000, 0, 0, 1.00000, 0, 0, -1.00000, 0, 0, 0, -1.00000, 0, 0, \ -1.00000, 0, 0, 3.00000, 0, 0, -1.00000, 0, 0, -1.00000, 0, 0, \ 1.00000, 0, 0, 1.00000, 0, 0, -1.00000, 0, 0, -1.00000, 0, 0, \ 1.00000, 0, 0, 0, -1.00000, 0, 0, -1.00000, 0, 0, -1.00000, 0, 0, \ 3.00000, 0, 0, -1.00000, 0, 0, -1.00000, 0, 0, -1.00000, 0, 0, \ 1.00000, 0, 0, 1.00000, 0, 0, 1.00000, 4.00000, 0, 0, -4.00000, \ -4.00000, -4.00000, 0, 0, 0, 0, 0, 0, 0, -2.00000, -2.00000, \ -2.00000, -2.00000, -2.00000, 2.00000, 0, 0, 2.00000, 0, 0, -2.00000, \ -2.00000, -2.00000, 0, 0, 0, 0, 0, 0, 0, 4.00000, 0, 4.00000, 0, 0, \ 0, 0, 0, 0, 2.00000, 0, 2.00000, 2.00000, 0, 2.00000, 0, 0, 0, 0, 0, \ 0, 2.00000, 0, 2.00000, 0, 0, 0, 4.00000, 0, 0, 0, 0, -4.00000, \ -4.00000, -4.00000, 0, 0, 0, 0, 2.00000, 0, -2.00000, -2.00000, \ -2.00000, -2.00000, 0, -2.00000, 0, 2.00000, 0, 0, 0, 0, -2.00000, \ -2.00000, -2.00000, 0, 0, 4.00000, 0, 0, 0, 0, 0, 0, -4.00000, \ -4.00000, -4.00000, 0, 0, 2.00000, 0, 0, 0, 0, 0, 2.00000, -2.00000, \ -2.00000, 0, -2.00000, -2.00000, -2.00000, -2.00000, -2.00000, \ -2.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 4.00000, 0, 0, 0, 0, \ 2.00000, 0, 0, 2.00000, 0, 0, 0, 2.00000, 0, 0, 2.00000, 0, 2.00000, \ 2.00000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4.00000, 0, 4.00000, 0, 0, 0, \ 0, 0, 0, 2.00000, 0, 0, 2.00000, 0, 2.00000, 0, 0, 2.00000, 0, 0, \ 2.00000, 2.00000}; // D2 values in (F,P, Dk) format const ValueType basisD2[]={ 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, \ 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, \ 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, \ 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, \ 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, \ 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, \ 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, \ 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, \ 4.00000, 4.00000, 4.00000, 4.00000, 4.00000, 0, 0, 0, 0, 0, 4.00000, \ 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, \ 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, \ 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, \ 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, \ 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, \ 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, \ 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 0, 0, 4.00000, \ 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, \ 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, \ 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, \ 0, 0, 4.00000, -8.00000, -4.00000, -4.00000, 0, 0, 0, -8.00000, \ -4.00000, -4.00000, 0, 0, 0, -8.00000, -4.00000, -4.00000, 0, 0, 0, \ -8.00000, -4.00000, -4.00000, 0, 0, 0, -8.00000, -4.00000, -4.00000, \ 0, 0, 0, -8.00000, -4.00000, -4.00000, 0, 0, 0, -8.00000, -4.00000, \ -4.00000, 0, 0, 0, -8.00000, -4.00000, -4.00000, 0, 0, 0, -8.00000, \ -4.00000, -4.00000, 0, 0, 0, -8.00000, -4.00000, -4.00000, 0, 0, 0, \ 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, \ 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, \ 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, \ 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, -4.00000, 0, -8.00000, -4.00000, \ 0, 0, -4.00000, 0, -8.00000, -4.00000, 0, 0, -4.00000, 0, -8.00000, \ -4.00000, 0, 0, -4.00000, 0, -8.00000, -4.00000, 0, 0, -4.00000, 0, \ -8.00000, -4.00000, 0, 0, -4.00000, 0, -8.00000, -4.00000, 0, 0, \ -4.00000, 0, -8.00000, -4.00000, 0, 0, -4.00000, 0, -8.00000, \ -4.00000, 0, 0, -4.00000, 0, -8.00000, -4.00000, 0, 0, -4.00000, 0, \ -8.00000, -4.00000, 0, 0, 0, -4.00000, 0, -4.00000, -8.00000, 0, 0, \ -4.00000, 0, -4.00000, -8.00000, 0, 0, -4.00000, 0, -4.00000, \ -8.00000, 0, 0, -4.00000, 0, -4.00000, -8.00000, 0, 0, -4.00000, 0, \ -4.00000, -8.00000, 0, 0, -4.00000, 0, -4.00000, -8.00000, 0, 0, \ -4.00000, 0, -4.00000, -8.00000, 0, 0, -4.00000, 0, -4.00000, \ -8.00000, 0, 0, -4.00000, 0, -4.00000, -8.00000, 0, 0, -4.00000, 0, \ -4.00000, -8.00000, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, \ 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, \ 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, \ 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 0, 0, \ 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, \ 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, \ 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, 0, 0, 0, 4.00000, 0, 0, \ 0, 0, 0, 4.00000, 0 }; try{ ordinal_type nthrow = 0, ncatch = 0; DynRankViewHost ConstructWithLabel(tetNodesHost, 10, 3); tetNodesHost(0,0) = 0.0; tetNodesHost(0,1) = 0.0; tetNodesHost(0,2) = 0.0; tetNodesHost(1,0) = 1.0; tetNodesHost(1,1) = 0.0; tetNodesHost(1,2) = 0.0; tetNodesHost(2,0) = 0.0; tetNodesHost(2,1) = 1.0; tetNodesHost(2,2) = 0.0; tetNodesHost(3,0) = 0.0; tetNodesHost(3,1) = 0.0; tetNodesHost(3,2) = 1.0; tetNodesHost(4,0) = 0.5; tetNodesHost(4,1) = 0.0; tetNodesHost(4,2) = 0.0; tetNodesHost(5,0) = 0.5; tetNodesHost(5,1) = 0.5; tetNodesHost(5,2) = 0.0; tetNodesHost(6,0) = 0.0; tetNodesHost(6,1) = 0.5; tetNodesHost(6,2) = 0.0; tetNodesHost(7,0) = 0.0; tetNodesHost(7,1) = 0.0; tetNodesHost(7,2) = 0.5; tetNodesHost(8,0) = 0.5; tetNodesHost(8,1) = 0.0; tetNodesHost(8,2) = 0.5; tetNodesHost(9,0) = 0.0; tetNodesHost(9,1) = 0.5; tetNodesHost(9,2) = 0.5; auto tetNodes = Kokkos::create_mirror_view(typename DeviceSpaceType::memory_space(), tetNodesHost); Kokkos::deep_copy(tetNodes, tetNodesHost); // Dimensions for the output arrays: const auto numFields = tetBasis.getCardinality(); const auto numPoints = tetNodes.dimension(0); const auto spaceDim = tetBasis.getBaseCellTopology().getDimension(); const auto D2cardinality = getDkCardinality(OPERATOR_D2, spaceDim); { // Check VALUE of basis functions: resize vals to rank-2 container: DynRankView ConstructWithLabel(vals, numFields, numPoints); tetBasis.getValues(vals, tetNodes, OPERATOR_VALUE); auto vals_host = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), vals); Kokkos::deep_copy(vals_host, vals); for (auto i = 0; i < numFields; ++i) { for (auto j = 0; j < numPoints; ++j) { const auto l = i + j * numFields; if (std::abs(vals_host(i,j) - basisValues[l]) > tol) { errorFlag++; *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n"; // Output the multi-index of the value where the error is: *outStream << " At multi-index { "; *outStream << i << " ";*outStream << j << " "; *outStream << "} computed value: " << vals_host(i,j) << " but reference value: " << basisValues[l] << "\n"; } } } } { // Check GRAD of basis function: resize vals to rank-3 container DynRankView ConstructWithLabel(vals, numFields, numPoints, spaceDim); tetBasis.getValues(vals, tetNodes, OPERATOR_GRAD); auto vals_host = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), vals); Kokkos::deep_copy(vals_host, vals); for (auto i = 0; i < numFields; ++i) { for (auto j = 0; j < numPoints; ++j) { for (auto k = 0; k < spaceDim; ++k) { // basisGrads is (F,P,D), compute offset: const auto l = k + j * spaceDim + i * spaceDim * numPoints; if (std::abs(vals_host(i,j,k) - basisGrads[l]) > tol) { errorFlag++; *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n"; // Output the multi-index of the value where the error is: *outStream << " At multi-index { "; *outStream << i << " ";*outStream << j << " ";*outStream << k << " "; *outStream << "} computed grad component: " << vals_host(i,j,k) << " but reference grad component: " << basisGrads[l] << "\n"; } } } } // Check D1 of basis function (do not resize vals because it has the correct size: D1 = GRAD) tetBasis.getValues(vals, tetNodes, OPERATOR_D1); Kokkos::deep_copy(vals_host, vals); for (auto i = 0; i < numFields; ++i) { for (auto j = 0; j < numPoints; ++j) { for (auto k = 0; k < spaceDim; ++k) { // basisGrads is (F,P,D), compute offset: const auto l = k + j * spaceDim + i * spaceDim * numPoints; if (std::abs(vals_host(i,j,k) - basisGrads[l]) > tol) { errorFlag++; *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n"; // Output the multi-index of the value where the error is: *outStream << " At multi-index { "; *outStream << i << " ";*outStream << j << " ";*outStream << k << " "; *outStream << "} computed D1 component: " << vals_host(i,j,k) << " but reference D1 component: " << basisGrads[l] << "\n"; } } } } } { // Check D2 of basis function DynRankView ConstructWithLabel(vals, numFields, numPoints, D2cardinality); tetBasis.getValues(vals, tetNodes, OPERATOR_D2); auto vals_host = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), vals); Kokkos::deep_copy(vals_host, vals); for (auto i = 0; i < numFields; ++i) { for (auto j = 0; j < numPoints; ++j) { for (auto k = 0; k < D2cardinality; ++k) { // basisD2 is (F,P,Dk), compute offset: const auto l = k + j * D2cardinality + i * D2cardinality * numPoints; if (std::abs(vals_host(i,j,k) - basisD2[l]) > tol) { errorFlag++; *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n"; // Output the multi-index of the value where the error is: *outStream << " At multi-index { "; *outStream << i << " ";*outStream << j << " ";*outStream << k << " "; *outStream << "} computed D2 component: " << vals_host(i,j,k) << " but reference D2 component: " << basisD2[l] << "\n"; } } } } } { // Check all higher derivatives - must be zero. const EOperator ops[] = { OPERATOR_D3, OPERATOR_D4, OPERATOR_D5, OPERATOR_D6, OPERATOR_D7, OPERATOR_D8, OPERATOR_D9, OPERATOR_D10, OPERATOR_MAX }; for (auto h=0;ops[h]!=OPERATOR_MAX;++h) { const auto op = ops[h]; // The last dimension is the number of kth derivatives and needs to be resized for every Dk const auto DkCardin = getDkCardinality(op, spaceDim); DynRankView vals("vals", numFields, numPoints, DkCardin); tetBasis.getValues(vals, tetNodes, op); auto vals_host = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), vals); Kokkos::deep_copy(vals_host, vals); for (auto i1 = 0; i1 < numFields; ++i1) for (auto i2 = 0; i2 < numPoints; ++i2) for (auto i3 = 0; i3 < DkCardin; ++i3) { if (std::abs(vals_host(i1,i2,i3)) > tol) { errorFlag++; *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n"; // Get the multi-index of the value where the error is and the operator order int ord = Intrepid2::getOperatorOrder(op); *outStream << " At multi-index { "<<i1<<" "<<i2 <<" "<<i3; *outStream << "} computed D"<< ord <<" component: " << vals_host(i1,i2,i3) << " but reference D" << ord << " component: 0 \n"; } } } } } catch (std::logic_error err) { *outStream << err.what() << "\n\n"; errorFlag = -1000; }; if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; // reset format state of std::cout std::cout.copyfmt(oldFormatState); return errorFlag; }
Type Foam::fieldValues::cellSource::processValues ( const Field<Type>& values, const scalarField& V, const scalarField& weightField ) const { Type result = pTraits<Type>::zero; switch (operation_) { case opSum: { result = sum(values); break; } case opAverage: { result = sum(values)/values.size(); break; } case opWeightedAverage: { result = sum(values)/sum(weightField); break; } case opVolAverage: { result = sum(values*V)/sum(V); break; } case opVolIntegrate: { result = sum(values*V); break; } case opMin: { result = min(values); break; } case opMax: { result = max(values); break; } case opCoV: { Type meanValue = sum(values*V)/sum(V); const label nComp = pTraits<Type>::nComponents; for (direction d=0; d<nComp; ++d) { scalarField vals(values.component(d)); scalar mean = component(meanValue, d); scalar& res = setComponent(result, d); res = sqrt(sum(V*sqr(vals - mean))/(V.size()*sum(V)))/mean; } break; } default: { // Do nothing } } return result; }