-
-
Notifications
You must be signed in to change notification settings - Fork 360
Added C++ lang implementation of the split operator #420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added C++ lang implementation of the split operator #420
Conversation
…rithm-archive into CPP_integration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments:
- The OOP nature of this makes it difficult to read. I would argue that most people implementing this algorithm will be coming from a Scientific computing background, meaning that this style of programming might not comfortable for them. I am up for debate here, though.
- The energy calculation can also be put into the quantum systems chapter. Would you be willing to put your energy calculation code there as well?
C++ is a sticky point for the Algorithm Archive in that a lot of people tend to want to write different styles of C++ code. We tend to try to take the middle ground with this project and avoid overly OOP and Functional programming and instead use what makes sense at the time for readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for not using classes when you don't have any methods
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of big things:
- Please, please use
std::
. I know it's not required on most compilers, but either it orusing
is by the standard, andusing
is a bad habit to give peope. - If you're going to use C-style string formatting and IO, do it right, especially in teaching code.
@nic-hartley Where do I implicitly use |
@rajdakin Basically everywhere you were using a C function, before the changes. The standard mandates that they be put in Or, in the standard's words: S20.5.1.1.2-3
S20.1.5.2.4
(formatting omitted for ease of copy/pasting) latest draft, p. 456-457 Just to be explicitly clear, though: You are not implicitly using it anymore. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me now!
I am getting compilation errors:
|
diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp
index 814ef2a..525d66f 100644
--- a/contents/split-operator_method/code/c++/split_op.cpp
+++ b/contents/split-operator_method/code/c++/split_op.cpp
@@ -58,8 +58,8 @@ public:
ke.emplace_back(exp(-0.5 * par.dt * pow(par.k[i], 2)));
pe.emplace_back(exp(-0.5 * par.dt * v[i]));
} else {
- ke.emplace_back(exp(-0.5 * par.dt * pow(par.k[i], 2) * std::complex(0.0, 1.0)));
- pe.emplace_back(exp(-0.5 * par.dt * v[i] * std::complex(0.0, 1.0)));
+ ke.emplace_back(exp(-0.5 * par.dt * pow(par.k[i], 2) * std::complex<double>(0.0, 1.0)));
+ pe.emplace_back(exp(-0.5 * par.dt * v[i] * std::complex<double>(0.0, 1.0)));
}
}
}
@@ -147,8 +147,8 @@ void split_op(Params &par, Operators &opr) {
}
double calculate_energy(Params par, Operators opr) {
- std::vector<std::complex<double>> wfc_r = std::vector(opr.wfc);
- std::vector<std::complex<double>> wfc_k = std::vector(opr.wfc);
+ std::vector<std::complex<double>> wfc_r(opr.wfc);
+ std::vector<std::complex<double>> wfc_k(opr.wfc);
std::vector<std::complex<double>> wfc_c;
fft(wfc_k, opr.size, false);
@@ -160,7 +160,7 @@ double calculate_energy(Params par, Operators opr) {
std::vector<std::complex<double>> energy_r;
for (size_t i = 0; i < opr.size; ++i) {
- energy_k[i] = wfc_k[i] * pow(std::complex(par.k[i], 0.0), 2);
+ energy_k[i] = wfc_k[i] * pow(std::complex<double>(par.k[i], 0.0), 2);
}
fft(energy_k, opr.size, true); There is also a segfault that probably originates from accessing |
The problem was not due to the fact that I accessed |
It looks like this problem has been fixed? I just compiled and it worked just fine. |
Would it be possible to put the energy calculation in the quantum systems chapter as well? It's a small modification to what you currently have. |
Uh, no. |
@berquist I fixed the problems, it works now. |
@rajdakin You can also assign and read outside the bounds of a normal array without crashing; that's how Heartbleed worked. It's still UB. All |
Hey, what is the status on this PR? Once this is merged, we can attack #476 . I would like to point out that the code seems to output that the energy is 0 right now, which shouldn't be correct. I can look into this more, so let me know if you want me to do this. As a note: I am not sure what the best solution to the Undefined Behaviour (UB) is, but I can also take a swing at that, if desired. |
There's also an incredible amount of whitespace in the file that needs to be removed. |
@berquist I also removed all the unused whitespaces. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last thing.
It looks like this review is mostly done. After this, I'll double check that the energy calculation is consistent in #476 and attack that next. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the code looks good. One last book thing.
Hey @berquist sorry to bug you, but the changes also seem to be made. Is this one good to go? |
This pull request is one of the two new pull requests requested in #409 and is made to integrate the C++ lang with the split operator method.