MtlOptModel::MtlOptModel(const string initf){ // load mtl JsonFilePaser jsonf; TEST_ASSERT ( jsonf.open(initf) ); TEST_ASSERT ( jsonf.read("T",T) ); TEST_ASSERT ( jsonf.read("h",h) ); TEST_ASSERT ( jsonf.read("alpha_k",alphaK) ); TEST_ASSERT ( jsonf.read("alpha_m",alphaM) ); // read modes, and lambda VectorXd t_lambda; TEST_ASSERT ( jsonf.readVecFile("eigenvalues",t_lambda) ); double lambda0_scale = 1.0f; if ( jsonf.read("lambda0_scale", lambda0_scale) ){ assert_gt(lambda0_scale,0); t_lambda *= lambda0_scale; } int number_modes = 0; TEST_ASSERT ( jsonf.read("rw", number_modes) ); if (number_modes <= 0) number_modes = t_lambda.size(); testModeId.resize(number_modes); for (int i = 0; i < number_modes; ++i) testModeId[i] = i; lambda.resize(testModeId.size()); ASSERT_LE(testModeId.size(),t_lambda.size()); for (int i = 0; i < testModeId.size(); ++i) lambda[i] = t_lambda[testModeId[i]]; if(!jsonf.read("rs",r_s)){ r_s = lambda.size(); } // keyframe constraints int fix_head = 0, fix_end = 0; TEST_ASSERT ( jsonf.read("fix_head", fix_head) ); TEST_ASSERT ( jsonf.read("fix_end", fix_end) ); Kid.clear(); for (int i = 0; i < fix_head; ++i){ Kid.push_back(i); } for (int i = T-fix_end; i < T; ++i){ Kid.push_back(i); } this->Kz.resize(selectedRedDim(),Kid.size()); this->Kz.setZero(); // partial constraints partialConPenalty = 10.0f; string partial_con_str; if (jsonf.readFilePath("partial_con",partial_con_str)){ TEST_ASSERT ( jsonf.read("con_penalty",partialConPenalty) ); PartialConstraintsSet AllConNodes; TEST_ASSERT ( AllConNodes.load(partial_con_str) ); const set<pPartialConstraints> &con_groups=AllConNodes.getPartialConSet(); BOOST_FOREACH(pPartialConstraints pc, con_groups){ if ( pc != NULL && !pc->isEmpty() ) { vector<int> conNod; VectorXd conPos; pc->getPartialCon(conNod,conPos); conFrames.push_back(pc->getFrameId()); conNodes.push_back(conNod); uc.push_back(conPos); } } }
int main(int argc, char *argv[]){ // check if (argc < 2){ ERROR_LOG("ussage: extend_basis ini_json_file_name"); return -1; } JsonFilePaser jsonf; if(!jsonf.open(argv[1])){ ERROR_LOG("failed to open json file: " << argv[1]); return -1; } // load data INFO_LOG("loading data...."); pTetMesh tet_mesh = pTetMesh(new TetMesh()); string tet_file, elastic_mtl; if(jsonf.readFilePath("vol_file", tet_file)){ if(!tet_mesh->load(tet_file)){ ERROR_LOG("failed to load tet mesh file: " << tet_file); return -1; }else if(jsonf.readFilePath("elastic_mtl", elastic_mtl)){ const bool s = tet_mesh->loadElasticMtl(elastic_mtl); ERROR_LOG_COND("failed to load the elastic material from: "<<elastic_mtl,s); } } set<int> fixed_nodes; jsonf.readVecFile("fixed_nodes", fixed_nodes,UTILITY::TEXT); INFO_LOG("number of fixed nodes: " << fixed_nodes.size()); int num_linear_modes = fixed_nodes.size()>0 ? 15:20; jsonf.read("num_linear_modes",num_linear_modes,num_linear_modes); INFO_LOG("number of linear modes: " << num_linear_modes); /// compute full K, M INFO_LOG("computing full K, M...."); ElasticForceTetFullStVK ela(tet_mesh); ela.prepare(); VectorXd x0; tet_mesh->nodes(x0); SparseMatrix<double> K = ela.K(x0)*(-1.0f); MassMatrix mass; DiagonalMatrix<double,-1> diagM; mass.compute(diagM,*tet_mesh); /// remove fixed nodes INFO_LOG("remove fixed dofs in K, M...."); SparseMatrix<double> P; EIGEN3EXT::genReshapeMatrix(K.rows(),3,fixed_nodes,P); K = P*(K*P.transpose()); const SparseMatrix<double> M = P*(diagM*P.transpose()); /// solve general eigen value problem for W, lambda INFO_LOG("solving general eigen value problem K*x = la*M*x...."); MatrixXd W; VectorXd lambda; const SparseMatrix<double> Klower = getLower(K); if(!EigenSparseGenEigenSolver::solve(Klower,M,W,lambda,num_linear_modes)){ ERROR_LOG("failed to solve the general eigen value problem."); return -1; } if (fixed_nodes.size() > 0){ W = P.transpose()*W; } // extend basis W to obtain B INFO_LOG("extending basis...."); MatrixXd B; if (fixed_nodes.size() > 0){ ExtendModalBasis::construct(W, B); }else{ ExtendModalBasis::construct(W, x0, B); } // save string save_b; jsonf.readFilePath("subspace_basis",save_b,string("B.b"),false); INFO_LOG("save extended basis to: "<< save_b); if(!write(save_b, B)){ ERROR_LOG("failed to save the extended basis to: " << save_b); } return 0; }