Opt.cpp File Reference
#include <Mercury3D.h>
#include <Species/Species.h>
#include <Species/LinearViscoelasticSpecies.h>
#include <CMakeDefinitions.h>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
#include "Walls/InfiniteWall.h"
#include <cmath>

Classes

class  SimpleOpt
 

Functions

std::string ExecCommand (const char *cmd)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ ExecCommand()

std::string ExecCommand ( const char cmd)
67  {
68  std::array<char, 128> buffer;
69  std::string result;
70  std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
71  if (!pipe) {
72  std::string ret = "Could not run an extra OS command: "; ret.append(cmd);
73  throw std::runtime_error(ret);
74  }
75  while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
76  result += buffer.data();
77  }
78  return result;
79 }

Referenced by main().

◆ main()

int main ( int argc  ,
char argv[] 
)
82 {
83  // In this simple example we optimize two angular parameters (theta and phi in a standard spherical coord system)
84  // to extremize the distance the projectile travels in a given direction
85 
86  int NumParams = 2;
87 
88  // Get parameters passed through the command line
89  std::vector <Mdouble> params(0);
90  for (int i = 0; i<NumParams; i++) {
91  std::string a;
92  Mdouble param = stod(helpers::readFromCommandLine(argc, argv, "-p"+std::to_string(i), a));
93  params.push_back(param);
94  std::cout<<params[i]<<std::endl;
95  }
96 
97  // Problem setup
98  SimpleOpt problem;
99 
100  // Species
102  species.setDensity(2500.0);
103  species.setStiffness(258.5);
104  species.setDissipation(0.0);
105  problem.speciesHandler.copyAndAddObject(species);
106 
107  // Tabletop
108  problem.wallHandler.clear();
109  InfiniteWall w0;
110  w0.setSpecies(problem.speciesHandler.getObject(0));
111  w0.set(Vec3D(0.0, 0.0, -1.0), Vec3D(0, 0, problem.getZMin()));
112  problem.wallHandler.copyAndAddObject(w0);
113 
114  // Projectile
116  p0.setSpecies(problem.speciesHandler.getObject(0));
117  p0.setRadius(0.05); // sets particle radius
118  p0.setPosition(Vec3D(0,0,0));
119  p0.setVelocity(Vec3D(0.1 * problem.getXMax(), 0.1 * problem.getYMax(), 0.1 * problem.getZMax())); // sets particle position
120 
121  Mdouble VelMag = 4;
122  Mdouble theta = params[0]; // Controls the inclination angle of a cannon
123  Mdouble phi = params[1]; // Controls azimutal orientation of a cannon
124 
125  // Set the initial velocity of the projectile
126  p0.setVelocity(Vec3D(VelMag * sin(theta) * cos(phi), VelMag * sin(theta) * sin(phi), VelMag * cos(theta)));
127  problem.particleHandler.copyAndAddObject(p0);
128 
129  // Time integration and log parameters
130  problem.setSaveCount(100);
135  logger(INFO, "run number: %", problem.dataFile.getCounter());
136  problem.setXBallsAdditionalArguments("-solidf -v0");
137  problem.setTimeStep(0.005 / 50.0); // (collision time)/50.0
138  problem.solve();
139 
140  // Paraview data
141  std::cout<<ExecCommand("rm -rf paraview_SimpleOpt/")<<std::endl;
142  std::cout<<ExecCommand("mkdir paraview_SimpleOpt/")<<std::endl;
143  std::cout<<ExecCommand("../../Tools/data2pvd SimpleOpt.data paraview_SimpleOpt/SimpleOpt")<<std::endl;
144 
145  // Return the functional via the text file
146  std::ofstream funct; funct.open ("functional.txt");
147  Vec3D pos = problem.particleHandler.getLastObject()->getPosition();
148  Mdouble fun = 1 / (pos.X * pos.X); // Minimization of this optimizes for the longest projectile travel along X direction
149  funct << fun;
150  funct.close();
151 
152  return 0;
153 }
@ NO_FILE
file will not be created/read
@ ONE_FILE
all data will be written into/ read from a single file called name_
double Mdouble
Definition: GeneralDefine.h:34
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
std::string ExecCommand(const char *cmd)
Definition: Opt.cpp:67
virtual void clear()
Empties the whole BaseHandler by removing all Objects and setting all other variables to 0.
Definition: BaseHandler.h:528
std::enable_if<!std::is_pointer< U >::value, U * >::type copyAndAddObject(const U &object)
Creates a copy of a Object and adds it to the BaseHandler.
Definition: BaseHandler.h:379
T * getObject(const unsigned int id)
Gets a pointer to the Object at the specified index in the BaseHandler.
Definition: BaseHandler.h:613
T * getLastObject()
Gets a pointer to the last Object in this BaseHandler.
Definition: BaseHandler.h:634
void setVelocity(const Vec3D &velocity)
set the velocity of the BaseInteractable.
Definition: BaseInteractable.cc:350
virtual void setPosition(const Vec3D &position)
Sets the position of this BaseInteractable.
Definition: BaseInteractable.h:239
const Vec3D & getPosition() const
Returns the position of this BaseInteractable.
Definition: BaseInteractable.h:218
virtual void setRadius(Mdouble radius)
Sets the particle's radius_ (and adjusts the mass_ accordingly, based on the particle's species)
Definition: BaseParticle.cc:553
void setSpecies(const ParticleSpecies *species)
Definition: BaseParticle.cc:818
void setSpecies(const ParticleSpecies *species)
Defines the species of the current wall.
Definition: BaseWall.cc:169
Mdouble getXMax() const
If the length of the problem domain in x-direction is XMax - XMin, then getXMax() returns XMax.
Definition: DPMBase.h:626
void setSaveCount(unsigned int saveCount)
Sets File::saveCount_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:408
File eneFile
An instance of class File to handle in- and output into a .ene file.
Definition: DPMBase.h:1488
SpeciesHandler speciesHandler
A handler to that stores the species type i.e. LinearViscoelasticSpecies, etc.
Definition: DPMBase.h:1427
File fStatFile
An instance of class File to handle in- and output into a .fstat file.
Definition: DPMBase.h:1483
File dataFile
An instance of class File to handle in- and output into a .data file.
Definition: DPMBase.h:1478
WallHandler wallHandler
An object of the class WallHandler. Contains pointers to all the walls created.
Definition: DPMBase.h:1447
File restartFile
An instance of class File to handle in- and output into a .restart file.
Definition: DPMBase.h:1493
void setXBallsAdditionalArguments(std::string newXBArgs)
Set the additional arguments for xballs.
Definition: DPMBase.cc:1347
ParticleHandler particleHandler
An object of the class ParticleHandler, contains the pointers to all the particles created.
Definition: DPMBase.h:1437
Mdouble getYMax() const
If the length of the problem domain in y-direction is YMax - YMin, then getYMax() returns XMax.
Definition: DPMBase.h:638
void setTimeStep(Mdouble newDt)
Sets a new value for the simulation time step.
Definition: DPMBase.cc:1234
void solve()
The work horse of the code.
Definition: DPMBase.cc:4270
Mdouble getZMax() const
If the length of the problem domain in z-direction is ZMax - ZMin, then getZMax() returns ZMax.
Definition: DPMBase.h:650
Mdouble getZMin() const
If the length of the problem domain in z-direction is ZMax - ZMin, then getZMin() returns ZMin.
Definition: DPMBase.h:644
void setFileType(FileType fileType)
Sets the type of file needed to write into or read from. File::fileType_.
Definition: File.cc:215
unsigned int getCounter() const
In case of multiple files, File::getCounter() returns the the number (FILE::Counter_) of the next fil...
Definition: File.cc:223
A infinite wall fills the half-space {point: (position_-point)*normal_<=0}.
Definition: InfiniteWall.h:48
void set(Vec3D normal, Vec3D point)
Defines a standard wall, given an outward normal vector s.t. normal*x=normal*point for all x of the w...
Definition: InfiniteWall.cc:118
void setDissipation(Mdouble dissipation)
Allows the normal dissipation to be changed.
Definition: LinearViscoelasticNormalSpecies.cc:117
void setStiffness(Mdouble new_k)
Allows the spring constant to be changed.
Definition: LinearViscoelasticNormalSpecies.cc:93
void setDensity(Mdouble density)
Definition: ParticleSpecies.cc:108
Definition: Opt.cpp:44
A spherical particle is the most simple particle used in MercuryDPM.
Definition: SphericalParticle.h:37
Definition: Vector.h:51
Mdouble X
the vector components
Definition: Vector.h:66
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51
bool readFromCommandLine(int argc, char *argv[], std::string varName)
Returns true if command line arguments contain varName, false else.
Definition: CommandLineHelpers.cc:103
Mdouble cos(Mdouble x)
Definition: ExtendedMath.cc:64
Mdouble sin(Mdouble x)
Definition: ExtendedMath.cc:44

References BaseHandler< T >::clear(), BaseHandler< T >::copyAndAddObject(), mathsFunc::cos(), DPMBase::dataFile, DPMBase::eneFile, ExecCommand(), DPMBase::fStatFile, File::getCounter(), BaseHandler< T >::getLastObject(), BaseHandler< T >::getObject(), BaseInteractable::getPosition(), DPMBase::getXMax(), DPMBase::getYMax(), DPMBase::getZMax(), DPMBase::getZMin(), constants::i, INFO, logger, NO_FILE, ONE_FILE, DPMBase::particleHandler, helpers::readFromCommandLine(), DPMBase::restartFile, InfiniteWall::set(), ParticleSpecies::setDensity(), LinearViscoelasticNormalSpecies::setDissipation(), File::setFileType(), BaseInteractable::setPosition(), BaseParticle::setRadius(), DPMBase::setSaveCount(), BaseParticle::setSpecies(), BaseWall::setSpecies(), LinearViscoelasticNormalSpecies::setStiffness(), DPMBase::setTimeStep(), BaseInteractable::setVelocity(), DPMBase::setXBallsAdditionalArguments(), mathsFunc::sin(), DPMBase::solve(), DPMBase::speciesHandler, DPMBase::wallHandler, and Vec3D::X.