00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <DomainPartitioner.h>
00035
00036 #include <stdlib.h>
00037 #include <GraphPartitioner.h>
00038 #include <PartitionedDomain.h>
00039 #include <Subdomain.h>
00040 #include <SubdomainIter.h>
00041 #include <Node.h>
00042 #include <Element.h>
00043 #include <SP_Constraint.h>
00044 #include <MP_Constraint.h>
00045 #include <NodeIter.h>
00046 #include <ElementIter.h>
00047 #include <MP_ConstraintIter.h>
00048 #include <SP_ConstraintIter.h>
00049 #include <Vertex.h>
00050 #include <VertexIter.h>
00051 #include <Graph.h>
00052 #include <Vector.h>
00053 #include <NodalLoad.h>
00054 #include <ElementalLoad.h>
00055 #include <NodalLoadIter.h>
00056 #include <ElementalLoadIter.h>
00057 #include <LoadBalancer.h>
00058 #include <LoadPatternIter.h>
00059 #include <LoadPattern.h>
00060
00061 #include <Timer.h>
00062
00063 #include <MapOfTaggedObjects.h>
00064
00065 class NodeLocations: public TaggedObject
00066 {
00067 public:
00068 NodeLocations(int tag);
00069 void Print(OPS_Stream &s, int flag =0);
00070 int addPartition(int partition);
00071 ID nodePartitions;
00072 int numPartitions;
00073 };
00074
00075
00076
00077 NodeLocations::NodeLocations(int tag)
00078 :TaggedObject(tag),
00079 nodePartitions(0,1),
00080 numPartitions(0)
00081 {
00082
00083 }
00084
00085 void
00086 NodeLocations::Print(OPS_Stream &s, int flag)
00087 {
00088 s << "NodeLocations tag: " << this->getTag() << " partitions: " << nodePartitions;
00089 }
00090
00091 int
00092 NodeLocations::addPartition(int partition)
00093 {
00094 if (nodePartitions.insert(partition) != 1)
00095 numPartitions++;
00096 return 0;
00097 }
00098
00099 DomainPartitioner::DomainPartitioner(GraphPartitioner &theGraphPartitioner)
00100 :myDomain(0),thePartitioner(theGraphPartitioner),theBalancer(0),
00101 theElementGraph(0), theBoundaryElements(0),
00102 theNodeLocations(0),elementPlace(0), numPartitions(0), partitionFlag(false), usingMainDomain(false)
00103 {
00104
00105 }
00106
00107 DomainPartitioner::DomainPartitioner(GraphPartitioner &theGraphPartitioner,
00108 LoadBalancer &theLoadBalancer)
00109 :myDomain(0),thePartitioner(theGraphPartitioner),theBalancer(&theLoadBalancer),
00110 theElementGraph(0), theBoundaryElements(0),
00111 theNodeLocations(0),elementPlace(0), numPartitions(0), partitionFlag(false), usingMainDomain(false)
00112 {
00113
00114 theLoadBalancer.setLinks(*this);
00115 }
00116
00117
00118 DomainPartitioner::~DomainPartitioner()
00119 {
00120 if (theBoundaryElements != 0) {
00121 for (int i=0; i<numPartitions; i++)
00122 if (theBoundaryElements[i] != 0)
00123 delete theBoundaryElements[i];
00124 delete []theBoundaryElements;
00125 }
00126 }
00127
00128 void
00129 DomainPartitioner::setPartitionedDomain(PartitionedDomain &theDomain)
00130 {
00131 myDomain = &theDomain;
00132 }
00133
00134 int
00135 DomainPartitioner::partition(int numParts, bool usingMain, int mainPartitionTag)
00136 {
00137 usingMainDomain = usingMain;
00138 mainPartition = mainPartitionTag;
00139
00140
00141
00142 for (int i=1; i<=numParts; i++) {
00143 if (i != mainPartition) {
00144 Subdomain *subdomainPtr = myDomain->getSubdomainPtr(i);
00145 if (subdomainPtr == 0) {
00146 opserr << "DomainPartitioner::partition - No Subdomain: ";
00147 opserr << i << " exists\n";
00148 return -1;
00149 }
00150 }
00151 }
00152
00153
00154
00155
00156 theElementGraph = &(myDomain->getElementGraph());
00157
00158 int theError = thePartitioner.partition(*theElementGraph, numParts);
00159 if (theError < 0) {
00160 opserr << "DomainPartitioner::partition";
00161 opserr << " - the graph partioner failed to partition the ";
00162 opserr << "element graph\n";
00163 return -10+theError;
00164 }
00165
00166
00167
00168
00169
00170
00171
00172 if (theBoundaryElements != 0)
00173 delete [] theBoundaryElements;
00174
00175 theBoundaryElements = new Graph * [numParts];
00176 if (theBoundaryElements == 0) {
00177 opserr << "DomainPartitioner::partition(int numParts)";
00178 opserr << " - ran out of memory\n";
00179 numPartitions = 0;
00180 return -1;
00181 }
00182
00183 for (int l=0; l<numParts; l++) {
00184 theBoundaryElements[l] = new Graph(2048);
00185
00186 if (theBoundaryElements[l] == 0) {
00187 opserr << "DomainPartitioner::partition(int numParts)";
00188 opserr << " - ran out of memory\n";
00189 numPartitions = 0;
00190 return -1;
00191 }
00192 }
00193
00194 numPartitions = numParts;
00195
00196
00197
00198
00199 theNodeLocations = new MapOfTaggedObjects();
00200 if (theNodeLocations == 0) {
00201 opserr << "DomainPartitioner::partition(int numParts)";
00202 opserr << " - ran out of memory creating MapOfTaggedObjectStorage for node locations\n";
00203 numPartitions = 0;
00204 return -1;
00205 }
00206
00207 NodeIter &theNodes = myDomain->getNodes();
00208 Node *nodePtr;
00209 while ((nodePtr = theNodes()) != 0) {
00210 NodeLocations *theNodeLocation = new NodeLocations(nodePtr->getTag());
00211 if (theNodeLocation == 0) {
00212 opserr << "DomainPartitioner::partition(int numParts)";
00213 opserr << " - ran out of memory creating NodeLocation for node: " << nodePtr->getTag() << endln;
00214 numPartitions = 0;
00215 return -1;
00216 }
00217 if (theNodeLocations->addComponent(theNodeLocation) == false) {
00218 opserr << "DomainPartitioner::partition(int numParts)";
00219 opserr << " - failed to add NodeLocation to Map for Node: " << nodePtr->getTag() << endln;
00220 numPartitions = 0;
00221 return -1;
00222 }
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 VertexIter &theVertexIter = theElementGraph->getVertices();
00235 Vertex *vertexPtr;
00236 while ((vertexPtr = theVertexIter()) != 0) {
00237 int eleTag = vertexPtr->getRef();
00238 int vertexColor = vertexPtr->getColor();
00239
00240 const ID &adjacency = vertexPtr->getAdjacency();
00241 int size = adjacency.Size();
00242 for (int i=0; i<size; i++) {
00243 Vertex *otherVertex = theElementGraph->getVertexPtr(adjacency(i));
00244 if (otherVertex->getColor() != vertexColor) {
00245 theBoundaryElements[vertexColor-1]->addVertex(vertexPtr,false);
00246 i = size;
00247 }
00248 }
00249
00250 Element *elePtr = myDomain->getElement(eleTag);
00251 const ID &nodes = elePtr->getExternalNodes();
00252 size = nodes.Size();
00253 for (int j=0; j<size; j++) {
00254 int nodeTag = nodes(j);
00255 TaggedObject *theTaggedObject = theNodeLocations->getComponentPtr(nodeTag);
00256 if (theTaggedObject == 0) {
00257 opserr << "DomainPartitioner::partition(int numParts)";
00258 opserr << " - failed to find NodeLocation in Map for Node: " << nodePtr->getTag() << " -- A BUG!!\n";
00259 numPartitions = 0;
00260 return -1;
00261 }
00262 NodeLocations *theNodeLocation = (NodeLocations *)theTaggedObject;
00263 theNodeLocation->addPartition(vertexColor);
00264 }
00265 }
00266
00267
00268
00269 MP_ConstraintIter &theMPs = myDomain->getMPs();
00270 MP_Constraint *mpPtr;
00271 while ((mpPtr = theMPs()) != 0) {
00272 int retained = mpPtr->getNodeRetained();
00273 int constrained = mpPtr->getNodeConstrained();
00274
00275 TaggedObject *theRetainedObject = theNodeLocations->getComponentPtr(retained);
00276 TaggedObject *theConstrainedObject = theNodeLocations->getComponentPtr(constrained);
00277
00278 if (theRetainedObject == 0 || theConstrainedObject == 0) {
00279 opserr << "DomainPartitioner::partition(int numParts)";
00280 if (theRetainedObject == 0)
00281 opserr << " - failed to find NodeLocation in Map for Node: " << retained << " -- A BUG!!\n";
00282 if (theConstrainedObject == 0)
00283 opserr << " - failed to find NodeLocation in Map for Node: " << constrained << " -- A BUG!!\n";
00284 numPartitions = 0;
00285 return -1;
00286 }
00287
00288 NodeLocations *theRetainedLocation = (NodeLocations *)theRetainedObject;
00289 NodeLocations *theConstrainedLocation = (NodeLocations *)theConstrainedObject;
00290 ID &theConstrainedNodesPartitions = theConstrainedLocation->nodePartitions;
00291 int numPartitions = theConstrainedNodesPartitions.Size();
00292 for (int i=0; i<numPartitions; i++) {
00293 theRetainedLocation->addPartition(theConstrainedNodesPartitions(i));
00294 }
00295 }
00296
00297
00298 TaggedObjectIter &theNodeLocationIter = theNodeLocations->getComponents();
00299 TaggedObject *theNodeObject;
00300 while ((theNodeObject = theNodeLocationIter()) != 0) {
00301 NodeLocations *theNodeLocation = (NodeLocations *)theNodeObject;
00302
00303 int nodeTag = theNodeLocation->getTag();
00304 ID &nodePartitions = theNodeLocation->nodePartitions;
00305 int numPartitions = theNodeLocation->numPartitions;
00306
00307 for (int i=0; i<numPartitions; i++) {
00308 int partition = nodePartitions(i);
00309 if (partition != mainPartition) {
00310 Subdomain *theSubdomain = myDomain->getSubdomainPtr(partition);
00311 if (numPartitions == 1) {
00312 Node *nodePtr = myDomain->removeNode(nodeTag);
00313 theSubdomain->addNode(nodePtr);
00314 } else {
00315 Node *nodePtr = myDomain->getNode(nodeTag);
00316 theSubdomain->addExternalNode(nodePtr);
00317 }
00318 }
00319 }
00320 }
00321
00322
00323 VertexIter &theVertices = theElementGraph->getVertices();
00324 while ((vertexPtr = theVertices()) != 0) {
00325
00326 int partition = vertexPtr->getColor();
00327 if (partition != mainPartition) {
00328 int eleTag = vertexPtr->getRef();
00329 Element *elePtr = myDomain->removeElement(eleTag);
00330 Subdomain *theSubdomain = myDomain->getSubdomainPtr(partition);
00331 theSubdomain->addElement(elePtr);
00332 }
00333 }
00334
00335
00336
00337
00338
00339
00340 LoadPatternIter &theLoadPatterns = myDomain->getLoadPatterns();
00341 LoadPattern *theLoadPattern;
00342 while ((theLoadPattern = theLoadPatterns()) != 0) {
00343 int loadPatternTag = theLoadPattern->getTag();
00344
00345
00346
00347 for (int i=1; i<=numParts; i++) {
00348 if (i != mainPartition) {
00349 Subdomain *theSubdomain = myDomain->getSubdomainPtr(i);
00350 LoadPattern *loadPatternCopy = theSubdomain->getLoadPattern(loadPatternTag);
00351 if (loadPatternCopy == 0) {
00352 LoadPattern *newLoadPattern = theLoadPattern->getCopy();
00353 if (newLoadPattern == 0) {
00354 opserr << "DomaiPartitioner::partition - out of memory creating LoadPatterns\n";
00355 return -1;
00356 }
00357 theSubdomain->addLoadPattern(newLoadPattern);
00358 }
00359 }
00360 }
00361
00362
00363
00364
00365 NodalLoadIter &theNodalLoads = theLoadPattern->getNodalLoads();
00366 NodalLoad *theNodalLoad;
00367 while ((theNodalLoad = theNodalLoads()) != 0) {
00368 int nodeTag = theNodalLoad->getNodeTag();
00369
00370 TaggedObject *theTaggedObject = theNodeLocations->getComponentPtr(nodeTag);
00371 if (theTaggedObject == 0) {
00372 opserr << "DomainPartitioner::partition(int numParts)";
00373 opserr << " - failed to find NodeLocation in Map for Node: " << nodeTag << " -- A BUG!!\n";
00374 numPartitions = 0;
00375 return -1;
00376 }
00377
00378 NodeLocations *theNodeLocation = (NodeLocations *)theTaggedObject;
00379 ID &nodePartitions = theNodeLocation->nodePartitions;
00380 int numPartitions = theNodeLocation->numPartitions;
00381 for (int i=0; i<numPartitions; i++) {
00382 int partition = nodePartitions(i);
00383 if (partition != mainPartition) {
00384 if (numPartitions == 1) {
00385 Subdomain *theSubdomain = myDomain->getSubdomainPtr(partition);
00386 theLoadPattern->removeNodalLoad(theNodalLoad->getTag());
00387 if ((theSubdomain->addNodalLoad(theNodalLoad, loadPatternTag)) != true)
00388 opserr << "DomainPartitioner::partition() - failed to add Nodal Load\n";
00389 }
00390 }
00391 }
00392 }
00393
00394 SP_ConstraintIter &theSPs = theLoadPattern->getSPs();
00395 SP_Constraint *spPtr;
00396 while ((spPtr = theSPs()) != 0) {
00397 int nodeTag = spPtr->getNodeTag();
00398
00399 TaggedObject *theTaggedObject = theNodeLocations->getComponentPtr(nodeTag);
00400 if (theTaggedObject == 0) {
00401 opserr << "DomainPartitioner::partition(int numParts)";
00402 opserr << " - failed to find NodeLocation in Map for Node: " << nodeTag << " -- A BUG!!\n";
00403 numPartitions = 0;
00404 return -1;
00405 }
00406
00407 NodeLocations *theNodeLocation = (NodeLocations *)theTaggedObject;
00408 ID &nodePartitions = theNodeLocation->nodePartitions;
00409 int numPartitions = theNodeLocation->numPartitions;
00410 for (int i=0; i<numPartitions; i++) {
00411 int partition = nodePartitions(i);
00412 if (partition != mainPartition) {
00413 Subdomain *theSubdomain = myDomain->getSubdomainPtr(partition);
00414 if (numPartitions == 1)
00415 theLoadPattern->removeSP_Constraint(spPtr->getTag());
00416 int res = theSubdomain->addSP_Constraint(spPtr, loadPatternTag);
00417 if (res < 0)
00418 opserr << "DomainPartitioner::partition() - failed to add SP Constraint\n";
00419 }
00420 }
00421 }
00422
00423 ElementalLoadIter &theLoads = theLoadPattern->getElementalLoads();
00424 ElementalLoad *theLoad;
00425 while ((theLoad = theLoads()) != 0) {
00426 opserr << "DomainPartitioner::partition - REMOVE ELEMENTAL LOADS\n";
00427
00428
00429
00430
00431 }
00432 }
00433
00434
00435
00436 SP_ConstraintIter &theDomainSP = myDomain->getSPs();
00437 SP_Constraint *spPtr;
00438 while ((spPtr = theDomainSP()) != 0) {
00439 int nodeTag = spPtr->getNodeTag();
00440
00441 TaggedObject *theTaggedObject = theNodeLocations->getComponentPtr(nodeTag);
00442 if (theTaggedObject == 0) {
00443 opserr << "DomainPartitioner::partition(int numParts)";
00444 opserr << " - failed to find NodeLocation in Map for Node: " << nodeTag << " -- A BUG!!\n";
00445 numPartitions = 0;
00446 return -1;
00447 }
00448
00449 NodeLocations *theNodeLocation = (NodeLocations *)theTaggedObject;
00450 ID &nodePartitions = theNodeLocation->nodePartitions;
00451 int numPartitions = theNodeLocation->numPartitions;
00452 for (int i=0; i<numPartitions; i++) {
00453 int partition = nodePartitions(i);
00454
00455 if (partition != mainPartition) {
00456 Subdomain *theSubdomain = myDomain->getSubdomainPtr(partition);
00457 if (numPartitions == 1) {
00458 myDomain->removeSP_Constraint(spPtr->getTag());
00459 }
00460 int res = theSubdomain->addSP_Constraint(spPtr);
00461 if (res < 0)
00462 opserr << "DomainPartitioner::partition() - failed to add SP Constraint\n";
00463 }
00464 }
00465 }
00466
00467
00468 MP_ConstraintIter &moreMPs = myDomain->getMPs();
00469 while ((mpPtr = moreMPs()) != 0) {
00470 int constrained = mpPtr->getNodeConstrained();
00471 TaggedObject *theConstrainedObject = theNodeLocations->getComponentPtr(constrained);
00472 NodeLocations *theConstrainedLocation = (NodeLocations *)theConstrainedObject;
00473 ID &theConstrainedNodesPartitions = theConstrainedLocation->nodePartitions;
00474 int numPartitions = theConstrainedLocation->numPartitions;
00475 for (int i=0; i<numPartitions; i++) {
00476 int partition = theConstrainedNodesPartitions(i);
00477 if (partition != mainPartition) {
00478 Subdomain *theSubdomain = myDomain->getSubdomainPtr(partition);
00479 if (numPartitions == 1)
00480 myDomain->removeMP_Constraint(mpPtr->getTag());
00481 int res = theSubdomain->addMP_Constraint(mpPtr);
00482 if (res < 0)
00483 opserr << "DomainPartitioner::partition() - failed to add MP Constraint\n";
00484 }
00485 }
00486 }
00487
00488
00489
00490
00491 SubdomainIter &theSubDomains = myDomain->getSubdomains();
00492 Subdomain *theSubDomain;
00493 while ((theSubDomain = theSubDomains()) != 0)
00494 theSubDomain->domainChange();
00495
00496
00497 myDomain->domainChange();
00498
00499
00500 partitionFlag = true;
00501
00502 return 0;
00503 }
00504
00505
00506 int
00507 DomainPartitioner::balance(Graph &theWeightedPGraph)
00508 {
00509 int res = 0;
00510
00511
00512 if (partitionFlag == false) {
00513 opserr << "DomainPartitioner::balance(const Vector &load)";
00514 opserr << " - not partitioned or DomainPartitioner did not partition\n";
00515 return -1;
00516 }
00517
00518 if (theBalancer != 0) {
00519
00520
00521 res = theBalancer->balance(theWeightedPGraph);
00522
00523
00524 SubdomainIter &theSubDomains = myDomain->getSubdomains();
00525 Subdomain *theSubDomain;
00526
00527 while ((theSubDomain = theSubDomains()) != 0)
00528 theSubDomain->domainChange();
00529
00530
00531 myDomain->domainChange();
00532 }
00533
00534 return res;
00535 }
00536
00537
00538
00539 int
00540 DomainPartitioner::getNumPartitions(void) const
00541 {
00542 return numPartitions;
00543 }
00544
00545
00546
00547 Graph &
00548 DomainPartitioner::getPartitionGraph(void)
00549 {
00550 if (myDomain == 0) {
00551 opserr << "ERROR: DomainPartitioner::getPartitionGraph(void)";
00552 opserr << " - No domain has been set";
00553 exit(-1);
00554 }
00555 return myDomain->getSubdomainGraph();
00556 }
00557
00558 Graph &
00559 DomainPartitioner::getColoredGraph(void)
00560 {
00561 if (myDomain == 0) {
00562 opserr << "ERROR: DomainPartitioner::getPartitionGraph(void)";
00563 opserr << " - No domain has been set";
00564 exit(0);
00565 }
00566
00567 return myDomain->getElementGraph();
00568 }
00569
00570
00571 int
00572 DomainPartitioner::swapVertex(int from, int to, int vertexTag,
00573 bool adjacentVertexNotInOther)
00574 {
00575 opserr << "DomainPartitioner::swapVertex() " << from << " " << to << " " << vertexTag << endln;
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899 return 0;
00900
00901 }
00902
00903
00904
00905
00906
00907
00908 int
00909 DomainPartitioner::swapBoundary(int from, int to, bool adjacentVertexNotInOther)
00910
00911 {
00912 opserr << "DomainPartitioner::swapBoundary: from " << from << " to " << to << endln;
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285 return 0;
01286 }
01287
01288
01289 int
01290 DomainPartitioner::releaseVertex(int from,
01291 int vertexTag,
01292 Graph &theWeightedPartitionGraph,
01293 bool mustReleaseToLighter,
01294 double factorGreater,
01295 bool adjacentVertexNotInOther)
01296 {
01297
01298 if (partitionFlag == false) {
01299 opserr << "DomainPartitioner::balance(const Vector &load)";
01300 opserr << " - not partitioned or DomainPartitioner did not partition\n";
01301 return -1;
01302 }
01303
01304
01305 Subdomain *fromSubdomain = myDomain->getSubdomainPtr(from);
01306 if (fromSubdomain == 0) {
01307 opserr << "DomainPartitioner::swapVertex - No from Subdomain: ";
01308 opserr << from << " exists\n";
01309 return -1;
01310 }
01311
01312
01313
01314 Graph *fromBoundary = theBoundaryElements[from-1];
01315
01316 Vertex *vertexPtr = fromBoundary->getVertexPtr(vertexTag);
01317 if (vertexPtr == 0)
01318 vertexPtr = theElementGraph->getVertexPtr(vertexTag);
01319
01320 if (vertexPtr == 0)
01321 return -3;
01322
01323 ID attraction(numPartitions+1);
01324 attraction.Zero();
01325
01326
01327 const ID &adjacent = vertexPtr->getAdjacency();
01328 int numAdjacent = adjacent.Size();
01329 for (int i=0; i<numAdjacent; i++) {
01330 int otherTag = adjacent(i);
01331 Vertex *otherVertex = theElementGraph->getVertexPtr(otherTag);
01332 int otherPartition = otherVertex->getColor();
01333 if (otherPartition != from)
01334 attraction(otherPartition) += 1;
01335 }
01336
01337
01338 int partition = 1;
01339 int maxAttraction = attraction(1);
01340 for (int j=2; j<=numPartitions; j++)
01341 if (attraction(j) > maxAttraction) {
01342 partition = j;
01343 maxAttraction = attraction(j);
01344 }
01345
01346
01347 if (mustReleaseToLighter == false)
01348 return swapVertex(from, partition, vertexTag, adjacentVertexNotInOther);
01349
01350 else {
01351 Vertex *fromVertex = theWeightedPartitionGraph.getVertexPtr(from);
01352 Vertex *toVertex = theWeightedPartitionGraph.getVertexPtr(partition);
01353
01354 double fromWeight = fromVertex->getWeight();
01355 double toWeight = toVertex->getWeight();
01356
01357 if (fromWeight == toWeight)
01358 opserr << "DomainPartitioner::releaseVertex - TO CHANGE >= to >\n";
01359
01360 if (fromWeight >= toWeight) {
01361 if (toWeight == 0.0)
01362 return swapVertex(from,partition,vertexTag,adjacentVertexNotInOther);
01363 if (fromWeight/toWeight > factorGreater)
01364 return swapVertex(from,partition,vertexTag,adjacentVertexNotInOther);
01365 }
01366 }
01367
01368 return 0;
01369 }
01370
01371
01372
01373 int
01374 DomainPartitioner::releaseBoundary(int from,
01375 Graph &theWeightedPartitionGraph,
01376 bool mustReleaseToLighter,
01377 double factorGreater,
01378 bool adjacentVertexNotInOther)
01379 {
01380
01381 if (partitionFlag == false) {
01382 opserr << "DomainPartitioner::balance(const Vector &load)";
01383 opserr << " - not partitioned or DomainPartitioner did not partition\n";
01384 return -1;
01385 }
01386
01387
01388 Subdomain *fromSubdomain = myDomain->getSubdomainPtr(from);
01389 if (fromSubdomain == 0) {
01390 opserr << "DomainPartitioner::swapVertex - No from Subdomain: ";
01391 opserr << from << " exists\n";
01392 return -1;
01393 }
01394
01395
01396 Graph *fromBoundary = theBoundaryElements[from-1];
01397
01398
01399
01400
01401
01402 Graph *swapVertices = new Graph(fromBoundary->getNumVertex());
01403
01404 VertexIter &swappableVertices = fromBoundary->getVertices();
01405 Vertex *vertexPtr;
01406
01407 while ((vertexPtr = swappableVertices()) != 0)
01408 swapVertices->addVertex(vertexPtr,false);
01409
01410
01411 VertexIter &verticesToSwap = swapVertices->getVertices();
01412 while ((vertexPtr = verticesToSwap()) != 0)
01413 releaseVertex(from,
01414 vertexPtr->getTag(),
01415 theWeightedPartitionGraph,
01416 mustReleaseToLighter,
01417 factorGreater,
01418 adjacentVertexNotInOther);
01419
01420 delete swapVertices;
01421
01422 return 0;
01423 }
01424