Complex Complex::operator + (const Complex &c2) const { return Complex(real+c2.real,image+c2.image); }
Complex Complex::cc() const { return Complex(real(), -imag()); }
Complex Complex::operator-(const Complex & a) const { return Complex(real() - a.real(), imag() - a.imag()); }
Complex Complex::operator*(const Complex & b) const { return Complex(re * b.re - im * b.im, im * b.re + b.im * re); }
Complex Complex::operator*(const double & b) const { return Complex(b * real(), b * imag()); }
int ComplexLUDecompose(pcomplex **a, int n, double *vv, int *indx, double *pd) // pcomplex **a; the matrix whose LU-decomposition is wanted // int n; order of a // double *vv; work vector of size n (stores implicit // scaling of each row) // int *indx; => row permutation according to partial // pivoting sequence // double *pd; => 1 if number of row interchanges was even, // -1 if odd (NULL OK) { int i, imax, j, k; double big, dum, temp, d; pcomplex sum, cdum; d = 1.0; imax = 0; // only to shut the compiler up. for (i = 0; i < n; i++) { big = 0.0; for (j = 0; j < n; j++) { if ((temp = Cabs(a[i][j])) > big) big = temp; } if (big == 0.0) { printf("singular matrix in routine ComplexLUDecompose\n"); return 1; } vv[i] = 1.0 / big; } for (j = 0; j < n; j++) { for (i = 0; i < j; i++) { sum = a[i][j]; for (k = 0; k < i; k++) sum = Csub(sum, Cmul(a[i][k], a[k][j])); a[i][j] = sum; } big = 0.0; for (i = j; i < n; i++) { sum = a[i][j]; for (k = 0; k < j; k++) sum = Csub(sum, Cmul(a[i][k], a[k][j])); a[i][j] = sum; dum = vv[i] * Cabs(sum); if (dum >= big) { big = dum; imax = i; } } if (j != imax) { for (k = 0; k < n; k++) { cdum = a[imax][k]; a[imax][k] = a[j][k]; a[j][k] = cdum; } d = -d; vv[imax] = vv[j]; } indx[j] = imax; if (a[j][j].re == 0.0 && a[j][j].im == 0.0) a[j][j] = Complex(1.0e-20, 1.0e-20); if (j != n - 1){ cdum = Cdiv(Complex(1.0, 0.0), a[j][j]); for (i = j + 1; i < n; i++) a[i][j] = Cmul(a[i][j], cdum); } } if (pd != NULL) *pd = d; return 0; }
void cisi(float x, float *ci, float *si) { void nrerror(char error_text[]); int i,k,odd; float a,err,fact,sign,sum,sumc,sums,t,term; fcomplex h,b,c,d,del; t=fabs(x); if (t == 0.0) { *si=0.0; *ci = -1.0/FPMIN; return; } if (t > TMIN) { b=Complex(1.0,t); c=Complex(1.0/FPMIN,0.0); d=h=Cdiv(ONE,b); for (i=2;i<=MAXIT;i++) { a = -(i-1)*(i-1); b=Cadd(b,Complex(2.0,0.0)); d=Cdiv(ONE,Cadd(RCmul(a,d),b)); c=Cadd(b,Cdiv(Complex(a,0.0),c)); del=Cmul(c,d); h=Cmul(h,del); if (fabs(del.r-1.0)+fabs(del.i) < EPS) break; } if (i > MAXIT) nrerror("cf failed in cisi"); h=Cmul(Complex(cos(t),-sin(t)),h); *ci = -h.r; *si=PIBY2+h.i; } else { if (t < sqrt(FPMIN)) { sumc=0.0; sums=t; } else { sum=sums=sumc=0.0; sign=fact=1.0; odd=TRUE; for (k=1;k<=MAXIT;k++) { fact *= t/k; term=fact/k; sum += sign*term; err=term/fabs(sum); if (odd) { sign = -sign; sums=sum; sum=sumc; } else { sumc=sum; sum=sums; } if (err < EPS) break; odd=!odd; } if (k > MAXIT) nrerror("maxits exceeded in cisi"); } *si=sums; *ci=sumc+log(t)+EULER; } if (x < 0.0) *si = -(*si); }
Complex Complex::operator * (const Complex &c) { double real=re_ * c.re() - im_ * c.im(); double image=re_ * c.im() + im_ * c.re(); return Complex(real, image); }
Complex Complex::conj() const { return Complex(re_, -1*im_ ); }
Complex Complex::operator +(const Complex &c) { return Complex (re_ + c.re(), im_ + c.im()); }
Complex Complex::operator -(const Complex &c) { return Complex(re_ - c.re(), im_ - c.im()); }
Complex Complex::operator / (const Complex &c2) const { return Complex(real/c2.real,image/c2.image); }
Complex Complex::operator * (const Complex &c2) const { return Complex(real*c2.real,image*c2.image); }
Complex Complex::operator - (const Complex &c2) const { return Complex(real-c2.real,image-c2.image); }
// / const Complex Complex::operator/(const Complex& rhs) { // how to divide? double real = (this->realpart * rhs.realpart) - (this->imgpart * rhs.imgpart); double img = (this->realpart * rhs.imgpart) + (this->imgpart * rhs.realpart); return Complex(real, img); }
void muxImaginaryZeros(T& fromVec,U& toVec) { toVec.resize(fromVec.size()); for (size_t i=0; i!=toVec.size(); i++) toVec[i] = Complex(fromVec[i],0); }
Complex Complex::complexFromPolar(double rho, double theta) { return Complex(rho * cos(theta), rho * sin(theta)); }
Real MAST::ComplexAssemblyBase::residual_l2_norm(const libMesh::NumericVector<Real>& real, const libMesh::NumericVector<Real>& imag) { START_LOG("complex_solve()", "Residual-L2"); MAST::NonlinearSystem& nonlin_sys = _system->system(); // iterate over each element, initialize it and get the relevant // analysis quantities RealVectorX sol, vec_re; RealMatrixX mat_re; ComplexVectorX delta_sol, vec; ComplexMatrixX mat; std::vector<libMesh::dof_id_type> dof_indices; const libMesh::DofMap& dof_map = nonlin_sys.get_dof_map(); std::unique_ptr<libMesh::NumericVector<Real> > residual_re(nonlin_sys.solution->zero_clone().release()), residual_im(nonlin_sys.solution->zero_clone().release()), localized_base_solution, localized_real_solution(build_localized_vector(nonlin_sys, real).release()), localized_imag_solution(build_localized_vector(nonlin_sys, imag).release()); if (_base_sol) localized_base_solution.reset(build_localized_vector(nonlin_sys, *_base_sol).release()); // if a solution function is attached, initialize it //if (_sol_function) // _sol_function->init( X); libMesh::MeshBase::const_element_iterator el = nonlin_sys.get_mesh().active_local_elements_begin(); const libMesh::MeshBase::const_element_iterator end_el = nonlin_sys.get_mesh().active_local_elements_end(); MAST::ComplexAssemblyElemOperations& ops = dynamic_cast<MAST::ComplexAssemblyElemOperations&>(*_elem_ops); for ( ; el != end_el; ++el) { const libMesh::Elem* elem = *el; dof_map.dof_indices (elem, dof_indices); ops.init(*elem); // get the solution unsigned int ndofs = (unsigned int)dof_indices.size(); sol.setZero(ndofs); delta_sol.setZero(ndofs); vec.setZero(ndofs); mat.setZero(ndofs, ndofs); // set zero velocity for base solution ops.set_elem_velocity(sol); // set the value of the base solution, if provided if (_base_sol) for (unsigned int i=0; i<dof_indices.size(); i++) sol(i) = (*localized_base_solution)(dof_indices[i]); ops.set_elem_solution(sol); // set the value of the small-disturbance solution for (unsigned int i=0; i<dof_indices.size(); i++) delta_sol(i) = Complex((*localized_real_solution)(dof_indices[i]), (*localized_imag_solution)(dof_indices[i])); ops.set_elem_complex_solution(delta_sol); // if (_sol_function) // ops.attach_active_solution_function(*_sol_function); // perform the element level calculations ops.elem_calculations(false, vec, mat); ops.clear_elem(); // ops.detach_active_solution_function(); // add to the real part of the residual vec_re = vec.real(); DenseRealVector v; MAST::copy(v, vec_re); dof_map.constrain_element_vector(v, dof_indices); residual_re->add_vector(v, dof_indices); // now add to the imaginary part of the residual vec_re = vec.imag(); v.zero(); MAST::copy(v, vec_re); dof_map.constrain_element_vector(v, dof_indices); residual_im->add_vector(v, dof_indices); } // if a solution function is attached, clear it if (_sol_function) _sol_function->clear(); residual_re->close(); residual_im->close(); // return the residual Real l2_real = residual_re->l2_norm(), l2_imag = residual_im->l2_norm(); STOP_LOG("complex_solve()", "Residual-L2"); return sqrt(pow(l2_real,2) + pow(l2_imag,2)); }
void Biquad::setAllpassPole(const Complex& pole) { Complex zero = Complex(1, 0) / pole; setZeroPolePairs(zero, pole); }
void MAST::ComplexAssemblyBase:: residual_and_jacobian_field_split (const libMesh::NumericVector<Real>& X_R, const libMesh::NumericVector<Real>& X_I, libMesh::NumericVector<Real>& R_R, libMesh::NumericVector<Real>& R_I, libMesh::SparseMatrix<Real>& J_R, libMesh::SparseMatrix<Real>& J_I) { libmesh_assert(_system); libmesh_assert(_discipline); libmesh_assert(_elem_ops); MAST::NonlinearSystem& nonlin_sys = _system->system(); R_R.zero(); R_I.zero(); J_R.zero(); J_I.zero(); // iterate over each element, initialize it and get the relevant // analysis quantities RealVectorX sol, vec_re; RealMatrixX mat_re; ComplexVectorX delta_sol, vec; ComplexMatrixX mat; std::vector<libMesh::dof_id_type> dof_indices; const libMesh::DofMap& dof_map = _system->system().get_dof_map(); std::unique_ptr<libMesh::NumericVector<Real> > localized_base_solution, localized_real_solution, localized_imag_solution; // localize the base solution, if it was provided if (_base_sol) localized_base_solution.reset(build_localized_vector(nonlin_sys, *_base_sol).release()); // localize sol to real vector localized_real_solution.reset(build_localized_vector(nonlin_sys, X_R).release()); // localize sol to imag vector localized_imag_solution.reset(build_localized_vector(nonlin_sys, X_I).release()); // if a solution function is attached, initialize it //if (_sol_function) // _sol_function->init( X); libMesh::MeshBase::const_element_iterator el = nonlin_sys.get_mesh().active_local_elements_begin(); const libMesh::MeshBase::const_element_iterator end_el = nonlin_sys.get_mesh().active_local_elements_end(); MAST::ComplexAssemblyElemOperations& ops = dynamic_cast<MAST::ComplexAssemblyElemOperations&>(*_elem_ops); for ( ; el != end_el; ++el) { const libMesh::Elem* elem = *el; dof_map.dof_indices (elem, dof_indices); ops.init(*elem); // get the solution unsigned int ndofs = (unsigned int)dof_indices.size(); sol.setZero(ndofs); delta_sol.setZero(ndofs); vec.setZero(ndofs); mat.setZero(ndofs, ndofs); // first set the velocity to be zero ops.set_elem_velocity(sol); // next, set the base solution, if provided if (_base_sol) for (unsigned int i=0; i<dof_indices.size(); i++) sol(i) = (*localized_base_solution)(dof_indices[i]); ops.set_elem_solution(sol); // set the value of the small-disturbance solution for (unsigned int i=0; i<dof_indices.size(); i++) delta_sol(i) = Complex((*localized_real_solution)(dof_indices[i]), (*localized_imag_solution)(dof_indices[i])); ops.set_elem_complex_solution(delta_sol); // if (_sol_function) // physics_elem->attach_active_solution_function(*_sol_function); // perform the element level calculations ops.elem_calculations(true, vec, mat); ops.clear_elem(); vec *= -1.; //physics_elem->detach_active_solution_function(); // extract the real or the imaginary part of the matrix/vector // The complex system of equations // (J_R + i J_I) (x_R + i x_I) + (r_R + i r_I) = 0 // is rewritten as // [ J_R -J_I] {x_R} + {r_R} = {0} // [ J_I J_R] {x_I} + {r_I} = {0} // DenseRealVector v; DenseRealMatrix m; // copy the real part of the residual and Jacobian MAST::copy(v, vec.real()); MAST::copy(m, mat.real()); dof_map.constrain_element_matrix_and_vector(m, v, dof_indices); R_R.add_vector(v, dof_indices); J_R.add_matrix(m, dof_indices); // copy the imag part of the residual and Jacobian v.zero(); m.zero(); MAST::copy(v, vec.imag()); MAST::copy(m, mat.imag()); dof_map.constrain_element_matrix_and_vector(m, v, dof_indices); R_I.add_vector(v, dof_indices); J_I.add_matrix(m, dof_indices); } // if a solution function is attached, clear it //if (_sol_function) // _sol_function->clear(); R_R.close(); R_I.close(); J_R.close(); J_I.close(); libMesh::out << "R_R: " << R_R.l2_norm() << " R_I: " << R_I.l2_norm() << std::endl; }
Complex operator+(const Complex & a, const Complex & b) { return Complex(a.real() + b.real(), a.imag() + b.imag()); }
void MAST::ComplexAssemblyBase:: residual_and_jacobian_blocked (const libMesh::NumericVector<Real>& X, libMesh::NumericVector<Real>& R, libMesh::SparseMatrix<Real>& J, MAST::Parameter* p) { libmesh_assert(_system); libmesh_assert(_discipline); libmesh_assert(_elem_ops); START_LOG("residual_and_jacobian()", "ComplexSolve"); MAST::NonlinearSystem& nonlin_sys = _system->system(); R.zero(); J.zero(); // iterate over each element, initialize it and get the relevant // analysis quantities RealVectorX sol; ComplexVectorX delta_sol, vec; ComplexMatrixX mat, dummy; // get the petsc vector and matrix objects Mat jac_bmat = dynamic_cast<libMesh::PetscMatrix<Real>&>(J).mat(); PetscInt ierr; std::vector<libMesh::dof_id_type> dof_indices; const libMesh::DofMap& dof_map = nonlin_sys.get_dof_map(); const std::vector<libMesh::dof_id_type>& send_list = nonlin_sys.get_dof_map().get_send_list(); std::unique_ptr<libMesh::NumericVector<Real> > localized_base_solution, localized_complex_sol(libMesh::NumericVector<Real>::build(nonlin_sys.comm()).release()); // prepare a send list for localization of the complex solution std::vector<libMesh::dof_id_type> complex_send_list(2*send_list.size()); for (unsigned int i=0; i<send_list.size(); i++) { complex_send_list[2*i ] = 2*send_list[i]; complex_send_list[2*i+1] = 2*send_list[i]+1; } localized_complex_sol->init(2*nonlin_sys.n_dofs(), 2*nonlin_sys.n_local_dofs(), complex_send_list, false, libMesh::GHOSTED); X.localize(*localized_complex_sol, complex_send_list); // localize the base solution, if it was provided if (_base_sol) localized_base_solution.reset(build_localized_vector(nonlin_sys, *_base_sol).release()); // if a solution function is attached, initialize it //if (_sol_function) // _sol_function->init( X); libMesh::MeshBase::const_element_iterator el = nonlin_sys.get_mesh().active_local_elements_begin(); const libMesh::MeshBase::const_element_iterator end_el = nonlin_sys.get_mesh().active_local_elements_end(); MAST::ComplexAssemblyElemOperations& ops = dynamic_cast<MAST::ComplexAssemblyElemOperations&>(*_elem_ops); for ( ; el != end_el; ++el) { const libMesh::Elem* elem = *el; dof_map.dof_indices (elem, dof_indices); ops.init(*elem); // get the solution unsigned int ndofs = (unsigned int)dof_indices.size(); sol.setZero(ndofs); delta_sol.setZero(ndofs); vec.setZero(ndofs); mat.setZero(ndofs, ndofs); // first set the velocity to be zero ops.set_elem_velocity(sol); // next, set the base solution, if provided if (_base_sol) for (unsigned int i=0; i<dof_indices.size(); i++) sol(i) = (*localized_base_solution)(dof_indices[i]); ops.set_elem_solution(sol); // set the value of the small-disturbance solution for (unsigned int i=0; i<dof_indices.size(); i++) { // get the complex block for this dof delta_sol(i) = Complex((*localized_complex_sol)(2*dof_indices[i]), (*localized_complex_sol)(2*dof_indices[i]+1)); } ops.set_elem_complex_solution(delta_sol); // if (_sol_function) // physics_elem->attach_active_solution_function(*_sol_function); // perform the element level calculations ops.elem_calculations(true, vec, mat); // if sensitivity was requested, then ask the element for sensitivity // of the residual if (p) { // set the sensitivity of complex sol to zero delta_sol.setZero(); ops.set_elem_complex_solution_sensitivity(delta_sol); vec.setZero(); ops.elem_sensitivity_calculations(*p, vec); } ops.clear_elem(); //physics_elem->detach_active_solution_function(); // extract the real or the imaginary part of the matrix/vector // The complex system of equations // (J_R + i J_I) (x_R + i x_I) + (r_R + i r_I) = 0 // is rewritten as // [ J_R -J_I] {x_R} + {r_R} = {0} // [ J_I J_R] {x_I} + {r_I} = {0} // DenseRealVector v_R, v_I; DenseRealMatrix m_R, m_I1, m_I2; std::vector<Real> vals(4); // copy the real part of the residual and Jacobian MAST::copy( m_R, mat.real()); MAST::copy(m_I1, mat.imag()); m_I1 *= -1.; // this is the -J_I component MAST::copy(m_I2, mat.imag()); // this is the J_I component MAST::copy( v_R, vec.real()); MAST::copy( v_I, vec.imag()); dof_map.constrain_element_matrix(m_R, dof_indices); dof_map.constrain_element_matrix(m_I1, dof_indices); dof_map.constrain_element_matrix(m_I2, dof_indices); dof_map.constrain_element_vector(v_R, dof_indices); dof_map.constrain_element_vector(v_I, dof_indices); for (unsigned int i=0; i<dof_indices.size(); i++) { R.add(2*dof_indices[i], v_R(i)); R.add(2*dof_indices[i]+1, v_I(i)); for (unsigned int j=0; j<dof_indices.size(); j++) { vals[0] = m_R (i,j); vals[1] = m_I1(i,j); vals[2] = m_I2(i,j); vals[3] = m_R (i,j); ierr = MatSetValuesBlocked(jac_bmat, 1, (PetscInt*)&dof_indices[i], 1, (PetscInt*)&dof_indices[j], &vals[0], ADD_VALUES); } } } // if a solution function is attached, clear it //if (_sol_function) // _sol_function->clear(); R.close(); J.close(); libMesh::out << "R: " << R.l2_norm() << std::endl; STOP_LOG("residual_and_jacobian()", "ComplexSolve"); }
Complex operator*(double a, const Complex & b) { return Complex(a * b.real(), a * b.imag()); }
int main(int argc, char **argv) { try { TCLAP::CmdLine cmd("CEVAL Algorithm", ' ', "0.1"); cmd.add(poly); cmd.add(x_min); cmd.add(x_max); cmd.add(y_min); cmd.add(y_max); cmd.add(use_rb); cmd.add(display); cmd.add(no_use_inclusion); cmd.add(min_box_size); cmd.add(max_box_size); cmd.add(random_poly); cmd.add(rand_degree); cmd.add(rand_seed); // Parse the args. cmd.parse(argc, argv); // Get the value parsed by each arg. //string name = nameArg.getValue(); } catch (TCLAP::ArgException &e) { cerr << "Error : " << e.error() << endl; cerr << "Processing arg : " << e.argId() << endl; return -1; } Polynomial<PolyType> a; if (random_poly.getValue()) { benchmark::GenerateRandom(&a, rand_degree.getValue(), 10 ,rand_seed.getValue()); } else { benchmark::GetPoly(poly.getValue().c_str(), &a); } double max_box_size_d(max_box_size.getValue()); double min_box_size_d(min_box_size.getValue()); Complex min, max; if (use_rb.getValue()) { double min_r, max_r; benchmark::GetRootBounds(a, &min_r, &max_r); min = Complex(min_r, min_r); max = Complex(max_r, max_r); if (display.getValue()) { cout << "Min : " << min << ",Max : " << max << endl; } } else { double x_min_d(x_min.getValue()), x_max_d(x_max.getValue()); double y_min_d(y_min.getValue()), y_max_d(y_max.getValue()); min = Complex(x_min_d, y_min_d); max = Complex(x_max_d, y_max_d); } Box *b = new Box(min, max); Box *b_copy = new Box(min, max); // start timing code. struct timeval start; struct timeval end; gettimeofday(&start, NULL); // end timing code. Predicates p(a); Algorithm algo(p, b, min_box_size_d, !no_use_inclusion.getValue(), display.getValue()); algo.Run(); if (no_use_inclusion.getValue()) { algo.AttemptIsolation(); } // start timing code. gettimeofday(&end, NULL); cout << ",time=" << (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec); if (display.getValue()) { cout << "Operated on Bounding box : " << min << "," << max << endl; cout << "With polynomial : " << endl; //a.dump(); cout << endl; cout << "--------------------------" << endl; cout << "Number of roots:" << algo.output()->size() << endl; list<const Disk *>::const_iterator it = algo.output()->begin(); while (it != algo.output()->end()) { cout << "m= " << (*it)->centre << ", r= " << (*it)->radius << endl; ++it; } display_funcs::SetDisplayParams(b_copy, algo.reject(), algo.output_boxes(), algo.ambiguous()); startGlutLoop(argc, argv); } else { cout << ",output=" << algo.output()->size() << endl; } }
Complex Complex::operator/(double b) const { return Complex(real() / b, imag() / b); }
void MeshData::read_unv_implementation (std::istream& in_file) { /* * This is the actual implementation of * reading in UNV format. This enables * to read either through the conventional * C++ stream, or through a stream that * allows to read .gz'ed files. */ if ( !in_file.good() ) { libMesh::err << "ERROR: Input file not good." << std::endl; libmesh_error(); } const std::string _label_dataset_mesh_data = "2414"; /* * locate the beginning of data set * and read it. */ { std::string olds, news; while (true) { in_file >> olds >> news; /* * Yes, really dirty: * * When we found a dataset, and the user does * not want this dataset, we jump back here */ go_and_find_the_next_dataset: /* * a "-1" followed by a number means the beginning of a dataset * stop combing at the end of the file */ while( ((olds != "-1") || (news == "-1") ) && !in_file.eof() ) { olds = news; in_file >> news; } if(in_file.eof()) break; /* * if beginning of dataset */ if (news == _label_dataset_mesh_data) { /* * Now read the data of interest. * Start with the header. For * explanation of the variable * dataset_location, see below. */ unsigned int dataset_location; /* * the type of data (complex, real, * float, double etc, see below) */ unsigned int data_type; /* * the number of floating-point values per entity */ unsigned int NVALDC; /* * If there is no MeshDataUnvHeader object * attached */ if (_unv_header==NULL) { /* * Ignore the first lines that stand for * analysis dataset label and name. */ for(unsigned int i=0; i<3; i++) in_file.ignore(256,'\n'); /* * Read the dataset location, where * 1: Data at nodes * 2: Data on elements * other sets are currently not supported. */ in_file >> dataset_location; /* * Ignore five ID lines. */ for(unsigned int i=0; i<6; i++) in_file.ignore(256,'\n'); /* * These data are all of no interest to us... */ unsigned int model_type, analysis_type, data_characteristic, result_type; /* * Read record 9. */ in_file >> model_type // not used here >> analysis_type // not used here >> data_characteristic // not used here >> result_type // not used here >> data_type >> NVALDC; /* * Ignore record 10 and 11 * (Integer analysis type specific data). */ for (unsigned int i=0; i<3; i++) in_file.ignore(256,'\n'); /* * Ignore record 12 and record 13. Since there * exist UNV files with 'D' instead of 'e' as * 10th-power char, it is safer to use a string * to read the dummy reals. */ { std::string dummy_Real; for (unsigned int i=0; i<12; i++) in_file >> dummy_Real; } } else { /* * the read() method returns false when * the user wanted a special header, and * when the current header is _not_ the correct * header */ if (_unv_header->read(in_file)) { dataset_location = _unv_header->dataset_location; NVALDC = _unv_header->nvaldc; data_type = _unv_header->data_type; } else { /* * This is not the correct header. Go * and find the next. For this to * work correctly, shift to the * next line, so that the "-1" * disappears from olds */ olds = news; in_file >> news; /* * No good style, i know... */ goto go_and_find_the_next_dataset; } } /* * Check the location of the dataset. */ if (dataset_location != 1) { libMesh::err << "ERROR: Currently only Data at nodes is supported." << std::endl; libmesh_error(); } /* * Now get the foreign node id number and the respective nodal data. */ int f_n_id; std::vector<Number> values; while(true) { in_file >> f_n_id; /* * if node_nr = -1 then we have reached the end of the dataset. */ if (f_n_id==-1) break; /* * Resize the values vector (usually data in three * principle directions, i.e. NVALDC = 3). */ values.resize(NVALDC); /* * Read the meshdata for the respective node. */ for (unsigned int data_cnt=0; data_cnt<NVALDC; data_cnt++) { /* * Check what data type we are reading. * 2,4: Real * 5,6: Complex * other data types are not supported yet. * As again, these floats may also be written * using a 'D' instead of an 'e'. */ if (data_type == 2 || data_type == 4) { std::string buf; in_file >> buf; MeshDataUnvHeader::need_D_to_e(buf); #ifdef LIBMESH_USE_COMPLEX_NUMBERS values[data_cnt] = Complex(std::atof(buf.c_str()), 0.); #else values[data_cnt] = std::atof(buf.c_str()); #endif } else if(data_type == 5 || data_type == 6) { #ifdef LIBMESH_USE_COMPLEX_NUMBERS Real re_val, im_val; std::string buf; in_file >> buf; if (MeshDataUnvHeader::need_D_to_e(buf)) { re_val = std::atof(buf.c_str()); in_file >> buf; MeshDataUnvHeader::need_D_to_e(buf); im_val = std::atof(buf.c_str()); } else { re_val = std::atof(buf.c_str()); in_file >> im_val; } values[data_cnt] = Complex(re_val,im_val); #else libMesh::err << "ERROR: Complex data only supported" << std::endl << "when libMesh is configured with --enable-complex!" << std::endl; libmesh_error(); #endif }
Complex Complex::operator/(const Complex & a) const { return Complex(*this * a.cc()) / sqr(a.mod()); }
void Arnoldi<SCAL>::Calc (int numval, Array<Complex> & lam, int numev, Array<shared_ptr<BaseVector>> & hevecs, const BaseMatrix * pre) const { static Timer t("arnoldi"); static Timer t2("arnoldi - orthogonalize"); static Timer t3("arnoldi - compute large vectors"); RegionTimer reg(t); auto hv = a.CreateVector(); auto hv2 = a.CreateVector(); auto hva = a.CreateVector(); auto hvm = a.CreateVector(); int n = hv.FV<SCAL>().Size(); int m = min2 (numval, n); Matrix<SCAL> matH(m); Array<shared_ptr<BaseVector>> abv(m); for (int i = 0; i < m; i++) abv[i] = a.CreateVector(); auto mat_shift = a.CreateMatrix(); mat_shift->AsVector() = a.AsVector() - shift*b.AsVector(); shared_ptr<BaseMatrix> inv; if (!pre) inv = mat_shift->InverseMatrix (freedofs); else { auto itso = make_shared<GMRESSolver<double>> (*mat_shift, *pre); itso->SetPrintRates(1); itso->SetMaxSteps(2000); inv = itso; } hv.SetRandom(); hv.SetParallelStatus (CUMULATED); FlatVector<SCAL> fv = hv.FV<SCAL>(); if (freedofs) for (int i = 0; i < hv.Size(); i++) if (! (*freedofs)[i] ) fv(i) = 0; t2.Start(); // matV = SCAL(0.0); why ? matH = SCAL(0.0); *hv2 = *hv; SCAL len = sqrt (S_InnerProduct<SCAL> (*hv, *hv2)); // parallel *hv /= len; for (int i = 0; i < m; i++) { cout << IM(1) << "\ri = " << i << "/" << m << flush; /* for (int j = 0; j < n; j++) matV(i,j) = hv.FV<SCAL>()(j); */ *abv[i] = *hv; *hva = b * *hv; *hvm = *inv * *hva; for (int j = 0; j <= i; j++) { /* SCAL sum = 0.0; for (int k = 0; k < n; k++) sum += hvm.FV<SCAL>()(k) * matV(j,k); matH(j,i) = sum; for (int k = 0; k < n; k++) hvm.FV<SCAL>()(k) -= sum * matV(j,k); */ /* SCAL sum = 0.0; FlatVector<SCAL> abvj = abv[j] -> FV<SCAL>(); FlatVector<SCAL> fv_hvm = hvm.FV<SCAL>(); for (int k = 0; k < n; k++) sum += fv_hvm(k) * abvj(k); matH(j,i) = sum; for (int k = 0; k < n; k++) fv_hvm(k) -= sum * abvj(k); */ matH(j,i) = S_InnerProduct<SCAL> (*hvm, *abv[j]); *hvm -= matH(j,i) * *abv[j]; } *hv = *hvm; *hv2 = *hv; SCAL len = sqrt (S_InnerProduct<SCAL> (*hv, *hv2)); if (i<m-1) matH(i+1,i) = len; *hv /= len; } t2.Stop(); t2.AddFlops (double(n)*m*m); cout << "n = " << n << ", m = " << m << " n*m*m = " << n*m*m << endl; cout << IM(1) << "\ri = " << m << "/" << m << endl; Vector<Complex> lami(m); Matrix<Complex> evecs(m); Matrix<Complex> matHt(m); matHt = Trans (matH); evecs = Complex (0.0); lami = Complex (0.0); cout << "Solve Hessenberg evp with Lapack ... " << flush; LapackHessenbergEP (matH.Height(), &matHt(0,0), &lami(0), &evecs(0,0)); cout << "done" << endl; for (int i = 0; i < m; i++) lami(i) = 1.0 / lami(i) + shift; lam.SetSize (m); for (int i = 0; i < m; i++) lam[i] = lami(i); t3.Start(); if (numev>0) { int nout = min2 (numev, m); hevecs.SetSize(nout); for (int i = 0; i< nout; i++) { hevecs[i] = a.CreateVector(); *hevecs[i] = 0; for (int j = 0; j < m; j++) *hevecs[i] += evecs(i,j) * *abv[j]; // hevecs[i]->FVComplex() = Trans(matV)*evecs.Row(i); } } t3.Stop(); }
Complex operator-(double b, const Complex & a) { return Complex(b - a.real(), -a.imag()); }
Complex operator+(double d, const Complex & c1) { return Complex(c1.getReal() + d, c1.getImagionary()); }