225 x_size(system.getSpecies().size()),
226 f_x_size(system.getComponents().size()
227 + system.getReactions().size()),
228 reaction_offset(system.getComponents().size()),
229 components_indexes(system.getComponents().size()),
230 species_indexes(system.getSpecies().size()),
231 fixed_activities(system.getComponents().size()) {
233 components_indexes[component->getIndex()] = component->getIndex();
234 for(
const auto& species : system.
getSpecies())
235 species_indexes[species->getIndex()]
236 = species->getIndex();
238 std::vector<std::size_t> species_offsets(system.
getSpecies().size());
240 CHEM_LOGV(6) <<
"C:" << component->getSpecies()->getName();
241 if(component->isFixed()) {
242 fixed_activities[component->getIndex()]
243 = component->getSpecies()->activity();
250 std::size_t i = component->getIndex()+1;
251 i < components_indexes.size(); i++) {
252 --components_indexes[i];
255 species_indexes[component->getSpecies()->getIndex()] =
INVALID_INDEX;
257 std::size_t i = component->getSpecies()->getIndex()+1;
258 i < species_indexes.size(); i++) {
259 --species_indexes[i];
260 ++species_offsets[i];
265 revert_species_indexes.resize(x_size);
266 for(
const auto& species : system.
getSpecies()) {
268 revert_species_indexes[species_indexes[species->getIndex()]]
269 = species_indexes[species->getIndex()]+species_offsets[species->getIndex()];
284 X complete_activities(system.getSpecies().size());
285 for(
const auto& component : system.getComponents())
286 if(component->isFixed())
287 complete_activities[component->getSpecies()->getIndex()]
288 = component->getSpecies()->activity();
289 for(std::size_t i = 0; i < reduced_activities.size(); i++) {
290 complete_activities[revert_species_indexes[i]]
291 = reduced_activities[i];
293 return complete_activities;
303 X complete_activities = completeActivities(activities);
305 X mass_conservation_results(system.getComponents().size());
307 system.massConservationLaw(
308 complete_activities, mass_conservation_results);
309 for(std::size_t i = 0; i < mass_conservation_results.size(); i++) {
310 if(components_indexes[i] != INVALID_INDEX)
311 f_x[components_indexes[i]] = mass_conservation_results[i];
315 for(
const auto& reaction : system.getReactions()) {
316 f_x[reaction_offset+reaction->getIndex()]
317 = system.distanceToEquilibrium(complete_activities, *reaction);
324 M jacobian(f_x_size);
325 std::vector<std::size_t> component_offsets;
328 for(
auto& component : system.getComponents()) {
329 if(components_indexes[component->getIndex()] != INVALID_INDEX) {
330 auto& d_f = jacobian[components_indexes[component->getIndex()]];
332 d_f[species_indexes[component->getSpecies()->getIndex()]] = 1.0;
335 for(
const auto& reaction : system.getReactions()) {
338 = system.getSpeciesReagent(*reaction);
341 for(
const auto& reagent : system.getComponentReagents(*reaction)) {
342 if(components_indexes[reagent.component->getIndex()] != INVALID_INDEX)
344 [components_indexes[reagent.component->getIndex()]]
352 for(
const auto& reaction : system.getReactions()) {
353 auto& d_f = jacobian[reaction_offset + reaction->getIndex()];
357 for(
const auto& dx_species : system.getSpecies()) {
360 if(species_indexes[dx_species->getIndex()] != INVALID_INDEX) {
361 d_f[species_indexes[dx_species->getIndex()]] = 0.0;
362 T d_reactives = -reaction->getK();
364 double species_coefficient_in_reactions;
365 bool species_in_reaction =
false;
368 const auto& reagent = system.getSpeciesReagent(*reaction);
369 if(reagent.species == dx_species.get()) {
372 species_coefficient_in_reactions = reagent.coefficient;
373 species_in_reaction =
true;
377 if(activities[species_indexes[reagent.species->getIndex()]] != 0.0) {
378 if(reagent.coefficient < 0.0) {
379 d_products *= std::pow(
380 activities[species_indexes[reagent.species->getIndex()]],
384 d_reactives *= std::pow(
385 activities[species_indexes[reagent.species->getIndex()]],
390 if(reagent.coefficient < 0.0) {
399 for(
const auto& reagent : system.getComponentReagents(*reaction)) {
400 if(reagent.component->getSpecies() == dx_species.get()) {
401 species_coefficient_in_reactions = reagent.coefficient;
402 species_in_reaction =
true;
404 T species_activity = 0.0;
405 if(species_indexes[reagent.component->getSpecies()->getIndex()] != INVALID_INDEX) {
406 species_activity = activities[species_indexes[reagent.component->getSpecies()->getIndex()]];
408 species_activity = fixed_activities[reagent.component->getIndex()];
410 if(species_activity != 0.0) {
411 if(reagent.coefficient < 0.0) {
412 d_products *= std::pow(
417 d_reactives *= std::pow(
423 if(reagent.coefficient < 0.0) {
433 if(species_in_reaction) {
434 std::size_t index = species_indexes[dx_species->getIndex()];
435 T activity = activities[index];
437 if(species_coefficient_in_reactions < 0)
438 d_f[index] = -species_coefficient_in_reactions
441 -species_coefficient_in_reactions-1
444 d_f[index] = species_coefficient_in_reactions
447 species_coefficient_in_reactions-1