Skip to content
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

Green's function for da solver #207

Merged
merged 15 commits into from Dec 21, 2021

Conversation

vathomass
Copy link
Contributor

Hi,

This is the 1st draft implementation for the semi-analytic solution for the DA solver, as discussed in #205.
For my testing, I am using the MJ_solar_cell_using_DA.py from the examples directory, where one can switch between the original and the new implementation by passing the option 'da_mode': 'bvp' or 'da_mode': 'green' along with the other options to the solar cell solver.

For now, I have not implemented any tests, neither have I updated the documentation.

I welcome any feedback you may have.

Best,
Thomas

Copy link
Collaborator

@dalonsoa dalonsoa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left a couple of comments, but it looks really, really good. If it works as epxected - and your example seems to indeicate so - this is much faster than the existing method.

Is there any limit where this method is not applicable?

@@ -383,6 +395,52 @@ def bc(ya, yb):
return out


def get_J_sc_diffusion_green(xa, xb, g, D, L, y0, S, ph, side='top'):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that solcore is not that great with docstrings - to put it mildly - but it will be great if any new addition were thorough with that and type annotations, to make the code more readable. Nothing to do in this first pass, but something to tkae into account.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring has been added, similar to the other functions. Do you think the newly introduced top level functions need a docstring (introduced because of your third comment)? I am not sure what you mean with type annotations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, yes, so the functions are really standalone. About type annotations, you have a very nice blog post describing the matter here: https://towardsdatascience.com/type-annotations-in-python-d90990b172dc

cadd = -2. * S / D * y0 / (1. - crvel) * np.exp(-xbL)
cp = 1.

def fun(x):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I've used them, I am not a big fan of internal functions like this one (and the one below) because they cannot be tested. I would suggest you define them as top level functions, passing all the relevant arguments to them.

Again, the point being improving the overall quality of solcore's code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Quad_vec does not support the "arg" argument like the other functions of the scipy.integate package (although the feature has been requested and will be implemented in the future). My suggestion is using the partial method from the functiontool package.

@codecov
Copy link

codecov bot commented Nov 27, 2021

Codecov Report

Merging #207 (9fc060e) into develop (2ca5ffb) will increase coverage by 0.37%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #207      +/-   ##
===========================================
+ Coverage    45.47%   45.84%   +0.37%     
===========================================
  Files           84       84              
  Lines         9029     9091      +62     
===========================================
+ Hits          4106     4168      +62     
  Misses        4923     4923              
Impacted Files Coverage Δ
solcore/science_tracker.py 26.53% <ø> (ø)
solcore/structure.py 91.75% <ø> (ø)
solcore/analytic_solar_cells/__init__.py 100.00% <100.00%> (ø)
...re/analytic_solar_cells/depletion_approximation.py 100.00% <100.00%> (ø)
solcore/solar_cell_solver.py 66.46% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2ca5ffb...9fc060e. Read the comment docs.

@vathomass
Copy link
Contributor Author

vathomass commented Dec 2, 2021

Hi Diego,

Thanks very much for taking the time to review the pull request and for your comments. I agree with all of the comments, see above.

Regarding the limitation of this method. From the theoretical point, it of course inherits the limitations of the depletion approximation. Numerically, the one that I have encountered is that the solution behaves poorly as the junction width becomes way larger the diffusion length (e.g. for junction with a lot of defects). In my code, I am guarding against this by assuming that if x>1.e2 then sinh(x) = cosh(x) = .5*e^x (for numpy this is actually the case). Keep in mind that this problem also affects the solve_bvp method, as the differential equation becomes stiffer and the accuracy drops, and the get_j_dark calculation (e.g. setting electron_diffusion_length=si("0.05um") for the third junction in the MJ_solar_cell_using_DA.py script results in nan's for the iv curve).

Best,
Thomas

Copy link
Collaborator

@dalonsoa dalonsoa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using partial is a great way of dealing with that issue. Well done! Keeping aside - optionally - completing the docstrings for the extra top level functions and adding type annotations, I think this is pretty much ready to:

  • write tests
  • update documentation

Great work! I believe this first addition of functionality to Solcore in a long, long time! Let's hope more people follow your lead!

@@ -383,6 +395,52 @@ def bc(ya, yb):
return out


def get_J_sc_diffusion_green(xa, xb, g, D, L, y0, S, ph, side='top'):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, yes, so the functions are really standalone. About type annotations, you have a very nice blog post describing the matter here: https://towardsdatascience.com/type-annotations-in-python-d90990b172dc

@vathomass
Copy link
Contributor Author

Hi Diego

Docstrings, tests and documentation has been updated. My system passes all the new tests and I get a 100% coverage for the added code.

Regarding the type annotations, I am facing some problems though. The examples from the article you sent me do not produce any warning when there is a type mismatch in the function arguments. The article hints that type evaluations have been postponed. This makes the implementation vague (how am I supposed to test), so I skipped them.

Best,
Thomas

@vathomass vathomass changed the title Green's function for da solver - 1st draft Green's function for da solver Dec 17, 2021
@dalonsoa
Copy link
Collaborator

Many thanks for implenting this. It looks great! I'm going to merge it as it is now as it ticks all the boxes.

To me, type annotations are more like documentation, to guide the user and the developer towards consistent, correct code. Most IDEs like PyCharm and VSCode can be configured to check the consistenncy of the types in the background - producing a warning if there is something wrong - or you can use an static type checker like mypy with the same purpose. But, in the end, is about documenting the types of the arguments to functions and their outputs, not a feature of the language that changes what it does.

@dalonsoa dalonsoa merged commit c963eb5 into qpv-research-group:develop Dec 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants