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

Fix bug with calculations on small timescales #682

Merged
merged 4 commits into from
Jan 18, 2024

Conversation

KulaginVladimir
Copy link
Collaborator

Proposed changes

When performing simulations on small timescales (e.g., final_time=1e-7), some bugs appear due to the use of np.isclose with the default parameters (atol, rtol). For example (see below), computation stops without reaching the final time. As @RemDelaporteMathurin suggested, the current PR attempts to fix the bug by setting atol=0 in np.isclose.

Types of changes

What types of changes does your code introduce to FESTIM?

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code refactoring
  • Documentation Update (if none of the other choices apply)
  • New tests

Checklist

  • Black formatted
  • Unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

MWE:

import festim as F
import numpy as np

my_model = F.Simulation(log_level = 40)

my_model.mesh = F.MeshFromVertices(np.linspace(0, 1, 10))

my_model.materials = F.Material(
    id=1,
    D_0=1,
    E_D=0.1,
)

my_model.T = F.Temperature(1000)

my_model.settings = F.Settings(
    absolute_tolerance=1e-10,
    relative_tolerance=1e-10,
    final_time=1e-7,
)

my_model.dt = F.Stepsize(
    initial_value=1e-10,
)

my_model.initialise()
my_model.run()

Stops at 90%:

Defining initial values
Defining variational problem
Defining source terms
Defining boundary conditions
Time stepping...
90.0 %        9.0e-08 s    Ellapsed time so far: 3.2 s

@RemDelaporteMathurin
Copy link
Collaborator

Very nice MWE @KulaginVladimir , would you mind making use of it in a test that catches the bug?

I think you can do something like:

def test_small_timesteps_final_time_bug():
    """Some description"""
    my_model = F.Simulation(log_level = 40)
    
    my_model.mesh = F.MeshFromVertices(np.linspace(0, 1, 10))
    
    my_model.materials = F.Material(
        id=1,
        D_0=1,
        E_D=0.1,
    )
    
    my_model.T = F.Temperature(1000)
    
    my_model.settings = F.Settings(
        absolute_tolerance=1e-10,
        relative_tolerance=1e-10,
        final_time=1e-7,
    )
    
    my_model.dt = F.Stepsize(
        initial_value=1e-10,
    )
    
    my_model.initialise()
    my_model.run()

    assert np.isclose(my_model.t, my_model.settings.final_time, atol=0.0)

Copy link

codecov bot commented Jan 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (7c6ca17) 98.93% compared to head (0d3dd1e) 98.93%.
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #682   +/-   ##
=======================================
  Coverage   98.93%   98.93%           
=======================================
  Files          57       57           
  Lines        2154     2154           
=======================================
  Hits         2131     2131           
  Misses         23       23           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@RemDelaporteMathurin RemDelaporteMathurin left a comment

Choose a reason for hiding this comment

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

Hi @KulaginVladimir just a small comment on my side, Thanks for this fix! 🚀

@RemDelaporteMathurin RemDelaporteMathurin added the bug Something isn't working label Jan 17, 2024
Copy link
Collaborator

@RemDelaporteMathurin RemDelaporteMathurin left a 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 thanks @KulaginVladimir

@RemDelaporteMathurin RemDelaporteMathurin merged commit df6016a into festim-dev:main Jan 18, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants