Page MenuHomec4science

perf-test.cc
No OneTemporary

File Metadata

Created
Thu, Apr 25, 18:20

perf-test.cc

/**
* @file scalability_test.cc
* @author Nicolas Richart <nicolas.richart@epfl.ch>
* @date Tue Feb 22 09:35:58 2011
*
* @brief Test de scalability
*
* @section LICENSE
*
* Copyright (©) 2010-2011 EPFL (Ecole Polytechnique Fédérale de Lausanne)
* Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
*
* Akantu is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* Akantu is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Akantu. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* -------------------------------------------------------------------------- */
#include <chrono>
#include <fstream>
#include <limits>
/* -------------------------------------------------------------------------- */
#include "aka_common.hh"
#define _AKANTU_VERISON \
((10000 * AKANTU_VERSION_MAJOR) + (100 * AKANTU_VERSION_MINOR))
#if (((AKANTU_VERSION_MAJOR >= 2) && (AKANTU_VERSION_MINOR >= 1)) || \
(AKANTU_VERSION_MAJOR >= 3))
# include "aka_array.hh"
#else
# include "aka_vector.hh"
#endif
//#define DUMPS_energy
//#define DUMPS_paraview
#include "solid_mechanics_model.hh"
#if _AKANTU_VERISON < 20301
# include "mesh.hh"
# include "mesh_io.hh"
# include "mesh_partition_scotch.hh"
#endif
#if _AKANTU_VERISON < 30000
# include "static_communicator.hh"
#else
# include "communicator.hh"
#endif
using namespace akantu;
typedef std::chrono::high_resolution_clock clk;
using std::chrono::duration_cast;
using std::chrono::microseconds;
//#define AKANTU_VERSION_MAJOR 2
using clk = std::chrono::high_resolution_clock;
using seconds = std::chrono::duration<double>;
using milliseconds = std::chrono::duration<double, std::milli>;
//#define AKANTU_VERSION_MAJOR 2
class Chrono {
public:
Chrono(int prank, int psize) : prank(prank), psize(psize) {
}
inline void start() { _start = clk::now(); };
inline void store_time(const std::string &type) {
clk::time_point _end = clk::now();
if (measures.find(type) == measures.end()) {
measures[type] = _end - _start;
nb_measures[type] = 1;
} else {
measures[type] += _end - _start;
++nb_measures[type];
}
_start = clk::now();
}
virtual void printself(std::ostream &stream, int indent = 0) const {
std::string space(AKANTU_INDENT, indent);
stream << space << "Chrono [" << std::endl;
for (auto && measure : measures) {
const unsigned int &nb_measure = nb_measures.find(measure.first)->second;
stream << space << " + " << measure.first << "\t: " << std::setw(25)
<< std::fixed << std::setprecision(16)
<< measure.second.count()
<< "us - nb_repetition: " << nb_measure << std::endl;
}
stream << space << "]" << std::endl;
}
virtual void printself_csv(std::ostream &stream, int indent = 0) const {
std::string space(AKANTU_INDENT, indent);
bool first = true;
stream << "\"psize\"";
for (auto && measure : measures) {
stream << ", \""
<< measure.first
<< "\", \""
<< measure.first
<< "nb_rep"
<< "\"";
first = false;
}
stream << std::endl;
stream << psize;
first=true;
for (auto && measure : measures) {
const unsigned int &nb_measure = nb_measures.find(measure.first)->second;
stream << ", "
<< measure.second.count()
<< ", " << nb_measure;
first = false;
}
stream << std::endl;
}
private:
clk::time_point _start;
std::map<std::string, seconds> measures;
std::map<std::string, unsigned int> nb_measures;
int prank, psize;
};
inline std::ostream &operator<<(std::ostream &stream, const Chrono &_this) {
_this.printself_csv(stream);
return stream;
}
/* -------------------------------------------------------------------------- */
int main(int argc, char * argv[]) {
debug::setDebugLevel(dblWarning);
#if AKANTU_VERSION_MAJOR > 1
initialize("material.dat", argc, argv);
#else
initialize(&argc, &argv);
#endif
#if _AKANTU_VERISON < 30000 && _AKANTU_VERISON >= 20000
const auto & comm = StaticCommunicator::getStaticCommunicator();
#elif _AKANTU_VERISON < 20000
const auto & comm = *StaticCommunicator::getStaticCommunicator();
#else
const auto & comm = Communicator::getStaticCommunicator();
#endif
Int psize = comm.getNbProc();
Int prank = comm.whoAmI();
const auto &usersect = getUserParser();
const bool output_energy = usersect.getParameter("output_energy", false);
const bool output_paraview = usersect.getParameter("output_paraview", false);
//const UInt max_steps = usersect.getParameter("max_steps");
const std::string mesh_filename = "bar.msh"; //usersect.getParameter("mesh", "bar.msh");
/* --------------------------------------------------------------------------
*/
UInt dim = 2;
UInt max_steps = 1000;
Real time_factor = 0.8;
const Real pulse_width = .5;
const Real A = 0.01;
Real width = 1;
Real height = 0.2;
Chrono chrono(prank, psize);
chrono.start();
/* ------------------------------------------------------------------------ */
Mesh mesh(dim);
#if AKANTU_VERSION_MAJOR < 3
MeshPartition * partition = NULL;
#endif
if (prank == 0) {
#if _AKANTU_VERISON < 20201
MeshIOMSH mesh_io;
mesh_io.read(mesh_filename, mesh);
#else
mesh.read(mesh_filename);
#endif
chrono.store_time("read_mesh");
}
if (prank == 0) {
#if AKANTU_VERSION_MAJOR < 3 && defined(AKANTU_PARALLEL)
partition = new MeshPartitionScotch(mesh, dim);
partition->partitionate(psize);
#endif
}
#if AKANTU_VERSION_MAJOR >= 3
mesh.distribute();
chrono.store_time("distribute");
#endif
/// model initialization
SolidMechanicsModel model(mesh);
#if AKANTU_VERSION_MAJOR < 3
model.initParallel(partition);
chrono.store_time("distribute");
#endif
#if AKANTU_VERSION > 20000
model.initFull();
#else
model.initVectors();
auto nn = model.getFEM().getMesh().getNbNodes();
auto szr = sizeof(akantu::Real);
memset(model.getForce().values, 0, dim*nn*szr);
memset(model.getVelocity().values, 0, dim*nn*szr);
memset(model.getAcceleration().values, 0, dim*nn*szr);
memset(model.getDisplacement().values, 0, dim*nn*szr);
model.initModel();
model.readMaterials("material.dat");
model.initMaterials();
model.initExplicit();
model.initModel();
#endif
chrono.store_time("init_full");
#if AKANTU_VERSION_MAJOR < 3
model.assembleMassLumped();
chrono.store_time("lumped_mass");
#endif
/// boundary conditions
Real eps = 1e-16;
#if AKANTU_VERSION_MAJOR < 2
const Vector<Real> & pos = mesh.getNodes();
Vector<bool> & boundary = model.getBoundary();
Vector<Real> & disp = model.getDisplacement();
#else
const Array<Real> & pos = mesh.getNodes();
Array<bool> & boundary = model.getBlockedDOFs();
Array<Real> & disp = model.getDisplacement();
#endif
UInt nb_nodes = mesh.getNbNodes();
for (UInt n = 0; n < nb_nodes; ++n) {
Real x = pos(n, 0);
// Sinus * Gaussian
Real L = pulse_width;
Real k = 0.1 * 2 * M_PI * 3 / L;
disp(n, 0) = A * sin(k * x) * exp(-(k * x) * (k * x) / (L * L));
if(std::abs((std::abs(pos(n, 1)) - height/ 2.) <= eps ))
boundary(n, 1) = true;
if (std::abs(pos(n, 0) + width / 2.) <= eps)
boundary(n, 0) = true;
// if((std::abs(pos(i, 1) - height/2.) <= eps) || (std::abs(pos(i, 1) +
// height/2.) <= eps)) boundary(i, 1) = true;
}
chrono.store_time("bound_cond");
if(output_paraview) {
model.addDumpField("displacement");
model.addDumpField("velocity");
model.addDumpField("acceleration");
#if AKANTU_VERSION_MAJOR >= 3
model.addDumpField("internal_force");
#else
model.addDumpField("residual");
#endif
model.addDumpField("blocked_dofs");
chrono.store_time("init_dumper");
}
std::ofstream out;
if(output_energy) {
out.open("energies.csv");
out << "kinetic,potential" << std::endl;
chrono.store_time("init_energy");
}
Real time_step = model.getStableTimeStep() * time_factor;
model.setTimeStep(time_step);
//std::cout << "Time step: " << time_step << std::endl;
chrono.store_time("time_step");
Real epot, ekin;
for (UInt s = 1; s <= max_steps; ++s) {
chrono.start();
#if (AKANTU_VERSION_MAJOR >= 3)
model.solveStep();
// NonLinearSolver & nls =
// model.getDOFManager().getNonLinearSolver("implicit"); UInt n_iter =
// nls.get("nb_iterations"); Real error = nls.get("error"); std::cout <<
// n_iter << " -> " << error << std::endl;
#else
model.explicitPred();
model.updateResidual();
model.updateAcceleration();
model.explicitCorr();
#endif
chrono.store_time("solve_step");
if (output_energy) {
#if (((AKANTU_VERSION_MAJOR >= 2) && (AKANTU_VERSION_MINOR >= 2)) || \
(AKANTU_VERSION_MAJOR >= 3))
epot = model.getEnergy("potential");
ekin = model.getEnergy("kinetic");
#else
epot = model.getPotentialEnergy();
ekin = model.getKineticEnergy();
#endif
out << ekin << "," << epot << std::endl;
chrono.store_time("energies");
}
if (output_paraview) {
model.dump();
chrono.store_time("paraview");
}
}
if (prank == 0) {
std::cout << chrono;
}
finalize();
return EXIT_SUCCESS;
}

Event Timeline