def export_sbml(model, y0=None, volume=<NUM_LIT:1.0>, is_valid=True):
y0 = y0 or {}<EOL>import libsbml<EOL>document = libsbml.SBMLDocument(<NUM_LIT:3>, <NUM_LIT:1>)<EOL>m = document.createModel()<EOL>comp1 = m.createCompartment()<EOL>comp1.setId('<STR_LIT>')<EOL>comp1.setConstant(True)<EOL>if unit.HAS_PINT:<EOL><INDENT>if isinstance(volume, unit._Quantity):<EOL><INDENT>if unit.STRICT:<EOL><INDENT>if isinstance(volume.magnitude, ecell4_base.core.Real3) and not unit.check_dimensionality(volume, '<STR_LIT>'):<EOL><INDENT>raise ValueError("<STR_LIT>".format(<EOL>volume.dimensionality, volume.u))<EOL><DEDENT>elif not unit.check_dimensionality(volume, '<STR_LIT>'):<EOL><INDENT>raise ValueError("<STR_LIT>".format(<EOL>volume.dimensionality, volume.u))<EOL><DEDENT><DEDENT>volume = volume.to_base_units().magnitude<EOL><DEDENT>y0 = y0.copy()<EOL>for key, value in y0.items():<EOL><INDENT>if isinstance(value, unit._Quantity):<EOL><INDENT>if not unit.STRICT:<EOL><INDENT>y0[key] = value.to_base_units().magnitude<EOL><DEDENT>elif unit.check_dimensionality(value, '<STR_LIT>'):<EOL><INDENT>y0[key] = value.to_base_units().magnitude<EOL><DEDENT>elif unit.check_dimensionality(value, '<STR_LIT>'):<EOL><INDENT>volume = w.volume() if not isinstance(w, ecell4_base.spatiocyte.SpatiocyteWorld) else w.actual_volume()<EOL>y0[key] = value.to_base_units().magnitude * volume<EOL><DEDENT>else:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>".format(<EOL>key, value.dimensionality, value.u))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if isinstance(volume, ecell4_base.core.Real3):<EOL><INDENT>comp1.setSize(volume[<NUM_LIT:0>] * volume[<NUM_LIT:1>] * volume[<NUM_LIT:2>])<EOL><DEDENT>else:<EOL><INDENT>comp1.setSize(volume)<EOL><DEDENT>comp1.setSpatialDimensions(<NUM_LIT:3>)<EOL>species_list = []<EOL>for rr in model.reaction_rules():<EOL><INDENT>for sp in itertools.chain(rr.reactants(), rr.products()):<EOL><INDENT>species_list.append(sp)<EOL><DEDENT><DEDENT>species_list = list(set(species_list))<EOL>species_list.sort()<EOL>sid_map = {}<EOL>for cnt, sp in enumerate(species_list):<EOL><INDENT>sid_map[sp.serial()] = "<STR_LIT>".format(cnt)<EOL><DEDENT>for sp in species_list:<EOL><INDENT>sid = sid_map[sp.serial()]<EOL>s1 = m.createSpecies()<EOL>s1.setId(sid)<EOL>s1.setName(sp.serial())<EOL>s1.setCompartment('<STR_LIT>')<EOL>s1.setConstant(False)<EOL>if sp.serial() in y0.keys():<EOL><INDENT>s1.setInitialAmount(y0[sp.serial()])<EOL><DEDENT>else:<EOL><INDENT>s1.setInitialAmount(<NUM_LIT:0>)<EOL><DEDENT>s1.setBoundaryCondition(False)<EOL>s1.setHasOnlySubstanceUnits(False)<EOL><DEDENT>for cnt, rr in enumerate(model.reaction_rules()):<EOL><INDENT>desc = rr.get_descriptor()<EOL>r1 = m.createReaction()<EOL>r1.setId("<STR_LIT>".format(cnt))<EOL>r1.setReversible(True)<EOL>r1.setFast(False)<EOL>kinetic_law = r1.createKineticLaw()<EOL>species_coef_map = {}<EOL>if desc is None:<EOL><INDENT>for sp in rr.reactants():<EOL><INDENT>if sp not in species_coef_map.keys():<EOL><INDENT>species_coef_map[sp] = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>species_coef_map[sp] += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>for sp, coef in zip(rr.reactants(), desc.reactant_coefficients()):<EOL><INDENT>if sp not in species_coef_map.keys():<EOL><INDENT>species_coef_map[sp] = coef<EOL><DEDENT>else:<EOL><INDENT>species_coef_map[sp] += coef<EOL><DEDENT><DEDENT><DEDENT>if desc is None or isinstance(desc, ecell4_base.core.ReactionRuleDescriptorMassAction):<EOL><INDENT>p1 = m.createParameter()<EOL>p1.setId("<STR_LIT>".format(cnt))<EOL>p1.setConstant(True)<EOL>p1.setValue(rr.k() if desc is None else desc.k())<EOL>math_exp = "<STR_LIT>".format(cnt)<EOL>for sp, coef in species_coef_map.items():<EOL><INDENT>sid = sid_map[sp.serial()]<EOL>if coef == <NUM_LIT:1.0>:<EOL><INDENT>math_exp += "<STR_LIT>".format(sid)<EOL><DEDENT>else:<EOL><INDENT>math_exp += "<STR_LIT>".format(sid, coef)<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(desc, ecell4_base.core.ReactionRuleDescriptorPyfunc):<EOL><INDENT>math_exp = desc.as_string()<EOL>if math_exp in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>warnings.warn(<EOL>"<STR_LIT>".format(<EOL>rr.as_string()))<EOL><DEDENT>math_exp = replace_parseobj(math_exp, sid_map)<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>'.format(type(desc)))<EOL><DEDENT>for sp, coef in species_coef_map.items():<EOL><INDENT>sid = sid_map[sp.serial()]<EOL>s1 = r1.createReactant()<EOL>s1.setSpecies(sid)<EOL>s1.setConstant(False)<EOL>s1.setStoichiometry(coef)<EOL><DEDENT>if desc is None:<EOL><INDENT>for sp in rr.products():<EOL><INDENT>if sp not in species_coef_map.keys():<EOL><INDENT>species_coef_map[sp] = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>species_coef_map[sp] += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>species_coef_map = {}<EOL>for sp, coef in zip(rr.products(), desc.product_coefficients()):<EOL><INDENT>if sp not in species_coef_map.keys():<EOL><INDENT>species_coef_map[sp] = coef<EOL><DEDENT>else:<EOL><INDENT>species_coef_map[sp] += coef<EOL><DEDENT><DEDENT><DEDENT>for sp, coef in species_coef_map.items():<EOL><INDENT>sid = sid_map[sp.serial()]<EOL>s1 = r1.createProduct()<EOL>s1.setSpecies(sid)<EOL>s1.setConstant(False)<EOL>s1.setStoichiometry(coef)<EOL><DEDENT>math_ast = libsbml.parseL3Formula(math_exp)<EOL>kinetic_law.setMath(math_ast)<EOL><DEDENT>if is_valid:<EOL><INDENT>document.validateSBML()<EOL>num_errors = (document.getNumErrors(libsbml.LIBSBML_SEV_ERROR)<EOL>+ document.getNumErrors(libsbml.LIBSBML_SEV_FATAL))<EOL>if num_errors > <NUM_LIT:0>:<EOL><INDENT>messages = "<STR_LIT>"<EOL>messages += "<STR_LIT>".format(num_errors)<EOL>for i in range(document.getNumErrors(libsbml.LIBSBML_SEV_ERROR)):<EOL><INDENT>err = document.getErrorWithSeverity(i, libsbml.LIBSBML_SEV_ERROR)<EOL>messages += "<STR_LIT>".format(err.getSeverityAsString(), err.getShortMessage())<EOL><DEDENT>for i in range(document.getNumErrors(libsbml.LIBSBML_SEV_FATAL)):<EOL><INDENT>err = document.getErrorWithSeverity(i, libsbml.LIBSBML_SEV_FATAL)<EOL>messages += "<STR_LIT>".format(err.getSeverityAsString(), err.getShortMessage())<EOL><DEDENT>raise RuntimeError(messages)<EOL><DEDENT><DEDENT>return document<EOL>
Export a model as a SBMLDocument.

Parameters
----------
model : NetworkModel
y0 : dict
    Initial condition.
volume : Real or Real3, optional
    A size of the simulation volume. 1 as a default.
is_valid : bool, optional
    Check if the generated model is valid. True as a default.