Memory full cache

This example shows how to manipulate a MemoryFullCache object.

from __future__ import division, unicode_literals

from numpy import array

from gemseo.api import configure_logger
from gemseo.caches.memory_full_cache import MemoryFullCache

configure_logger()

Out:

<RootLogger root (INFO)>

Import

First, we import the array and the MemoryError classes.

Create

We can create an instance of the MemoryFullCache class. We can then print it, and we can see it is empty.

cache = MemoryFullCache()
print(cache)

Out:

Name: MemoryFullCache
Type: MemoryFullCache
Tolerance: 0.0
Input names: None
Output names: None
Length: 0

Cache

We can manually add data into the cache. However, it has to be noted that most of the time a cache is attached to an MDODiscipline. Then, the cache feeding has not to be performed explicitly by the user.

data = {"x": array([1.0]), "y": array([2.0])}
cache.cache_outputs(data, ["x"], data, ["y"])
data = {"x": array([2.0]), "y": array([3.0])}
cache.cache_outputs(data, ["x"], data, ["y"])
print(cache)

Out:

Name: MemoryFullCache
Type: MemoryFullCache
Tolerance: 0.0
Input names: ['x']
Output names: ['y']
Length: 2

Get all data

Once the cache has been filled, the user can get the length of the cache. The user can also print all the data contained inside the cache.

print(cache.get_length())
print(cache.get_all_data())

Out:

2
{1: {'inputs': {'x': array([1.])}, 'outputs': {'y': array([2.])}, 'jacobian': None}, 2: {'inputs': {'x': array([2.])}, 'outputs': {'y': array([3.])}, 'jacobian': None}}

Get last cached data

The user can access the last entry (inputs or outputs) which have been entered in the cache.

print(cache.get_last_cached_inputs())
print(cache.get_last_cached_outputs())

Out:

{'x': array([2.])}
{'y': array([3.])}

Clear

The user can clear an cache of all its entries by using the MemoryFullCache.clear() method:

cache.clear()
print(cache)

Out:

Name: MemoryFullCache
Type: MemoryFullCache
Tolerance: 0.0
Input names: None
Output names: None
Length: 0

Total running time of the script: ( 0 minutes 0.030 seconds)

Gallery generated by Sphinx-Gallery