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
|
#include "PoissonSolver.h"
#include "ATC_Coupling.h"
#include "FE_Engine.h"
#include "PhysicsModel.h"
#include "PrescribedDataManager.h"
#include "LinearSolver.h"
#include <utility>
#include <iostream>
using std::pair;
namespace ATC {
// ====================================================================
// PoissonSolver
// ====================================================================
PoissonSolver::PoissonSolver(
const FieldName fieldName,
const PhysicsModel * physicsModel,
const FE_Engine * feEngine,
const PrescribedDataManager * prescribedDataMgr,
/*const*/ ATC_Coupling * atc,
const Array2D<bool> & rhsMask,
const int solverType,
bool parallel
)
: atc_(atc),
feEngine_(feEngine),
prescribedDataMgr_(prescribedDataMgr),
physicsModel_(physicsModel),
fieldName_(fieldName),
rhsMask_(rhsMask),
linear_(false),
solver_(nullptr),
solverNL_(nullptr),
tangent_(nullptr),
solverType_(solverType),
solverTol_(0),
solverMaxIter_(0),
integrationType_(FULL_DOMAIN),
parallel_(parallel)
{
if (physicsModel_->has_linear_rhs(fieldName)) {
linear_ = true;
rhsMask_(fieldName,FLUX) = false;
}
else {
rhsMask_(fieldName,FLUX) = true;
rhsMask_(fieldName,SOURCE) = true;
}
if (prescribedDataMgr_->has_robin_source(fieldName)) {
rhsMask_(fieldName,ROBIN_SOURCE) = true;
}
}
// --------------------------------------------------------------------
PoissonSolver::~PoissonSolver()
{
if (tangent_) delete tangent_;
if (solverNL_) delete solverNL_;
if (solver_) delete solver_;
}
// --------------------------------------------------------------------
// Parser
// --------------------------------------------------------------------
bool PoissonSolver::modify(int /* narg */, char **arg)
{
bool match = false;
/*! \page man_poisson_solver fix_modify AtC poisson_solver
\section syntax
fix_modify AtC poisson_solver mesh create <nx> <ny> <nz> <region-id>
<f|p> <f|p> <f|p>
- nx ny nz = number of elements in x, y, z
- region-id = id of region that is to be meshed
- f p p = perioidicity flags for x, y, z
\section examples
<TT> fix_modify AtC poisson_solver mesh create 10 1 1 feRegion p p p </TT>
\section description
Creates a uniform mesh in a rectangular region
\section restrictions
creates only uniform rectangular grids in a rectangular region
\section related
\section default
none
*/
int argIdx = 0;
if (strcmp(arg[argIdx],"poisson_solver")==0) {
argIdx++;
if (strcmp(arg[argIdx],"mesh")==0) {
argIdx++;
// create a FE_Engine
//feEngine_ = new FE_Engine(this); need alternate constructor?
// send args to new engine
// arg[0] = "mesh";
// arg[1] = "create";
// feEngine_->modify(narg,arg);
}
}
return match;
}
// --------------------------------------------------------------------
// Initialize
// --------------------------------------------------------------------
void PoissonSolver::initialize(void)
{
nNodes_ = feEngine_->num_nodes();
if (atc_->source_atomic_quadrature(fieldName_))
integrationType_ = FULL_DOMAIN_ATOMIC_QUADRATURE_SOURCE;
// compute penalty for Dirichlet boundaries
if (prescribedDataMgr_->none_fixed(fieldName_))
throw ATC_Error("Poisson solver needs Dirichlet data");
const BC_SET & bcs = (prescribedDataMgr_->bcs(fieldName_))[0];
if (linear_) { // constant rhs
if (! solver_ ) {
pair<FieldName,FieldName> row_col(fieldName_,fieldName_);
Array2D <bool> rhsMask(NUM_FIELDS,NUM_FLUX);
rhsMask = false; rhsMask(fieldName_,FLUX) = true;
if (prescribedDataMgr_->has_robin_source(fieldName_)) {
rhsMask(fieldName_,ROBIN_SOURCE) = true;
}
// compute stiffness for Poisson solve
atc_->compute_rhs_tangent(row_col, rhsMask, atc_->fields(),
stiffness_, FULL_DOMAIN, physicsModel_);
// create solver
solver_ = new LinearSolver(stiffness_,bcs,solverType_,LinearSolver::AUTO_HANDLE_CONSTRAINTS,parallel_);
}
else {
// re-initialize
solver_->initialize(&bcs);
}
if (solverTol_) solver_->set_tolerance(solverTol_);
if (solverMaxIter_) solver_->set_max_iterations(solverMaxIter_);
}
else {
// print_mask(rhsMask_);
if ( solverNL_ ) delete solverNL_;
tangent_ = new PhysicsModelTangentOperator(atc_,physicsModel_, rhsMask_, integrationType_, fieldName_);
solverNL_ = new NonLinearSolver(tangent_,&bcs,0,parallel_);
if (solverTol_) solverNL_->set_residual_tolerance(solverTol_);
if (solverMaxIter_) solverNL_->set_max_iterations(solverMaxIter_);
}
}
// --------------------------------------------------------------------
// Solve
// --------------------------------------------------------------------
bool PoissonSolver::solve(FIELDS & fields, FIELDS & rhs)
{
atc_->compute_rhs_vector(rhsMask_, fields, rhs,
integrationType_, physicsModel_);
CLON_VEC f = column(fields[fieldName_].set_quantity(),0);
CLON_VEC r = column(rhs[fieldName_].quantity(),0);
bool converged = false;
if (linear_) {converged = solver_->solve(f,r);}
else {converged = solverNL_->solve(f);}
if (atc_->source_atomic_quadrature(fieldName_)
&& LammpsInterface::instance()->atom_charge() ) set_charges(fields);
return converged;
}
bool PoissonSolver::solve(DENS_MAT & field, const DENS_MAT & rhs)
{
CLON_VEC f = column(field,0);
CLON_VEC r = column(rhs,0);
bool converged = false;
if (linear_) {converged = solver_->solve(f,r);}
else {converged = solverNL_->solve(f);}
if (atc_->source_atomic_quadrature(fieldName_)
&& LammpsInterface::instance()->atom_charge() ) set_charges(atc_->fields());
return converged;
}
// --------------------------------------------------------------------
// set charges on atoms
// --------------------------------------------------------------------
void PoissonSolver::set_charges(FIELDS & fields)
{
FIELD_MATS sources;
atc_->compute_sources_at_atoms(rhsMask_, fields, physicsModel_,sources);
FIELD_MATS::const_iterator nField = sources.find(fieldName_);
if (nField != sources.end()) {
const DENS_MAT & electronCharges = nField->second;
double * q = LammpsInterface::instance()->atom_charge();
int nLocal = atc_->nlocal();
if (nLocal > 0) {
const Array<int> & i2a = atc_->internal_to_atom_map();
for (int i=0; i < nLocal; i++) {
int atomIdx = i2a(i);
q[atomIdx] = -electronCharges(i,0);
}
}
}
}
} // namespace ATC
|