Simulating the Schrödinger wave equation using the Crank-Nicolson method in 2+1 dimensions
Simulating the Schrödinger wave equation using the Crank-Nicolson method in 2+1 dimensions
Loading...
Searching...
No Matches
wave_simulation.cpp
Go to the documentation of this file.
1
12#include "WaveSimulation.hpp"
13#include "utils.hpp"
14
15#include <fstream>
16#include <string>
17
18void execute_simulation(std::vector<std::string> &args)
19{
20 // Check that args has at least 11 elements
21 if (args.size() < 11) {
22 return;
23 }
24
25 WaveSimulation *sim;
26
27 // This is a simulation without a barrier
28 if (args.size() >= 11 && args.size() < 16) {
29 sim = new WaveSimulation(
30 std::stod(args[2]), std::stod(args[3]), std::stod(args[4]),
31 std::stod(args[5]), std::stod(args[6]), std::stod(args[7]),
32 std::stod(args[8]), std::stod(args[9]), std::stod(args[10]));
33 }
34 // This is a simulation with a barrier
35 else if (args.size() >= 16) {
36 sim = new WaveSimulation(
37 std::stod(args[2]), std::stod(args[3]), std::stod(args[4]),
38 std::stod(args[5]), std::stod(args[6]), std::stod(args[7]),
39 std::stod(args[8]), std::stod(args[9]), std::stod(args[10]),
40 std::stod(args[11]), std::stod(args[12]), std::stod(args[13]),
41 std::stod(args[14]), std::stoi(args[15]));
42 }
43
44 // Execute a probability deviation simulation
45 if (args[0].compare("deviation") == 0) {
46 if (args.size() == 17) {
47 sim->probability_deviation(args[1], args[16].compare("false"));
48 }
49 else if (args.size() == 12) {
50 sim->probability_deviation(args[1], args[11].compare("false"));
51 }
52 else {
53 sim->probability_deviation(args[1]);
54 }
55 }
56 // Execute wave packet simulation
57 else if (args[0].compare("simulate") == 0) {
58 if (args.size() == 17) {
59 sim->simulate(args[1], args[16].compare("false"));
60 }
61 else if (args.size() == 12) {
62 sim->simulate(args[1], args[11].compare("false"));
63 }
64 else if (args.size() > 17) {
65 std::vector<double> arr;
66 for (size_t i = 16; i < args.size(); i++) {
67 arr.push_back(std::stod(args[i]));
68 }
69 sim->simulate(args[1], arr);
70 }
71 else {
72 sim->simulate(args[1]);
73 }
74 }
75 delete sim;
76}
77
78int main(int argc, char **argv)
79{
80 // Check that an argument has been given
81 if (argc < 2) {
82 abort();
83 }
84
85 std::string filename = argv[1];
86
87 std::ifstream infile;
88 infile.open(filename);
89
90 for (std::string line; std::getline(infile, line);) {
91 std::vector<std::string> arr = utils::split(line, ';');
92 execute_simulation(arr);
93 }
94
95 infile.close();
96}
The definition of the WaveSimulation class.
Simulate the evolution of a wave packet in 2 + 1 domensions.
void simulate(std::string outfile, bool write_each_step=false)
Evolve the wave packet until the time T has been reached and write U to file.
void probability_deviation(std::string outfile, bool write_each_step=false)
Write the deviation of the sum of the probability of U from 1.
Function prototypes and macros that are useful.