<commit_before>from sfepy.base.base import *

import sfepy.base.ioutils as io
from sfepy.fem import ProblemDefinition
from sfepy.base.conf import get_standard_keywords

##
# c: 03.07.2007, r: 27.02.2008
def save_only( conf, save_names, problem = None ):
    """Save information available prior to setting equations and
    solving them."""
    if problem is None:
        problem = ProblemDefinition.from_conf( conf, init_variables = False )

    if save_names.regions is not None:
        problem.save_regions( save_names.regions )

    if save_names.field_meshes is not None:
        problem.save_field_meshes( save_names.field_meshes )

    if save_names.region_field_meshes is not None:
        problem.save_region_field_meshes( save_names.region_field_meshes )

    if save_names.ebc is not None:
        if not hasattr( problem, 'variables' ):
            problem.set_variables( conf.variables )
        try:
            ts = TimeStepper.from_conf( conf.ts )
            ts.set_step( 0 )
        except:
            ts = None
        try:
            problem.variables.equation_mapping( conf.ebcs, conf.epbcs,
                                                problem.domain.regions, ts,
                                                conf.funmod )
        except Exception, e:
            output( 'cannot make equation mapping!' )
            output( 'reason: %s' % e )
        else:
            problem.save_ebc( save_names.ebc )

##
# 20.03.2007, c
# 30.03.2007
# 28.05.2007
# 03.07.2007
# 18.07.2007
# 02.10.2007
# 03.10.2007
def solve_stationary( conf, data = None, save_names = None, nls_status = None ):

    if data is None:
        # Term-dependent data.
        data = {}
    problem = ProblemDefinition.from_conf( conf )

    problem.time_update( None )

    if save_names is not None:
        save_only( conf, save_names, problem = problem )

    state = problem.solve( nls_status = nls_status )

    return problem, state, data


def prepare_save_data( ts, conf ):
    try:
        save_steps = conf.options.save_steps
    except:
        save_steps = -1
    if save_steps == -1:
        save_steps = ts.n_step

    is_save = nm.linspace( 0, ts.n_step - 1, save_steps ).astype( nm.int32 )
    is_save = nm.unique1d( is_save )

    return ts.suffix, is_save

def time_step_function( ts, state0, problem, data ):
    problem.time_update( ts )

    if ts.step == 0:
        problem.apply_ebc( state0 )
        state = state0.copy()
        problem.init_time( ts )

        if problem.equations.caches:
            # Initialize caches.
            ev = problem.get_evaluator( ts = ts, **data )
            vec_r, ret = ev.eval_residual( state )
            if ret == 0: # OK.
                err = nla.norm( vec_r )
                output( 'initial residual: %e' % err )
            else:
                output( 'initial residual evaluation failed, giving up...' )
                raise ValueError

        if problem.is_linear():
            # Assemble linear system matrix for all
            # time steps.
            ev = problem.get_evaluator( ts = ts, mtx = problem.mtx_a, **data )
            mtx_a, ret = ev.eval_tangent_matrix( state )
            if ret != 0:
                output( 'matrix evaluation failed, giving up...' )
                raise ValueError
        else:
            mtx_a = None
<commit_msg>initialize data of state variables also in case of no term caches<commit_after>from sfepy.base.base import *

import sfepy.base.ioutils as io
from sfepy.fem import ProblemDefinition
from sfepy.base.conf import get_standard_keywords

##
# c: 03.07.2007, r: 27.02.2008
def save_only( conf, save_names, problem = None ):
    """Save information available prior to setting equations and
    solving them."""
    if problem is None:
        problem = ProblemDefinition.from_conf( conf, init_variables = False )

    if save_names.regions is not None:
        problem.save_regions( save_names.regions )

    if save_names.field_meshes is not None:
        problem.save_field_meshes( save_names.field_meshes )

    if save_names.region_field_meshes is not None:
        problem.save_region_field_meshes( save_names.region_field_meshes )

    if save_names.ebc is not None:
        if not hasattr( problem, 'variables' ):
            problem.set_variables( conf.variables )
        try:
            ts = TimeStepper.from_conf( conf.ts )
            ts.set_step( 0 )
        except:
            ts = None
        try:
            problem.variables.equation_mapping( conf.ebcs, conf.epbcs,
                                                problem.domain.regions, ts,
                                                conf.funmod )
        except Exception, e:
            output( 'cannot make equation mapping!' )
            output( 'reason: %s' % e )
        else:
            problem.save_ebc( save_names.ebc )

##
# 20.03.2007, c
# 30.03.2007
# 28.05.2007
# 03.07.2007
# 18.07.2007
# 02.10.2007
# 03.10.2007
def solve_stationary( conf, data = None, save_names = None, nls_status = None ):

    if data is None:
        # Term-dependent data.
        data = {}
    problem = ProblemDefinition.from_conf( conf )

    problem.time_update( None )

    if save_names is not None:
        save_only( conf, save_names, problem = problem )

    state = problem.solve( nls_status = nls_status )

    return problem, state, data


def prepare_save_data( ts, conf ):
    try:
        save_steps = conf.options.save_steps
    except:
        save_steps = -1
    if save_steps == -1:
        save_steps = ts.n_step

    is_save = nm.linspace( 0, ts.n_step - 1, save_steps ).astype( nm.int32 )
    is_save = nm.unique1d( is_save )

    return ts.suffix, is_save

def time_step_function( ts, state0, problem, data ):
    problem.time_update( ts )

    if ts.step == 0:
        problem.apply_ebc( state0 )
        state = state0.copy()
        problem.init_time( ts )

        if problem.equations.caches:
            # Initialize caches.
            ev = problem.get_evaluator( ts = ts, **data )
            vec_r, ret = ev.eval_residual( state )
            if ret == 0: # OK.
                err = nla.norm( vec_r )
                output( 'initial residual: %e' % err )
            else:
                output( 'initial residual evaluation failed, giving up...' )
                raise ValueError
        else:
            # Just initialize data of state variables.
            problem.variables.data_from_state( state )

        if problem.is_linear():
            # Assemble linear system matrix for all
            # time steps.
            ev = problem.get_evaluator( ts = ts, mtx = problem.mtx_a, **data )
            mtx_a, ret = ev.eval_tangent_matrix( state )
            if ret != 0:
                output( 'matrix evaluation failed, giving up...' )
                raise ValueError
        else:
            mtx_a = None

{'fraction_non_alphanumeric': 0.09214369158878505, 'fraction_numerical': 0.02511682242990654, 'mean_word_length': 1.9119897959183674, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2939586', 'n_tokens_mistral': 2219, 'n_tokens_neox': 2042, 'n_words': 564}