Skip to content

Commit

Permalink
Made phase 1 a bit easier to understand
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Greenburg committed Mar 21, 2024
1 parent d181362 commit 5a22ff0
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions project/phase1.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ You can test your implementation in the same way. You'll probably want to [set u

## C++

If you follow the [example code](https://github.com/BYUHPC/sci-comp-course-example-cxx) to create a `WaveOrthotope` class with `solve` and `sim_time` functions and a constructor taking rows, columns, and damping coefficient, your `main` can be very simple:
If you follow the [example code](https://github.com/BYUHPC/sci-comp-course-example-cxx/blob/main/src/MountainRange.hpp) to create a `WaveOrthotope` class with `solve` and `sim_time` functions and a constructor taking rows, columns, and damping coefficient, your `main` can be very simple:

```c++
#include <iostream>
Expand All @@ -63,25 +63,35 @@ int main() {
}
```

The most challenging part of the assignment for most students is using [2-dimensional arrays of runtime size in C++](https://stackoverflow.com/a/32279494). I recommend using a one-dimensional `std::vector` to store your data, then using a function to pretend it's two-dimensional:
The most challenging part of the assignment for most students is using [2-dimensional arrays of runtime size in C++](https://stackoverflow.com/a/32279494). I recommend using a one-dimensional `std::vector` to store your data, then using a function to pretend it's two-dimensional. Here's a simple interface class with everything you should need for this phase, including `displacement` and `velocity` functions for 2-dimensional array access:

```c++
class WaveOrthotope {
const size_t rows, cols;
std::vector<double> u, v;
protected:
const size_t rows, cols; // size
const double c; // damping coefficient
double t; // simulation time
std::vector<double> u, v; // displacement and velocity; each are of size rows*cols

// lots of other stuff
public:
WaveOrthotope(double damping_coef, decltype(u) disp, decltype(v) vel);

auto &displacement(auto i, auto j) { return u[i*cols+j]; }
auto &velocity( auto i, auto j) { return v[i*cols+j]; }

virtual double energy(); // optional, to be used in solve

virtual double step(double dt); // optional, to be used in solve

double solve();
}

// Usage:
// Example velocity function usage:
auto wo = WaveOrthotope(/*...*/);
wo.velocity(1, 2) = 1.5; // set v[1, 2] to 1.5
```
No matter what you do, your life will be much easier in subsequent assignments if the data is contiguous.
No matter what you do, your life will be much easier in subsequent assignments if the data in `u` and `v` is contiguous.
Expand Down

0 comments on commit 5a22ff0

Please sign in to comment.