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)
46  {
47  std::array<char, 128> buffer;
48  std::string result;
49  std::unique_ptr<FILE, int(*)(FILE*)> pipe(popen(cmd, "r"), pclose);
50  if (!pipe) {
51  std::string ret = "Could not run an extra OS command: "; ret.append(cmd);
52  throw std::runtime_error(ret);
53  }
54  while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
55  result += buffer.data();
56  }
57  return result;
58 }
return int(ret)+1
Eigen::DenseIndex ret
Definition: level1_cplx_impl.h:43
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286

References int(), ret, and oomph::Global_string_for_annotation::string().

Referenced by main().

◆ main()

int main ( int argc  ,
char argv[] 
)
61 {
62  // In this simple example we optimize two angular parameters (theta and phi in a standard spherical coord system)
63  // to extremize the distance the projectile travels in a given direction
64 
65  int NumParams = 2;
66 
67  // Get parameters passed through the command line
68  std::vector <Mdouble> params(0);
69  for (int i = 0; i<NumParams; i++) {
70  std::string a;
71  Mdouble param = stod(helpers::readFromCommandLine(argc, argv, "-p"+std::to_string(i), a));
72  params.push_back(param);
73  std::cout<<params[i]<<std::endl;
74  }
75 
76  // Problem setup
78 
79  // Species
81  species.setDensity(2500.0);
82  species.setStiffness(258.5);
83  species.setDissipation(0.0);
84  problem.speciesHandler.copyAndAddObject(species);
85 
86  // Tabletop
87  problem.wallHandler.clear();
88  InfiniteWall w0;
89  w0.setSpecies(problem.speciesHandler.getObject(0));
90  w0.set(Vec3D(0.0, 0.0, -1.0), Vec3D(0, 0, problem.getZMin()));
91  problem.wallHandler.copyAndAddObject(w0);
92 
93  // Projectile
95  p0.setSpecies(problem.speciesHandler.getObject(0));
96  p0.setRadius(0.05); // sets particle radius
97  p0.setPosition(Vec3D(0,0,0));
98  p0.setVelocity(Vec3D(0.1 * problem.getXMax(), 0.1 * problem.getYMax(), 0.1 * problem.getZMax())); // sets particle position
99 
100  Mdouble VelMag = 4;
101  Mdouble theta = params[0]; // Controls the inclination angle of a cannon
102  Mdouble phi = params[1]; // Controls azimutal orientation of a cannon
103 
104  // Set the initial velocity of the projectile
105  p0.setVelocity(Vec3D(VelMag * sin(theta) * cos(phi), VelMag * sin(theta) * sin(phi), VelMag * cos(theta)));
106  problem.particleHandler.copyAndAddObject(p0);
107 
108  // Time integration and log parameters
109  problem.setSaveCount(100);
110  problem.dataFile.setFileType(FileType::ONE_FILE);
111  problem.restartFile.setFileType(FileType::ONE_FILE);
112  problem.fStatFile.setFileType(FileType::NO_FILE);
113  problem.eneFile.setFileType(FileType::NO_FILE);
114  logger(INFO, "run number: %", problem.dataFile.getCounter());
115  problem.setXBallsAdditionalArguments("-solidf -v0");
116  problem.setTimeStep(0.005 / 50.0); // (collision time)/50.0
117  problem.solve();
118 
119  // Paraview data
120  std::cout<<ExecCommand("rm -rf paraview_SimpleOpt/")<<std::endl;
121  std::cout<<ExecCommand("mkdir paraview_SimpleOpt/")<<std::endl;
122  std::cout<<ExecCommand("../../Tools/data2pvd SimpleOpt.data paraview_SimpleOpt/SimpleOpt")<<std::endl;
123 
124  // Return the functional via the text file
125  std::ofstream funct; funct.open ("functional.txt");
126  Vec3D pos = problem.particleHandler.getLastObject()->getPosition();
127  Mdouble fun = 1 / (pos.X * pos.X); // Minimization of this optimizes for the longest projectile travel along X direction
128  funct << fun;
129  funct.close();
130 
131  return 0;
132 }
AnnoyingScalar cos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:136
AnnoyingScalar sin(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:137
int i
Definition: BiCGSTAB_step_by_step.cpp:9
@ NO_FILE
file will not be created/read
@ ONE_FILE
all data will be written into/ read from a single file called name_
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
Vector3f p0
Definition: MatrixBase_all.cpp:2
std::string ExecCommand(const char *cmd)
Definition: Opt.cpp:46
void setSpecies(const ParticleSpecies *species)
Defines the species of the current wall.
Definition: BaseWall.cc:148
A infinite wall fills the half-space {point: (position_-point)*normal_<=0}.
Definition: InfiniteWall.h:27
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:97
void setDissipation(Mdouble dissipation)
Allows the normal dissipation to be changed.
Definition: LinearViscoelasticNormalSpecies.cc:96
void setStiffness(Mdouble new_k)
Allows the spring constant to be changed.
Definition: LinearViscoelasticNormalSpecies.cc:72
void setDensity(Mdouble density)
Definition: ParticleSpecies.cc:88
A spherical particle is the most simple particle used in MercuryDPM.
Definition: SphericalParticle.h:16
Definition: Kernel/Math/Vector.h:30
Mdouble X
the vector components
Definition: Kernel/Math/Vector.h:45
const Scalar * a
Definition: level2_cplx_impl.h:32
#define INFO(i)
Definition: mumps_solver.h:54
double theta
Definition: two_d_biharmonic.cc:236
dictionary params
Definition: Particles2023AnalysisHung.py:35
Definition: SimpleOpt.py:1
bool readFromCommandLine(int argc, char *argv[], const std::string &varName)
Returns true if command line arguments contain varName, false else.
Definition: CommandLineHelpers.cc:99
std::string to_string(T object, unsigned float_precision=8)
Definition: oomph_utilities.h:189
Constructor for SteadyAxisymAdvectionDiffusion problem
Definition: steady_axisym_advection_diffusion.cc:213

References a, cos(), ExecCommand(), i, INFO, logger, NO_FILE, ONE_FILE, p0, Particles2023AnalysisHung::params, problem, helpers::readFromCommandLine(), InfiniteWall::set(), ParticleSpecies::setDensity(), LinearViscoelasticNormalSpecies::setDissipation(), BaseWall::setSpecies(), LinearViscoelasticNormalSpecies::setStiffness(), sin(), oomph::Global_string_for_annotation::string(), BiharmonicTestFunctions2::theta, oomph::StringConversion::to_string(), and Vec3D::X.