bool CFitProblem::calculate() { mCounter += 1; bool Continue = true; unsigned i, imax = mpExperimentSet->getExperimentCount(); unsigned j; unsigned kmax; mCalculateValue = 0.0; CExperiment * pExp = NULL; C_FLOAT64 * Residuals = mResiduals.array(); C_FLOAT64 * DependentValues = mExperimentDependentValues.array(); UpdateMethod ** pUpdate = mExperimentUpdateMethods.array(); std::vector<COptItem *>::iterator itItem; std::vector<COptItem *>::iterator endItem = mpOptItems->end(); std::vector<COptItem *>::iterator itConstraint; std::vector<COptItem *>::iterator endConstraint = mpConstraintItems->end(); std::vector< Refresh *>::const_iterator itRefresh; std::vector< Refresh *>::const_iterator endRefresh; // Reset the constraints memory for (itConstraint = mpConstraintItems->begin(); itConstraint != endConstraint; ++itConstraint) static_cast<CFitConstraint *>(*itConstraint)->resetConstraintViolation(); CFitConstraint **ppConstraint = mExperimentConstraints.array(); CFitConstraint **ppConstraintEnd; try { for (i = 0; i < imax && Continue; i++) // For each experiment { pExp = mpExperimentSet->getExperiment(i); mpModel->setInitialState(*mpInitialState); mpModel->updateInitialValues(); // set the global and experiment local fit item values. for (itItem = mpOptItems->begin(); itItem != endItem; itItem++, pUpdate++) if (*pUpdate) (**pUpdate)(static_cast<CFitItem *>(*itItem)->getLocalValue()); // Update initial values which changed due to the fit item values. itRefresh = mExperimentInitialRefreshes[i].begin(); endRefresh = mExperimentInitialRefreshes[i].end(); while (itRefresh != endRefresh) (**itRefresh++)(); kmax = pExp->getNumDataRows(); switch (pExp->getExperimentType()) { case CCopasiTask::steadyState: // set independent data for (j = 0; j < kmax && Continue; j++) // For each data row; { pExp->updateModelWithIndependentData(j); Continue = mpSteadyState->process(true); if (!Continue) { mFailedCounter++; mCalculateValue = mInfinity; break; } // We check after each simulation whether the constraints are violated. // Make sure the constraint values are up to date. itRefresh = mExperimentConstraintRefreshes[i].begin(); endRefresh = mExperimentConstraintRefreshes[i].end(); for (; itRefresh != endRefresh; ++itRefresh) (**itRefresh)(); ppConstraint = mExperimentConstraints[i]; ppConstraintEnd = ppConstraint + mExperimentConstraints.numCols(); for (; ppConstraint != ppConstraintEnd; ++ppConstraint) if (*ppConstraint)(*ppConstraint)->calculateConstraintViolation(); if (mStoreResults) mCalculateValue += pExp->sumOfSquaresStore(j, DependentValues); else mCalculateValue += pExp->sumOfSquares(j, Residuals); } break; case CCopasiTask::timeCourse: for (j = 0; j < kmax && Continue; j++) // For each data row; { if (j) { mpTrajectory->processStep(pExp->getTimeData()[j]); } else { // set independent data pExp->updateModelWithIndependentData(j); mpTrajectory->processStart(true); if (pExp->getTimeData()[0] != mpModel->getInitialTime()) { mpTrajectory->processStep(pExp->getTimeData()[0]); } } // We check after each simulation step whether the constraints are violated. // Make sure the constraint values are up to date. itRefresh = mExperimentConstraintRefreshes[i].begin(); endRefresh = mExperimentConstraintRefreshes[i].end(); for (; itRefresh != endRefresh; ++itRefresh) (**itRefresh)(); ppConstraint = mExperimentConstraints[i]; ppConstraintEnd = ppConstraint + mExperimentConstraints.numCols(); for (; ppConstraint != ppConstraintEnd; ++ppConstraint) if (*ppConstraint)(*ppConstraint)->calculateConstraintViolation(); if (mStoreResults) mCalculateValue += pExp->sumOfSquaresStore(j, DependentValues); else mCalculateValue += pExp->sumOfSquares(j, Residuals); } break; default: break; } // restore independent data pExp->restoreModelIndependentData(); } } catch (CCopasiException) { // We do not want to clog the message cue. CCopasiMessage::getLastMessage(); mFailedCounter++; mCalculateValue = mInfinity; if (pExp) pExp->restoreModelIndependentData(); } catch (...) { mFailedCounter++; mCalculateValue = mInfinity; if (pExp) pExp->restoreModelIndependentData(); } if (isnan(mCalculateValue)) mCalculateValue = mInfinity; if (mpCallBack) return mpCallBack->progressItem(mhCounter); return true; }