1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
|
// ATC Headers
#include "AtomicRegulator.h"
#include "ATC_Error.h"
#include "ATC_Coupling.h"
#include "PrescribedDataManager.h"
#include "TimeIntegrator.h"
#include "LinearSolver.h"
using std::map;
using std::string;
using std::set;
using std::pair;
namespace ATC {
// only one regulator method at time, i.e. fixed & flux, thermo & elastic
// regulator manages lambda variables, creates new ones when requested with dimensions and zero ics (map of tag to lambda)
// regulator keeps track of which lambda are being used, unused lambdas deleted (map of tag to bool), all tags set to unused on start of initialization
// method requests needed lambda from regulator
// method sets up all needed linear solvers, null linear solver does nothing
// regulator adds nodes to fixed or fluxed lists it owns, based on localization and type
// method gets lists of fixed nodes and fluxed nodes
// method lumps fluxed lambdas and truncates fixed lambdas based on single localized bool in regulator
// inherited methods should be fixed, fluxed, combined
//--------------------------------------------------------
//--------------------------------------------------------
// Class AtomicRegulator
//--------------------------------------------------------
//--------------------------------------------------------
//--------------------------------------------------------
// Constructor
//--------------------------------------------------------
AtomicRegulator::AtomicRegulator(ATC_Coupling * atc,
const string & regulatorPrefix) :
atc_(atc),
howOften_(1),
needReset_(true),
maxIterations_(myMaxIterations),
tolerance_(myTolerance),
regulatorTarget_(NONE),
couplingMode_(UNCOUPLED),
nNodes_(0),
nsd_(atc_->nsd()),
nLocal_(0),
useLocalizedLambda_(false),
useLumpedLambda_(false),
timeFilter_(nullptr),
regulatorMethod_(nullptr),
boundaryIntegrationType_(NO_QUADRATURE),
regulatorPrefix_(regulatorPrefix)
{
applyInDirection_.resize(atc_->nsd(),true);
}
//--------------------------------------------------------
// Destructor
//--------------------------------------------------------
AtomicRegulator::~AtomicRegulator()
{
delete_method();
set_all_data_to_unused();
delete_unused_data();
}
//--------------------------------------------------------
// delete_method:
// deletes the method
//--------------------------------------------------------
void AtomicRegulator::delete_method()
{
if (regulatorMethod_)
delete regulatorMethod_;
}
//--------------------------------------------------------
// delete_unused_data:
// deletes all data that is currently not in use
//--------------------------------------------------------
void AtomicRegulator::delete_unused_data()
{
map<string, pair<bool,DENS_MAN * > >::iterator it;
for (it = regulatorData_.begin(); it != regulatorData_.end(); it++) {
if (((it->second).first)) {
delete (it->second).second;
regulatorData_.erase(it);
}
}
}
//--------------------------------------------------------
// get_regulator_data:
// gets a pointer to the requested data, is crated if
// if doesn't exist
//--------------------------------------------------------
DENS_MAN * AtomicRegulator::regulator_data(const string tag, int nCols)
{
DENS_MAN * data(nullptr);
map<string, pair<bool,DENS_MAN * > >::iterator it = regulatorData_.find(tag);
if (it == regulatorData_.end()) {
data = new DENS_MAN(nNodes_,nCols);
regulatorData_.insert(pair<string, pair<bool,DENS_MAN * > >(tag,pair<bool,DENS_MAN * >(false,data)));
}
else {
data = (it->second).second;
if ((data->nRows() != nNodes_) || (data->nCols() != nCols)) {
data->reset(nNodes_,nCols);
}
(it->second).first = false;
}
return data;
}
//--------------------------------------------------------
// get_regulator_data:
// gets a pointer to the requested data, or nullptr if
// if doesn't exist
//--------------------------------------------------------
const DENS_MAN * AtomicRegulator::regulator_data(const string tag) const
{
map<string, pair<bool,DENS_MAN * > >::const_iterator it = regulatorData_.find(tag);
if (it == regulatorData_.end()) {
return nullptr;
}
else {
return const_cast<DENS_MAN * >((it->second).second);
}
}
//--------------------------------------------------------
// set_all_data_to_unused:
// sets bool such that all data is unused
//--------------------------------------------------------
void AtomicRegulator::set_all_data_to_unused()
{
map<string, pair<bool,DENS_MAN * > >::iterator it;
for (it = regulatorData_.begin(); it != regulatorData_.end(); it++) {
(it->second).first = true;
}
}
//--------------------------------------------------------
// set_all_data_to_used:
// sets bool such that all data is used
//--------------------------------------------------------
void AtomicRegulator::set_all_data_to_used()
{
map<string, pair<bool,DENS_MAN * > >::iterator it;
for (it = regulatorData_.begin(); it != regulatorData_.end(); it++) {
(it->second).first = false;
}
}
//--------------------------------------------------------
// modify:
// parses and adjusts controller state based on
// user input, in the style of LAMMPS user input
//--------------------------------------------------------
bool AtomicRegulator::modify(int /* narg */, char **arg)
{
bool foundMatch = false;
// set parameters for numerical matrix solutions
/*! \page man_control fix_modify AtC control
\section syntax
fix_modify AtC control <physics_type> <solution_parameter> <value>\n
- physics_type (string) = thermal | momentum\n
- solution_parameter (string) = max_iterations | tolerance\n
fix_modify AtC transfer <physics_type> control max_iterations <max_iterations>\n
- max_iterations (int) = maximum number of iterations that will be used by iterative matrix solvers\n
fix_modify AtC transfer <physics_type> control tolerance <tolerance> \n
- tolerance (float) = relative tolerance to which matrix equations will be solved\n
\section examples
<TT> fix_modify AtC control thermal max_iterations 10 </TT> \n
<TT> fix_modify AtC control momentum tolerance 1.e-5 </TT> \n
\section description
Sets the numerical parameters for the matrix solvers used in the specified control algorithm. Many solution approaches require iterative solvers, and these methods enable users to provide the maximum number of iterations and the relative tolerance.
\section restrictions
only for be used with specific controllers :
thermal, momentum \n
They are ignored if a lumped solution is requested
\section related
\section default
max_iterations is the number of rows in the matrix\n
tolerance is 1.e-10
*/
int argIndex = 0;
if (strcmp(arg[argIndex],"max_iterations")==0) {
argIndex++;
maxIterations_ = atoi(arg[argIndex]);
if (maxIterations_ < 1) {
throw ATC_Error("Bad maximum iteration count");
}
needReset_ = true;
foundMatch = true;
}
else if (strcmp(arg[argIndex],"tolerance")==0) {
argIndex++;
tolerance_ = atof(arg[argIndex]);
if (tolerance_ < 0.) {
throw ATC_Error("Bad tolerance value");
}
needReset_ = true;
foundMatch = true;
}
/*! \page man_localized_lambda fix_modify AtC control localized_lambda
\section syntax
fix_modify AtC control localized_lambda <on|off>
\section examples
<TT> fix_modify atc control localized_lambda on </TT> \n
\section description
Turns on localization algorithms for control algorithms to restrict the influence of FE coupling or boundary conditions to a region near the boundary of the MD region. Control algorithms will not affect atoms in elements not possessing faces on the boundary of the region. Flux-based control is localized via row-sum lumping while quantity control is done by solving a truncated matrix equation.
\section restrictions
\section related
\section default
Default is off.
*/
else if (strcmp(arg[argIndex],"localized_lambda")==0) {
argIndex++;
if (strcmp(arg[argIndex],"on")==0) {
useLocalizedLambda_ = true;
foundMatch = true;
}
else if (strcmp(arg[argIndex],"off")==0) {
useLocalizedLambda_ = false;
foundMatch = true;
}
}
/*! \page man_lumped_lambda_solve fix_modify AtC control lumped_lambda_solve
\section syntax
fix_modify AtC control lumped_lambda_solve <on|off>
\section examples
<TT> fix_modify atc control lumped_lambda_solve on </TT> \n
\section description
Command to use or not use lumped matrix for lambda solve
\section restrictions
\section related
\section default
*/
else if (strcmp(arg[argIndex],"lumped_lambda_solve")==0) {
argIndex++;
if (strcmp(arg[argIndex],"on")==0) {
useLumpedLambda_ = true;
foundMatch = true;
}
else if (strcmp(arg[argIndex],"off")==0) {
useLumpedLambda_ = false;
foundMatch = true;
}
}
/*! \page man_mask_direction fix_modify AtC control mask_direction
\section syntax
fix_modify AtC control mask_direction <direction> <on|off>
\section examples
<TT> fix_modify atc control mask_direction 0 on </TT> \n
\section description
Command to mask out certain dimensions from the atomic regulator
\section restrictions
\section related
\section default
*/
else if (strcmp(arg[argIndex],"mask_direction")==0) {
argIndex++;
int dir = atoi(arg[argIndex]);
argIndex++;
if (strcmp(arg[argIndex],"on")==0) {
applyInDirection_[dir] = false;
foundMatch = true;
}
else if (strcmp(arg[argIndex],"off")==0) {
applyInDirection_[dir] = true;
foundMatch = true;
}
}
return foundMatch;
}
//--------------------------------------------------------
// reset_nlocal:
// resizes lambda force if necessary
//--------------------------------------------------------
void AtomicRegulator::reset_nlocal()
{
nLocal_ = atc_->nlocal();
if (regulatorMethod_)
regulatorMethod_->reset_nlocal();
}
//--------------------------------------------------------
// reset_atom_materials:
// resets the localized atom to material map
//--------------------------------------------------------
void AtomicRegulator::reset_atom_materials(const Array<int> & elementToMaterialMap,
const MatrixDependencyManager<DenseMatrix, int> * atomElement)
{
if (regulatorMethod_)
regulatorMethod_->reset_atom_materials(elementToMaterialMap,
atomElement);
}
//--------------------------------------------------------
// reset_method:
// sets up methods, if necessary
//--------------------------------------------------------
void AtomicRegulator::reset_method()
{
// set up defaults for anything that didn't get set
if (!regulatorMethod_)
regulatorMethod_ = new RegulatorMethod(this);
if (!timeFilter_)
timeFilter_ = (atc_->time_filter_manager())->construct();
}
//--------------------------------------------------------
// md_fixed_nodes:
// determines if any fixed nodes overlap the MD region
//--------------------------------------------------------
bool AtomicRegulator::md_fixed_nodes(FieldName fieldName) const
{
FixedNodes fixedNodes(atc_,fieldName);
const set<int> & myNodes(fixedNodes.quantity());
if (myNodes.size() == 0) {
return false;
}
else {
return true;
}
}
//--------------------------------------------------------
// md_flux_nodes:
// determines if any nodes with fluxes overlap the MD region
//--------------------------------------------------------
bool AtomicRegulator::md_flux_nodes(FieldName fieldName) const
{
FluxNodes fluxNodes(atc_,fieldName);
const set<int> & myNodes(fluxNodes.quantity());
if (myNodes.size() == 0) {
return false;
}
else {
return true;
}
}
//--------------------------------------------------------
// construct_methods:
// sets up methods before a run
//--------------------------------------------------------
void AtomicRegulator::construct_methods()
{
// get base-line data that was set in stages 1 & 2 of ATC_Method::initialize
// computational geometry
nNodes_ = atc_->num_nodes();
// make sure consistent boundary integration is being used
atc_->set_boundary_integration_type(boundaryIntegrationType_);
}
//--------------------------------------------------------
// construct_transfers:
// pass through to appropriate transfer constuctors
//--------------------------------------------------------
void AtomicRegulator::construct_transfers()
{
regulatorMethod_->construct_transfers();
}
//--------------------------------------------------------
// initialize:
// sets up methods before a run
//--------------------------------------------------------
void AtomicRegulator::initialize()
{
regulatorMethod_->initialize();
needReset_ = false;
}
//--------------------------------------------------------
// output:
// pass through to appropriate output methods
//--------------------------------------------------------
void AtomicRegulator::output(OUTPUT_LIST & outputData) const
{
regulatorMethod_->output(outputData);
}
//--------------------------------------------------------
// finish:
// pass through to appropriate end-of-run methods
//--------------------------------------------------------
void AtomicRegulator::finish()
{
regulatorMethod_->finish();
set_all_data_to_unused();
}
//--------------------------------------------------------
// apply_pre_predictor:
// applies the controller in the pre-predictor
// phase of the time integrator
//--------------------------------------------------------
void AtomicRegulator::apply_pre_predictor(double dt, int timeStep)
{
if (timeStep % howOften_==0) // apply full integration scheme, including filter
regulatorMethod_->apply_pre_predictor(dt);
}
//--------------------------------------------------------
// apply_mid_predictor:
// applies the controller in the mid-predictor
// phase of the time integrator
//--------------------------------------------------------
void AtomicRegulator::apply_mid_predictor(double dt, int timeStep)
{
if (timeStep % howOften_==0) // apply full integration scheme, including filter
regulatorMethod_->apply_mid_predictor(dt);
}
//--------------------------------------------------------
// apply_post_predictor:
// applies the controller in the post-predictor
// phase of the time integrator
//--------------------------------------------------------
void AtomicRegulator::apply_post_predictor(double dt, int timeStep)
{
if (timeStep % howOften_==0) // apply full integration scheme, including filter
regulatorMethod_->apply_post_predictor(dt);
}
//--------------------------------------------------------
// apply_pre_corrector:
// applies the controller in the pre-corrector phase
// of the time integrator
//--------------------------------------------------------
void AtomicRegulator::apply_pre_corrector(double dt, int timeStep)
{
if (timeStep % howOften_==0) // apply full integration scheme, including filter
regulatorMethod_->apply_pre_corrector(dt);
}
//--------------------------------------------------------
// apply_post_corrector:
// applies the controller in the post-corrector phase
// of the time integrator
//--------------------------------------------------------
void AtomicRegulator::apply_post_corrector(double dt, int timeStep)
{
if (timeStep % howOften_==0) // apply full integration scheme, including filter
regulatorMethod_->apply_post_corrector(dt);
}
//--------------------------------------------------------
// pre_exchange
//--------------------------------------------------------
void AtomicRegulator::pre_exchange()
{
regulatorMethod_->pre_exchange();
}
//--------------------------------------------------------
// pre_force
//--------------------------------------------------------
void AtomicRegulator::pre_force()
{
regulatorMethod_->post_exchange();
}
//--------------------------------------------------
// pack_fields
// bundle all allocated field matrices into a list
// for output needs
//--------------------------------------------------
void AtomicRegulator::pack_fields(RESTART_LIST & data)
{
map<string, pair<bool,DENS_MAN * > >::iterator it;
for (it = regulatorData_.begin(); it != regulatorData_.end(); it++) {
data[(it->first)] = &(((it->second).second)->set_quantity());
}
}
//--------------------------------------------------------
// compute_boundary_flux:
// computes the boundary flux to be consistent with
// the controller
//--------------------------------------------------------
void AtomicRegulator::compute_boundary_flux(FIELDS & fields)
{
regulatorMethod_->compute_boundary_flux(fields);
}
//--------------------------------------------------------
// add_to_rhs:
// adds any controller contributions to the FE rhs
//--------------------------------------------------------
void AtomicRegulator::add_to_rhs(FIELDS & rhs)
{
regulatorMethod_->add_to_rhs(rhs);
}
//--------------------------------------------------------
//--------------------------------------------------------
// Class RegulatorMethod
//--------------------------------------------------------
//--------------------------------------------------------
//--------------------------------------------------------
// Constructor
//--------------------------------------------------------
RegulatorMethod::RegulatorMethod(AtomicRegulator * atomicRegulator,
const string & regulatorPrefix) :
atomicRegulator_(atomicRegulator),
atc_(atomicRegulator_->atc_transfer()),
boundaryFlux_(atc_->boundary_fluxes()),
fieldMask_(NUM_FIELDS,NUM_FLUX),
nNodes_(atomicRegulator_->num_nodes()),
regulatorPrefix_(atomicRegulator->regulator_prefix()+regulatorPrefix),
shpFcnDerivs_(nullptr)
{
fieldMask_ = false;
}
//--------------------------------------------------------
// compute_boundary_flux
// default computation of boundary flux based on
// finite
//--------------------------------------------------------
void RegulatorMethod::compute_boundary_flux(FIELDS & fields)
{
atc_->compute_boundary_flux(fieldMask_,
fields,
boundaryFlux_,
atomMaterialGroups_,
shpFcnDerivs_);
}
//--------------------------------------------------------
//--------------------------------------------------------
// Class RegulatorShapeFunction
//--------------------------------------------------------
//--------------------------------------------------------
//--------------------------------------------------------
// Constructor
//--------------------------------------------------------
RegulatorShapeFunction::RegulatorShapeFunction(AtomicRegulator * atomicRegulator,
const string & regulatorPrefix) :
RegulatorMethod(atomicRegulator,regulatorPrefix),
lambda_(nullptr),
atomLambdas_(nullptr),
shapeFunctionMatrix_(nullptr),
linearSolverType_(AtomicRegulator::NO_SOLVE),
maxIterations_(atomicRegulator->max_iterations()),
tolerance_(atomicRegulator->tolerance()),
matrixSolver_(nullptr),
regulatedNodes_(nullptr),
applicationNodes_(nullptr),
boundaryNodes_(nullptr),
shpFcn_(nullptr),
atomicWeights_(nullptr),
elementMask_(nullptr),
lambdaAtomMap_(nullptr),
weights_(nullptr),
nsd_(atomicRegulator_->nsd()),
nLocal_(atomicRegulator_->nlocal())
{
// do nothing
}
//--------------------------------------------------------
// Destructor
//--------------------------------------------------------
RegulatorShapeFunction::~RegulatorShapeFunction()
{
if (matrixSolver_)
delete matrixSolver_;
}
//--------------------------------------------------------
// create_node_maps
// - creates the node mappings between all nodes and the
// subset which are regulated
//--------------------------------------------------------
void RegulatorShapeFunction::create_node_maps()
{
this->construct_regulated_nodes();
InterscaleManager & interscaleManager(atc_->interscale_manager());
nodeToOverlapMap_ = static_cast<NodeToSubset * >(interscaleManager.dense_matrix_int(regulatorPrefix_+"NodeToOverlapMap"));
if (!nodeToOverlapMap_) {
nodeToOverlapMap_ = new NodeToSubset(atc_,regulatedNodes_);
interscaleManager.add_dense_matrix_int(nodeToOverlapMap_,
regulatorPrefix_+"NodeToOverlapMap");
}
overlapToNodeMap_ = static_cast<SubsetToNode * >(interscaleManager.dense_matrix_int(regulatorPrefix_+"OverlapToNodeMap"));
if (!overlapToNodeMap_) {
overlapToNodeMap_ = new SubsetToNode(nodeToOverlapMap_);
interscaleManager.add_dense_matrix_int(overlapToNodeMap_,
regulatorPrefix_+"OverlapToNodeMap");
}
}
//--------------------------------------------------------
// construct_transfers
// - create all the needed transfer operators, in this
// case weights for the lambda matrix
//--------------------------------------------------------
void RegulatorShapeFunction::construct_transfers()
{
this->set_weights(); // construct specific weighting matrix transfer
// specialized quantities for boundary flux integration if the lambda atom map exists
if (lambdaAtomMap_ && (atomicRegulator_->boundary_integration_type() == FE_INTERPOLATION)) {
InterscaleManager & interscaleManager(atc_->interscale_manager());
// atomic weights
PerAtomDiagonalMatrix<double> * atomWeights(interscaleManager.per_atom_diagonal_matrix("AtomVolume"));
atomicWeights_ = new MappedDiagonalMatrix(atc_,
atomWeights,
lambdaAtomMap_);
interscaleManager.add_diagonal_matrix(atomicWeights_,
regulatorPrefix_+"RegulatorAtomWeights");
// shape function
shpFcn_ = new RowMappedSparseMatrix(atc_,
interscaleManager.per_atom_sparse_matrix("Interpolant"),
lambdaAtomMap_);
interscaleManager.add_sparse_matrix(shpFcn_,
regulatorPrefix_+"RegulatorShapeFunction");
// shape function derivatives
VectorDependencyManager<SPAR_MAT * > * interpolantGradient = interscaleManager.vector_sparse_matrix("InterpolantGradient");
if (!interpolantGradient) {
interpolantGradient = new PerAtomShapeFunctionGradient(atc_);
interscaleManager.add_vector_sparse_matrix(interpolantGradient,
"InterpolantGradient");
}
shpFcnDerivs_ = new RowMappedSparseMatrixVector(atc_,
interpolantGradient,
lambdaAtomMap_);
interscaleManager.add_vector_sparse_matrix(shpFcnDerivs_,
regulatorPrefix_+"RegulatorShapeFunctionGradient");
}
}
//--------------------------------------------------------
// initialize
// - pre-run work, in this cases constructs the linear
// solver
//--------------------------------------------------------
void RegulatorShapeFunction::initialize()
{
if (!shapeFunctionMatrix_) {
throw ATC_Error("RegulatorShapeFunction::initialize - shapeFunctionMatrix_ must be created before the initialize phase");
}
if (matrixSolver_)
delete matrixSolver_;
if (linearSolverType_ == AtomicRegulator::RSL_SOLVE) {
matrixSolver_ = new LambdaMatrixSolverLumped(matrixTemplate_,
shapeFunctionMatrix_,
maxIterations_,
tolerance_,
applicationNodes_,
nodeToOverlapMap_);
}
else if (linearSolverType_ == AtomicRegulator::CG_SOLVE) {
matrixSolver_ = new LambdaMatrixSolverCg(matrixTemplate_,
shapeFunctionMatrix_,
maxIterations_,
tolerance_);
}
else {
throw ATC_Error("RegulatorShapeFunction::initialize - unsupported solver type");
}
compute_sparsity();
}
//--------------------------------------------------------
// compute_sparsity
// - creates sparsity template
//--------------------------------------------------------
void RegulatorShapeFunction::compute_sparsity(void)
{
// first get local pattern from N N^T
int nNodeOverlap = nodeToOverlapMap_->size();
DENS_MAT tmpLocal(nNodeOverlap,nNodeOverlap);
DENS_MAT tmp(nNodeOverlap,nNodeOverlap);
const SPAR_MAT & myShapeFunctionMatrix(shapeFunctionMatrix_->quantity());
if (myShapeFunctionMatrix.nRows() > 0) {
tmpLocal = myShapeFunctionMatrix.transMat(myShapeFunctionMatrix);
}
// second accumulate total pattern across processors
LammpsInterface::instance()->allsum(tmpLocal.ptr(), tmp.ptr(), tmp.size());
// third extract non-zero entries & construct sparse template
SPAR_MAT & myMatrixTemplate(matrixTemplate_.set_quantity());
myMatrixTemplate.reset(nNodeOverlap,nNodeOverlap);
for (int i = 0; i < nNodeOverlap; i++) {
for (int j = 0; j < nNodeOverlap; j++) {
if (abs(tmp(i,j))>0) {
myMatrixTemplate.add(i,j,0.);
}
}
}
myMatrixTemplate.compress();
}
//--------------------------------------------------------
// solve_for_lambda
// solves matrix equation for lambda using given rhs
//--------------------------------------------------------
void RegulatorShapeFunction::solve_for_lambda(const DENS_MAT & rhs,
DENS_MAT & lambda)
{
// assemble N^T W N with appropriate weighting matrix
DIAG_MAT weights;
if (shapeFunctionMatrix_->nRows() > 0) {
weights.reset(weights_->quantity());
}
matrixSolver_->assemble_matrix(weights);
// solve on overlap nodes
int nNodeOverlap = nodeToOverlapMap_->size();
DENS_MAT rhsOverlap(nNodeOverlap,rhs.nCols());
map_unique_to_overlap(rhs, rhsOverlap);
DENS_MAT lambdaOverlap(nNodeOverlap,lambda.nCols());
for (int i = 0; i < rhs.nCols(); i++) {
CLON_VEC tempLambda(lambdaOverlap,CLONE_COL,i);
if (atomicRegulator_->apply_in_direction(i)) {
CLON_VEC tempRHS(rhsOverlap,CLONE_COL,i);
matrixSolver_->execute(tempRHS,tempLambda);
}
else {
tempLambda = 0.;
}
}
// map solution back to all nodes
map_overlap_to_unique(lambdaOverlap,lambda);
}
//--------------------------------------------------------
// reset_nlocal:
// resets data dependent on local atom count
//--------------------------------------------------------
void RegulatorShapeFunction::reset_nlocal()
{
RegulatorMethod::reset_nlocal();
nLocal_ = atomicRegulator_->nlocal();
//compute_sparsity();
}
//--------------------------------------------------------
// reset_atom_materials:
// resets the localized atom to material map
//--------------------------------------------------------
void RegulatorShapeFunction::reset_atom_materials(const Array<int> & elementToMaterialMap,
const MatrixDependencyManager<DenseMatrix, int> * atomElement)
{
// specialized quantities for boundary flux integration if the lambda atom map exists
if (lambdaAtomMap_ && (atomicRegulator_->boundary_integration_type() == FE_INTERPOLATION)) {
int nMaterials = (atc_->physics_model())->nMaterials();
atomMaterialGroups_.reset(nMaterials);
const INT_ARRAY & atomToElementMap(atomElement->quantity());
const INT_ARRAY & map(lambdaAtomMap_->quantity());
int idx;
for (int i = 0; i < nLocal_; i++) {
idx = map(i,0);
if (idx > -1) {
atomMaterialGroups_(elementToMaterialMap(atomToElementMap(i,0))).insert(idx);
}
}
}
}
//--------------------------------------------------------
// map_unique_to_overlap:
// maps unique node data to overlap node data
//--------------------------------------------------------
void RegulatorShapeFunction::map_unique_to_overlap(const MATRIX & uniqueData,
MATRIX & overlapData)
{
const INT_ARRAY & nodeToOverlapMap(nodeToOverlapMap_->quantity());
for (int i = 0; i < nNodes_; i++) {
if (nodeToOverlapMap(i,0) > -1) {
for (int j = 0; j < uniqueData.nCols(); j++) {
overlapData(nodeToOverlapMap(i,0),j) = uniqueData(i,j);
}
}
}
}
//--------------------------------------------------------
// map_overlap_to_unique:
// maps overlap node data to unique node data
//--------------------------------------------------------
void RegulatorShapeFunction::map_overlap_to_unique(const MATRIX & overlapData,
MATRIX & uniqueData)
{
const INT_ARRAY & overlapToNodeMap(overlapToNodeMap_->quantity());
uniqueData.resize(nNodes_,overlapData.nCols());
for (int i = 0; i < overlapToNodeMap.size(); i++) {
for (int j = 0; j < overlapData.nCols(); j++) {
uniqueData(overlapToNodeMap(i,0),j) = overlapData(i,j);
}
}
}
//--------------------------------------------------------
// construct_regulated_nodes:
// constructs the set of nodes being regulated
//--------------------------------------------------------
void RegulatorShapeFunction::construct_regulated_nodes()
{
InterscaleManager & interscaleManager(atc_->interscale_manager());
regulatedNodes_ = interscaleManager.set_int("RegulatedNodes");
if (!regulatedNodes_) {
if (!(atomicRegulator_->use_localized_lambda())) {
regulatedNodes_ = new RegulatedNodes(atc_);
}
else {
regulatedNodes_ = new AllRegulatedNodes(atc_);
}
interscaleManager.add_set_int(regulatedNodes_,
regulatorPrefix_+"RegulatedNodes");
}
// application and regulated are same, unless specified
applicationNodes_ = regulatedNodes_;
// boundary and regulated nodes are same, unless specified
boundaryNodes_ = regulatedNodes_;
// special set of boundary elements
if (atomicRegulator_->use_localized_lambda()) {
elementMask_ = interscaleManager.dense_matrix_bool(regulatorPrefix_+"BoundaryElementMask");
if (!elementMask_) {
elementMask_ = new ElementMaskNodeSet(atc_,boundaryNodes_);
interscaleManager.add_dense_matrix_bool(elementMask_,
regulatorPrefix_+"BoundaryElementMask");
}
}
}
//--------------------------------------------------------
// compute_boundary_flux
// default computation of boundary flux based on
// finite
//--------------------------------------------------------
void RegulatorShapeFunction::compute_boundary_flux(FIELDS & fields)
{
atc_->compute_boundary_flux(fieldMask_,
fields,
boundaryFlux_,
atomMaterialGroups_,
shpFcnDerivs_,
shpFcn_,
atomicWeights_,
elementMask_,
boundaryNodes_);
}
//--------------------------------------------------------
//--------------------------------------------------------
// Class LambdaMatrixSolver
//--------------------------------------------------------
//--------------------------------------------------------
//--------------------------------------------------------
// Constructor
// Grab references to necessary data
//--------------------------------------------------------
LambdaMatrixSolver::LambdaMatrixSolver(SPAR_MAN & matrixTemplate, SPAR_MAN * shapeFunctionMatrix, int maxIterations, double tolerance) :
matrixTemplate_(matrixTemplate),
shapeFunctionMatrix_(shapeFunctionMatrix),
maxIterations_(maxIterations),
tolerance_(tolerance)
{
// do nothing
}
//--------------------------------------------------------
// assemble_matrix
// Assemble the matrix using the shape function
// matrices and weights. This improves efficiency
// when multiple solves or iterations are required.
//--------------------------------------------------------
void LambdaMatrixSolver::assemble_matrix(DIAG_MAT & weights)
{
// form matrix : sum_a N_Ia * W_a * N_Ja
SPAR_MAT lambdaMatrixLocal(matrixTemplate_.quantity());
if (weights.nRows()>0)
lambdaMatrixLocal.weighted_least_squares(shapeFunctionMatrix_->quantity(),weights);
// swap contributions
lambdaMatrix_ = matrixTemplate_.quantity();
LammpsInterface::instance()->allsum(lambdaMatrixLocal.ptr(),
lambdaMatrix_.ptr(), lambdaMatrix_.size());
}
//--------------------------------------------------------
//--------------------------------------------------------
// Class LambdaMatrixSolverLumped
//--------------------------------------------------------
//--------------------------------------------------------
//--------------------------------------------------------
// Constructor
// Grab references to necessary data
//--------------------------------------------------------
LambdaMatrixSolverLumped::LambdaMatrixSolverLumped(SPAR_MAN & matrixTemplate, SPAR_MAN * shapeFunctionMatrix, int maxIterations, double tolerance, const SetDependencyManager<int> * applicationNodes, const NodeToSubset * nodeToOverlapMap) :
LambdaMatrixSolver(matrixTemplate,shapeFunctionMatrix,maxIterations,tolerance),
applicationNodes_(applicationNodes),
nodeToOverlapMap_(nodeToOverlapMap)
{
// do nothing
}
//--------------------------------------------------------
// assemble_matrix
// Assemble the matrix using the shape function
// matrices and weights. This improves efficiency
// when multiple solves or iterations are required.
//--------------------------------------------------------
void LambdaMatrixSolverLumped::assemble_matrix(DIAG_MAT & weights)
{
LambdaMatrixSolver::assemble_matrix(weights);
lumpedMatrix_ = lambdaMatrix_.row_sum_lump();
}
void LambdaMatrixSolverLumped::execute(VECTOR & rhs, VECTOR & lambda)
{
// solve lumped equation
const set<int> & applicationNodes(applicationNodes_->quantity());
const INT_ARRAY & nodeToOverlapMap(nodeToOverlapMap_->quantity());
lambda = 0.;
set<int>::const_iterator iset;
for (iset = applicationNodes.begin(); iset != applicationNodes.end(); iset++) {
int node = nodeToOverlapMap(*iset,0);
lambda(node) = rhs(node)/lumpedMatrix_(node,node);
}
}
//--------------------------------------------------------
//--------------------------------------------------------
// Class LambdaMatrixSolverCg
//--------------------------------------------------------
//--------------------------------------------------------
//--------------------------------------------------------
// Constructor
// Grab references to necessary data
//--------------------------------------------------------
LambdaMatrixSolverCg::LambdaMatrixSolverCg(SPAR_MAN & matrixTemplate, SPAR_MAN * shapeFunctionMatrix, int maxIterations, double tolerance) :
LambdaMatrixSolver(matrixTemplate,shapeFunctionMatrix,maxIterations,tolerance)
{
// do nothing
}
void LambdaMatrixSolverCg::execute(VECTOR & rhs, VECTOR & lambda)
{
if (lambdaMatrix_.size()<1)
throw ATC_Error("solver given zero size matrix in LambdaMatrixSolverCg::execute()");
LinearSolver solver(lambdaMatrix_, ATC::LinearSolver::ITERATIVE_SOLVE_SYMMETRIC, true);
int myMaxIt = maxIterations_ > 0 ? maxIterations_ : 2*lambdaMatrix_.nRows();
solver.set_max_iterations(myMaxIt);
solver.set_tolerance(tolerance_);
solver.solve(lambda,rhs);
}
};
|