diff --git a/NonLinearSolver/modelReduction/DeepMaterialNetworks.cpp b/NonLinearSolver/modelReduction/DeepMaterialNetworks.cpp index 27d6a83bf22fe7872d8c83f79f2b53fb5cbc2de4..a9a5f35f4397ecce35dbd620b72591c4e49eb495 100644 --- a/NonLinearSolver/modelReduction/DeepMaterialNetworks.cpp +++ b/NonLinearSolver/modelReduction/DeepMaterialNetworks.cpp @@ -2544,6 +2544,12 @@ DeepMaterialNetwork::DeepMaterialNetwork(const Tree& T): _T(&T), _isInitialized( #if defined(HAVE_PETSC) || defined(HAVE_GMM) _sys = NULL; #endif // + + if (_T->checkDuplicate()) + { + Msg::Error("Duplicating node exists, the computation cannot continue !!!"); + Msg::Exit(0); + } }; @@ -2609,7 +2615,7 @@ void DeepMaterialNetwork::initializeOnlineEvaluation() _T->getBackTrackingPath(_leaves[i],path); _backTrackingMap[_leaves[i]] = path; }; - printf("size of void leaves = %d\n",_voidLeaves.size()); + printf("size of void leaves = %ld\n",_voidLeaves.size()); // leaf map printf("creating associated leaves map\n"); @@ -2700,7 +2706,7 @@ void DeepMaterialNetwork::initializeOnlineEvaluation() } }; - printf("size of void _notUnknownRegularNodes = %d\n",_notUnknownRegularNodes.size()); + printf("size of void _notUnknownRegularNodes = %ld\n",_notUnknownRegularNodes.size()); }; void DeepMaterialNetwork::getKeys(const TreeNode* node, std::vector<Dof>& keys) const diff --git a/NonLinearSolver/modelReduction/Tree.cpp b/NonLinearSolver/modelReduction/Tree.cpp index 3858f487aaaed0cc52d0b58fbab1fe51096f678a..e00abd926827ab13449157c16b9af3d2b7b99711 100644 --- a/NonLinearSolver/modelReduction/Tree.cpp +++ b/NonLinearSolver/modelReduction/Tree.cpp @@ -958,6 +958,34 @@ int Tree::getNumberOfNodes() const return allNodes.size(); }; +bool Tree::checkDuplicate() const +{ + bool ok = false; + nodeContainer allNodes; + getAllNodes(allNodes); + std::set<int> nodeId; + for (int i=0; i< allNodes.size(); i++) + { + const TreeNode* node = allNodes[i]; + std::set<int>::const_iterator itF = nodeId.find(node->getNodeId()); + if (itF == nodeId.end()) + { + nodeId.insert(node->getNodeId()); + } + else + { + Msg::Error("node duplicating:"); + node->printData(); + ok = true; + } + } + if (!ok) + { + Msg::Info("no duplicatint node is found !!!"); + } + return ok; +} + bool Tree::removeZeroLeaves(double tol, bool iteration) { bool ok = false; @@ -1014,94 +1042,67 @@ bool Tree::removeZeroLeaves(double tol, bool iteration) otherChild->parent = parent->parent; otherChild->childOrder = parent->childOrder; grandParent->childs[parent->childOrder] = otherChild; + + // decrease all associated nodes by 1 + std::vector<TreeNode*> allAssociatedNodesToChild; + getRefToAssociatedNodes(otherChild,allAssociatedNodesToChild); + for (int j=0; j< allAssociatedNodesToChild.size(); j++) + { + std::vector<int> loc; + getMaxLocationByDepth(loc); + allAssociatedNodesToChild[j]->depth--; + allAssociatedNodesToChild[j]->location=loc[allAssociatedNodesToChild[j]->depth]+1; + }; + Msg::Info("child weight = %e otherChild weight = %e parent weight = %e",leaf->af->getVal(leaf->weight),otherChild->af->getVal(otherChild->weight),parent->weight); } else if (parent->childs.size() > 2) { Msg::Info("----------------------------------"); parent->printData(); - for (int i=0; i< parent->childs.size(); i++) - { - parent->childs[i]->printData(); - } - - std::vector<TreeNode*> newChilds; - bool found = false; - int removePos = -1; - for (int i=0; i< parent->childs.size(); i++) - { - if (parent->childs[i] == leaf) - { - found = true; - removePos = i; - } - if (!found) - { - newChilds.push_back(parent->childs[i]); - } - else - { - for (int j=i+1; j<parent->childs.size(); j++) - { - TreeNode* n = parent->childs[j]; - n->childOrder = j-1; - newChilds.push_back(n); - }; - break; - } - }; - // + std::vector<TreeNode*> newChilds; std::vector<double> newDirection; - if (removePos > -1) + // + int numberNormal = parent->childs.size()-1; + int totalNumberDirVars = parent->direction.size(); + int numberVarsPerNormal = totalNumberDirVars/numberNormal; + + for (int j=0; j< parent->childs.size(); j++) { - int numberNormal = parent->childs.size()-1; - int totalNumberDirVars = parent->direction.size(); - int numberVarsPerNormal = totalNumberDirVars/numberNormal; - // - if (removePos == 0) - { - for (int j=numberVarsPerNormal; j< parent->direction.size(); j++) - { - newDirection.push_back(parent->direction[j]); - } - } - else if (removePos == parent->childs.size()-1) + if (parent->childs[j]->location != leaf->location) { - for (int j=0; j< parent->direction.size()-numberVarsPerNormal; j++) - { - newDirection.push_back(parent->direction[j]); - } - } - else - { - for (int j=0; j< (removePos-1)*numberVarsPerNormal; j++) - { - newDirection.push_back(parent->direction[j]); - } - for (int j=(removePos)*numberVarsPerNormal; j< parent->direction.size(); j++) + newChilds.push_back(parent->childs[j]); + if (newChilds.size() > 1) { - newDirection.push_back(parent->direction[j]); + if (numberVarsPerNormal == 1) + { + newDirection.push_back(parent->direction[j-1]); + } + else if (numberVarsPerNormal == 2) + { + newDirection.push_back(parent->direction[(j-1)*numberVarsPerNormal]); + newDirection.push_back(parent->direction[(j-1)*numberVarsPerNormal+1]); + } + else + { + Msg::Error("numberVarsPerNormal = %d is not correct !!!",numberVarsPerNormal); + Msg::Exit(0); + } } } - Msg::Info("old size = %d, new size = %d",parent->direction.size(),newDirection.size()); } - else - { - Msg::Error("leaf is not removed correctly"); - Msg::Exit(0); - }; - - parent->childs = newChilds; - parent->direction = newDirection; - - Msg::Info("after removing"); + Msg::Info("\nSTART removing "); parent->printData(); - for (int i=0; i< parent->childs.size(); i++) + parent->childs = newChilds; + for (int j=0; j <parent->childs.size(); j++) { - parent->childs[i]->printData(); + parent->childs[j]->childOrder = j; } - Msg::Info("----------------------------------"); + parent->direction = newDirection; + Msg::Info("------------------"); + parent->printData(); + Msg::Info("DONE removing\n"); } else { @@ -1414,7 +1415,8 @@ bool Tree::removeLeavesWithZeroContribution(double tol) Msg::Error("removeLeavesWithZeroContribution: maximal number of iterations (%d) reaches", maxIterRemove); Msg::Exit(0); } - bool okLeaf = removeZeroBranches(tol); + //bool okLeaf = removeZeroBranches(tol); + bool okLeaf = removeZeroLeaves(tol,true); bool okMerge = treeMerging(); // check if (okLeaf || okLeaf) @@ -1485,6 +1487,22 @@ void Tree::printPhaseFraction() const allPhase.print("all phase fraction:"); }; +void Tree::printLeafFraction() const +{ + nodeContainer allLeaves; + getAllLeaves(allLeaves); + double total = getNonNegativeWeight(_root); + printf("print fraction at leaf:\n"); + int ord=0; + for (int i=0; i< allLeaves.size(); i++) + { + const TreeNode* leaf = allLeaves[i]; + printf("loc %d: %d %d, fraction = %.5e\n",ord,leaf->depth,leaf->location,getNonNegativeWeight(leaf)/total); + ord++; + } + printf("done printing:\n"); +}; + void Tree::printTree() const { printf("Tree print \n"); @@ -2112,7 +2130,7 @@ void Tree::clear() _root = NULL; }; -void Tree::initialize(bool rand) +void Tree::initialize(bool rand, bool normalizedWeight) { // random weight for leaves unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); @@ -2137,23 +2155,28 @@ void Tree::initialize(bool rand) for (int i=0; i< allLeaves.size(); i++) { TreeNode* n = allLeaves[i]; - n->weight = getVal(minWeight,maxWeight); + double randVal = getVal(minWeight,maxWeight); + n->weight = n->af->getReciprocalVal(randVal); toltalWeightLeaves += n->af->getVal(n->weight); } + for (int i=0; i< allLeaves.size(); i++) { TreeNode* n = allLeaves[i]; - double vv = (n->af->getVal(n->weight))/toltalWeightLeaves; - n->weight *= n->af->getReciprocalVal(vv); + if (normalizedWeight) + { + double vv = (n->af->getVal(n->weight))/toltalWeightLeaves; + n->weight = n->af->getReciprocalVal(vv); + } // propagate data double w = n->weight; - while (n->parent != NULL) + TreeNode* parent = n->parent; + while (parent != NULL) { - n->parent->weight += n->af->getVal(w); - n = n->parent; + parent->weight += n->af->getVal(w); + parent = parent->parent; }; }; - //printf("pi = %e\n",pi); double minAlpha = 0.; double maxAlpha = 1; diff --git a/NonLinearSolver/modelReduction/Tree.h b/NonLinearSolver/modelReduction/Tree.h index dfaff94d743e2c810b69e96968dc6c43957465ab..2f775afd5068e61beaa40ecd4409930e1fae1493 100644 --- a/NonLinearSolver/modelReduction/Tree.h +++ b/NonLinearSolver/modelReduction/Tree.h @@ -59,7 +59,7 @@ class Tree void printTree() const; void printTreeInteraction(const std::string fname="treeInteraction.txt", bool colorMat = true, int dir=1) const; void clear(); - void initialize(bool rand=true); + void initialize(bool rand=true, bool normalizedWeight=true); const TreeNode* getRootNode() const {return _root;}; void assignMaterialLaws(int numPhases); @@ -72,8 +72,10 @@ class Tree void saveDataToFile(std::string filename) const; void loadDataFromFile(std::string filename); int getNumberOfNodes() const; + bool checkDuplicate() const; double getPhaseFraction(int i) const; void printPhaseFraction() const; + void printLeafFraction() const; void printNodeFraction() const; bool removeLeavesWithZeroContribution(double tol=1e-6);