예제 #1
0
파일: homogen.py 프로젝트: Gkdnz/sfepy
def main():
    parser = OptionParser(usage=usage, version="%prog " + sfepy.__version__)
    parser.add_option("-o", "", metavar='filename', action="store",
                      dest="output_filename_trunk",
                      default=None, help=help['filename'])

    (options, args) = parser.parse_args()

    if (len(args) == 1):
        filename_in = args[0]
    else:
        parser.print_help(),
        return

    required, other = get_standard_keywords()
    required.remove('equations')

    conf = ProblemConf.from_file(filename_in, required, other)

    app = HomogenizationApp(conf, options, 'homogen:')
    opts = conf.options
    if hasattr(opts, 'parametric_hook'):  # Parametric study.
        parametric_hook = conf.get_function(opts.parametric_hook)
        app.parametrize(parametric_hook)
    app()
예제 #2
0
파일: homogen.py 프로젝트: clazaro/sfepy
def main():
    parser = ArgumentParser()
    parser.add_argument("--version", action="version",
                        version="%(prog)s " + sfepy.__version__)
    parser.add_argument('--debug',
                        action='store_true', dest='debug',
                        default=False, help=helps['debug'])
    parser.add_argument("-o", metavar='filename', action="store",
                        dest="output_filename_trunk",
                        default=None, help=helps['filename'])
    parser.add_argument('filename_in')
    options = parser.parse_args()

    if options.debug:
        from sfepy.base.base import debug_on_error; debug_on_error()

    filename_in = options.filename_in

    required, other = get_standard_keywords()
    required.remove('equations')

    conf = ProblemConf.from_file(filename_in, required, other)

    app = HomogenizationApp(conf, options, 'homogen:')
    opts = conf.options
    if hasattr(opts, 'parametric_hook'):  # Parametric study.
        parametric_hook = conf.get_function(opts.parametric_hook)
        app.parametrize(parametric_hook)
    app()
예제 #3
0
파일: material_opt.py 프로젝트: Gkdnz/sfepy
    def create_app(filename, is_homog=False, **kwargs):
        from sfepy.base.conf import ProblemConf, get_standard_keywords
        from sfepy.homogenization.homogen_app import HomogenizationApp
        from sfepy.applications import PDESolverApp

        required, other = get_standard_keywords()
        if is_homog:
            required.remove('equations')

        conf = ProblemConf.from_file(filename, required, other,
                                     define_args=kwargs)
        options = Struct(output_filename_trunk=None,
                         save_ebc=False,
                         save_ebc_nodes=False,
                         save_regions=False,
                         save_regions_as_groups=False,
                         save_field_meshes=False,
                         solve_not=False)

        if is_homog:
            app = HomogenizationApp(conf, options, 'material_opt_micro:')

        else:
            app = PDESolverApp(conf, options, 'material_opt_macro:')

        app.conf.opt_data = {}
        opts = conf.options
        if hasattr(opts, 'parametric_hook'):  # Parametric study.
            parametric_hook = conf.get_function(opts.parametric_hook)
            app.parametrize(parametric_hook)

        return app
예제 #4
0
def main():
    parser = OptionParser(usage=usage, version="%prog " + sfepy.__version__)
    parser.add_option("-o",
                      "",
                      metavar='filename',
                      action="store",
                      dest="output_filename_trunk",
                      default=None,
                      help=help['filename'])

    (options, args) = parser.parse_args()

    if (len(args) == 1):
        filename_in = args[0]
    else:
        parser.print_help(),
        return

    required, other = get_standard_keywords()
    required.remove('equations')

    conf = ProblemConf.from_file(filename_in, required, other)

    app = HomogenizationApp(conf, options, 'homogen:')
    opts = conf.options
    if hasattr(opts, 'parametric_hook'):  # Parametric study.
        parametric_hook = conf.get_function(opts.parametric_hook)
        app.parametrize(parametric_hook)
    app()
예제 #5
0
    def create_app(filename, is_homog=False, **kwargs):
        from sfepy.base.conf import ProblemConf, get_standard_keywords
        from sfepy.homogenization.homogen_app import HomogenizationApp
        from sfepy.applications import PDESolverApp

        required, other = get_standard_keywords()
        if is_homog:
            required.remove('equations')

        conf = ProblemConf.from_file(filename,
                                     required,
                                     other,
                                     define_args=kwargs)
        options = Struct(output_filename_trunk=None,
                         save_ebc=False,
                         save_ebc_nodes=False,
                         save_regions=False,
                         save_regions_as_groups=False,
                         save_field_meshes=False,
                         solve_not=False)

        if is_homog:
            app = HomogenizationApp(conf, options, 'material_opt_micro:')

        else:
            app = PDESolverApp(conf, options, 'material_opt_macro:')

        app.conf.opt_data = {}
        opts = conf.options
        if hasattr(opts, 'parametric_hook'):  # Parametric study.
            parametric_hook = conf.get_function(opts.parametric_hook)
            app.parametrize(parametric_hook)

        return app
예제 #6
0
    def setup_options(self):
        HomogenizationApp.setup_options(self)

        if self.options.phase_velocity:
            process_options = AcousticBandGapsApp.process_options_pv
        else:
            process_options = AcousticBandGapsApp.process_options
        self.app_options += process_options(self.conf.options)
예제 #7
0
파일: micmac.py 프로젝트: rocksonchan/sfepy
def get_homog_coefs_linear(ts, coor, mode,
                           micro_filename=None, regenerate=False,
                           coefs_filename=None, define_args=None):

    oprefix = output.prefix
    output.prefix = 'micro:'

    required, other = get_standard_keywords()
    required.remove( 'equations' )

    conf = ProblemConf.from_file(micro_filename, required, other,
                                 verbose=False, define_args=define_args)
    if coefs_filename is None:
        coefs_filename = conf.options.get('coefs_filename', 'coefs')
        coefs_filename = op.join(conf.options.get('output_dir', '.'),
                                 coefs_filename) + '.h5'

    if not regenerate:
        if op.exists( coefs_filename ):
            if not pt.is_hdf5_file( coefs_filename ):
                regenerate = True
        else:
            regenerate = True

    if regenerate:
        options = Struct( output_filename_trunk = None )

        app = HomogenizationApp( conf, options, 'micro:' )
        coefs = app()
        if type(coefs) is tuple:
            coefs = coefs[0]

        coefs.to_file_hdf5( coefs_filename )
    else:
        coefs = Coefficients.from_file_hdf5( coefs_filename )

    out = {}
    if mode == None:
        for key, val in six.iteritems(coefs.__dict__):
            out[key] = val

    elif mode == 'qp':
        for key, val in six.iteritems(coefs.__dict__):
            if type( val ) == nm.ndarray or type(val) == nm.float64:
                out[key] = nm.tile( val, (coor.shape[0], 1, 1) )
            elif type(val) == dict:
                for key2, val2 in six.iteritems(val):
                    if type(val2) == nm.ndarray or type(val2) == nm.float64:
                        out[key+'_'+key2] = \
                                          nm.tile(val2, (coor.shape[0], 1, 1))

    else:
        out = None

    output.prefix = oprefix

    return out
예제 #8
0
def main():
    parser = ArgumentParser()
    parser.add_argument("--version",
                        action="version",
                        version="%(prog)s " + sfepy.__version__)
    parser.add_argument('--debug',
                        action='store_true',
                        dest='debug',
                        default=False,
                        help=help['debug'])
    parser.add_argument("-o",
                        metavar='filename',
                        action="store",
                        dest="output_filename_trunk",
                        default=None,
                        help=help['filename'])
    parser.add_argument('filename_in')
    options = parser.parse_args()

    if options.debug:
        from sfepy.base.base import debug_on_error
        debug_on_error()

    filename_in = options.filename_in

    required, other = get_standard_keywords()
    required.remove('equations')

    conf = ProblemConf.from_file(filename_in, required, other)

    app = HomogenizationApp(conf, options, 'homogen:')
    opts = conf.options
    if hasattr(opts, 'parametric_hook'):  # Parametric study.
        parametric_hook = conf.get_function(opts.parametric_hook)
        app.parametrize(parametric_hook)
    app()
예제 #9
0
def main():
    # if multi_mpi.cpu_count() < 2:
    #     raise ValueError('MPI mode - the number of nodes is less then 2!')

    if multi_mpi.mpi_rank == multi_mpi.mpi_master:
        # MPI master node - solve problem at macro scale
        parser = ArgumentParser(description=__doc__,
                                formatter_class=RawDescriptionHelpFormatter)
        parser.add_argument('--debug',
                            action='store_true', dest='debug',
                            default=False, help=helps['debug'])
        parser.add_argument('--debug_mpi',
                            action='store_true', dest='debug_mpi',
                            default=False, help=helps['debug_mpi'])
        parser.add_argument('-c', '--conf', metavar='"key : value, ..."',
                            action='store', dest='conf', type=str,
                            default=None, help=helps['conf'])
        parser.add_argument('-O', '--options', metavar='"key : value, ..."',
                            action='store', dest='app_options', type=str,
                            default=None, help=helps['options'])
        parser.add_argument('-d', '--define', metavar='"key : value, ..."',
                            action='store', dest='define_args', type=str,
                            default=None, help=helps['define'])
        parser.add_argument('-o', metavar='filename',
                            action='store', dest='output_filename_trunk',
                            default=None, help=helps['filename'])
        parser.add_argument('--format', metavar='format',
                            action='store', dest='output_format',
                            default=None, help=helps['output_format'])
        parser.add_argument('--log', metavar='file',
                            action='store', dest='log',
                            default=None, help=helps['log'])
        parser.add_argument('-q', '--quiet',
                            action='store_true', dest='quiet',
                            default=False, help=helps['quiet'])
        group = parser.add_mutually_exclusive_group(required=True)
        group.add_argument('filename_in', nargs='?')
        options = parser.parse_args()

        for k in ['save_ebc', 'save_ebc_nodes', 'save_regions',
                  'save_regions_as_groups', 'save_field_meshes', 'solve_not']:
            setattr(options, k, False)

        if options.debug:
            from sfepy.base.base import debug_on_error; debug_on_error()

        if options.debug_mpi:
            multi_mpi.set_logging_level('debug')

        filename_in = options.filename_in
        output.set_output(filename=options.log,
                          quiet=options.quiet,
                          combined=options.log is not None)

        required, other = get_standard_keywords()
        conf = ProblemConf.from_file_and_options(filename_in, options,
            required, other, define_args=options.define_args)

        opts = conf.options
        nslaves = multi_mpi.cpu_count() - 1
        opts.n_mpi_homog_slaves = nslaves
        output_prefix = opts.get('output_prefix', 'sfepy:')

        app = PDESolverApp(conf, options, output_prefix)
        if hasattr(opts, 'parametric_hook'):  # Parametric study.
            parametric_hook = conf.get_function(opts.parametric_hook)
            app.parametrize(parametric_hook)
        app()

        multi_mpi.master_send_task('finalize', None)
    else:
        # MPI slave mode - calculate homogenized coefficients
        homogen_app = None
        done = False
        rank = multi_mpi.mpi_rank
        while not done:
            task, data = multi_mpi.slave_get_task('main slave loop')

            if task == 'init':  # data: micro_file, n_micro
                output.set_output(filename='homog_app_mpi_%d.log' % rank,
                                  quiet=True)
                micro_file, n_micro = data[:2]
                required, other = get_standard_keywords()
                required.remove('equations')
                conf = ProblemConf.from_file(micro_file, required, other,
                                             verbose=False)
                options = Struct(output_filename_trunk=None)
                homogen_app = HomogenizationApp(conf, options, 'micro:',
                                                n_micro=n_micro,
                                                update_micro_coors=True)
            elif task == 'calculate':  # data: rel_def_grad, ts, iteration
                rel_def_grad, ts, iteration = data[:3]
                homogen_app.setup_macro_deformation(rel_def_grad)
                homogen_app(ret_all=True, itime=ts.step, iiter=iteration)
            elif task == 'finalize':
                done = True
예제 #10
0
def get_homog_coefs_nonlinear(ts, coor, mode, mtx_f=None,
                              term=None, problem=None,
                              iteration=None, **kwargs):
    if not (mode == 'qp'):
        return

    oprefix = output.prefix
    output.prefix = 'micro:'

    if not hasattr(problem, 'homogen_app'):
        required, other = get_standard_keywords()
        required.remove('equations')
        micro_file = problem.conf.options.micro_filename
        conf = ProblemConf.from_file(micro_file, required, other,
                                     verbose=False)
        options = Struct(output_filename_trunk=None)
        app = HomogenizationApp(conf, options, 'micro:',
                                n_micro=coor.shape[0], update_micro_coors=True)
        problem.homogen_app = app

        if hasattr(app.app_options, 'use_mpi') and app.app_options.use_mpi:
            multiproc, multiproc_mode = multi.get_multiproc(mpi=True)
            multi_mpi = multiproc if multiproc_mode == 'mpi' else None
        else:
            multi_mpi = None

        app.multi_mpi = multi_mpi

        if multi_mpi is not None:
            multi_mpi.master_send_task('init', (micro_file, coor.shape[0]))
    else:
        app = problem.homogen_app
        multi_mpi = app.multi_mpi

    def_grad = mtx_f(problem, term) if callable(mtx_f) else mtx_f
    if hasattr(problem, 'def_grad_prev'):
        rel_def_grad = la.dot_sequences(def_grad,
                                        nm.linalg.inv(problem.def_grad_prev),
                                        'AB')
    else:
        rel_def_grad = def_grad.copy()

    problem.def_grad_prev = def_grad.copy()
    app.setup_macro_deformation(rel_def_grad)

    if multi_mpi is not None:
        multi_mpi.master_send_task('calculate', (rel_def_grad, ts, iteration))

    coefs, deps = app(ret_all=True, itime=ts.step, iiter=iteration)

    if type(coefs) is tuple:
        coefs = coefs[0]

    out = {}
    for key, val in six.iteritems(coefs.__dict__):
        if isinstance(val, list):
            out[key] = nm.array(val)
        elif isinstance(val, dict):
            for key2, val2 in six.iteritems(val):
                out[key+'_'+key2] = nm.array(val2)

    for key in six.iterkeys(out):
        shape = out[key].shape
        if len(shape) == 1:
            out[key] = out[key].reshape(shape + (1, 1))
        elif len(shape) == 2:
            out[key] = out[key].reshape(shape + (1,))

    output.prefix = oprefix

    return out
예제 #11
0
    def test_solution(self):

        from sfepy.base.base import Struct
        from sfepy.base.conf import ProblemConf, get_standard_keywords
        from sfepy.homogenization.homogen_app import HomogenizationApp
        #import numpy as nm
        import os.path as op

        ok = True

        required, other = get_standard_keywords()
        required.remove('equations')
        print(input_name)
        full_name = op.join(op.dirname(__file__), input_name)
        test_conf = ProblemConf.from_file(full_name, required, other)

        options = Struct(output_filename_trunk=None,
                         save_ebc=False,
                         save_ebc_nodes=False,
                         save_regions=False,
                         save_field_meshes=False,
                         save_regions_as_groups=False,
                         solve_not=False)

        test_conf.options['output_dir'] = './output-tests'
        app = HomogenizationApp(test_conf, options, 'homogen:' )
        coefs = app()

        aerr = 1.0e-9
        self.report('allowed error: abs = %e' % (aerr, ))

        # G^A = G^B ?
        ok = ok and self.compare_scalars(coefs.GA, coefs.GB,\
                                         'G^A', 'G^B', aerr)

        # F^{A+} + F^{B+} = -1/h \int_{\partial_+Y_m} ?
        aux = 1.0 / test_conf.param_h * coefs.volume['bYMp']
        ok = ok and self.compare_scalars(coefs.FpA + coefs.FpB, -aux,
                                         'F^{A+} + F^{B+}', '-bYM^+', aerr)

        # F^{A-} + F^{B-} = -1/h \int_{\partial_-Y_m} ?
        aux = 1.0 / test_conf.param_h * coefs.volume['bYMm']
        ok = ok and self.compare_scalars(coefs.FmA + coefs.FmB, -aux,
                                         'F^{A-} + F^{B-}', '-bYM^-', aerr)

        # symmetry of H ?
        ok = ok and self.compare_scalars(coefs.Hpm, coefs.Hmp,
                                         'H^{+-}', 'H^{-+}', aerr)

        # E = -F ?
        ok = ok and self.compare_scalars(coefs.EmA, -coefs.FmA,
                                         'E^{A-}', '-F^{A-}',aerr)
        ok = ok and self.compare_scalars(coefs.EpA, -coefs.FpA,
                                         'E^{A+}', '-F^{A+}',aerr)
        ok = ok and self.compare_scalars(coefs.EmB, -coefs.FmB,
                                         'E^{B-}', '-F^{B-}',aerr)
        ok = ok and self.compare_scalars(coefs.EpB, -coefs.FpB,
                                         'E^{B+}', '-F^{B+}',aerr)

        # S = S_test ?
        coefsd = coefs.to_dict()
        compare = []
        for ii in six.iterkeys(coefsd):
            if 'S_test' in ii:
                ch = ii[6]
                io = ii[-1]
                compare.append((ii, 'S%s_%s' % (ch, io)))

        for s1, s2 in compare:
            ok = ok and self.compare_vectors(coefsd[s1], -coefsd[s2],
                                             label1='S_test', label2='S',
                                             allowed_error=aerr)

        return ok
예제 #12
0
def main():
    # if multi_mpi.cpu_count() < 2:
    #     raise ValueError('MPI mode - the number of nodes is less then 2!')

    if multi_mpi.mpi_rank == multi_mpi.mpi_master:
        # MPI master node - solve problem at macro scale
        parser = ArgumentParser(description=__doc__,
                                formatter_class=RawDescriptionHelpFormatter)
        parser.add_argument('--debug',
                            action='store_true',
                            dest='debug',
                            default=False,
                            help=helps['debug'])
        parser.add_argument('--debug_mpi',
                            action='store_true',
                            dest='debug_mpi',
                            default=False,
                            help=helps['debug_mpi'])
        parser.add_argument('-c',
                            '--conf',
                            metavar='"key : value, ..."',
                            action='store',
                            dest='conf',
                            type=str,
                            default=None,
                            help=helps['conf'])
        parser.add_argument('-O',
                            '--options',
                            metavar='"key : value, ..."',
                            action='store',
                            dest='app_options',
                            type=str,
                            default=None,
                            help=helps['options'])
        parser.add_argument('-d',
                            '--define',
                            metavar='"key : value, ..."',
                            action='store',
                            dest='define_args',
                            type=str,
                            default=None,
                            help=helps['define'])
        parser.add_argument('-o',
                            metavar='filename',
                            action='store',
                            dest='output_filename_trunk',
                            default=None,
                            help=helps['filename'])
        parser.add_argument('--format',
                            metavar='format',
                            action='store',
                            dest='output_format',
                            default=None,
                            help=helps['output_format'])
        parser.add_argument('--log',
                            metavar='file',
                            action='store',
                            dest='log',
                            default=None,
                            help=helps['log'])
        parser.add_argument('-q',
                            '--quiet',
                            action='store_true',
                            dest='quiet',
                            default=False,
                            help=helps['quiet'])
        group = parser.add_mutually_exclusive_group(required=True)
        group.add_argument('filename_in', nargs='?')
        options = parser.parse_args()

        for k in [
                'save_ebc', 'save_ebc_nodes', 'save_regions',
                'save_regions_as_groups', 'save_field_meshes', 'solve_not'
        ]:
            setattr(options, k, False)

        if options.debug:
            from sfepy.base.base import debug_on_error
            debug_on_error()

        if options.debug_mpi:
            multi_mpi.set_logging_level('debug')

        filename_in = options.filename_in
        output.set_output(filename=options.log,
                          quiet=options.quiet,
                          combined=options.log is not None)

        required, other = get_standard_keywords()
        conf = ProblemConf.from_file_and_options(
            filename_in,
            options,
            required,
            other,
            define_args=options.define_args)

        opts = conf.options
        nslaves = multi_mpi.cpu_count() - 1
        opts.n_mpi_homog_slaves = nslaves
        output_prefix = opts.get('output_prefix', 'sfepy:')

        app = PDESolverApp(conf, options, output_prefix)
        if hasattr(opts, 'parametric_hook'):  # Parametric study.
            parametric_hook = conf.get_function(opts.parametric_hook)
            app.parametrize(parametric_hook)
        app()

        multi_mpi.master_send_task('finalize', None)
    else:
        # MPI slave mode - calculate homogenized coefficients
        homogen_app = None
        done = False
        rank = multi_mpi.mpi_rank
        while not done:
            task, data = multi_mpi.slave_get_task('main slave loop')

            if task == 'init':  # data: micro_file, n_micro
                output.set_output(filename='homog_app_mpi_%d.log' % rank,
                                  quiet=True)
                micro_file, n_micro = data[:2]
                required, other = get_standard_keywords()
                required.remove('equations')
                conf = ProblemConf.from_file(micro_file,
                                             required,
                                             other,
                                             verbose=False)
                options = Struct(output_filename_trunk=None)
                homogen_app = HomogenizationApp(conf,
                                                options,
                                                'micro:',
                                                n_micro=n_micro)
            elif task == 'calculate':  # data: rel_def_grad, ts, iteration
                macro_data, ts, iteration = data[:3]
                homogen_app.setup_macro_data(macro_data)
                homogen_app(ret_all=True, itime=ts.step, iiter=iteration)
            elif task == 'finalize':
                done = True