<commit_before>/* Begin CVS Header
   $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/model/CCompartment.cpp,v $
   $Revision: 1.47 $
   $Name:  $
   $Author: shoops $ 
   $Date: 2005/04/08 02:53:53 $
   End CVS Header */

// CCompartment
//
// Derived from Gepasi's ccompart.cpp
// (C) <NAME> 1995-2000
//
// Converted for Copasi by <NAME>

#include <iostream>
#include <float.h>

#define  COPASI_TRACE_CONSTRUCTION

#include "copasi.h"
#include "utilities/CReadConfig.h"
#include "utilities/CCopasiVector.h"
#include "utilities/utility.h"
#include "report/CCopasiObjectReference.h"
#include "report/CKeyFactory.h"
#include "CCompartment.h"

CCompartment::CCompartment(const std::string & name,
                           const CCopasiContainer * pParent):
    CCopasiContainer(name, pParent, "Compartment"),
    mKey(GlobalKeys.add("Compartment", this)),
    mInitialVolume(1.0),
    mVolume(1.0),
    mVolumeInv(1.0),
    mMetabolites("Metabolites", this)
{
  initObjects();
  CONSTRUCTOR_TRACE;
}

CCompartment::CCompartment(const CCompartment & src,
                           const CCopasiContainer * pParent):
    CCopasiContainer(src, pParent),
    mKey(GlobalKeys.add("Compartment", this)),
    mInitialVolume(src.mInitialVolume),
    mVolume(src.mVolume),
    mVolumeInv(src.mVolumeInv),
    mMetabolites(src.mMetabolites, this)
{
  CONSTRUCTOR_TRACE;
  initObjects();
  //  for (unsigned C_INT32 i = 0; i < mMetabolites.size(); i++)
  //    mMetabolites[i]->setCompartment(this);
}

CCompartment::~CCompartment()
{
  GlobalKeys.remove(mKey);
  DESTRUCTOR_TRACE;
}

void CCompartment::cleanup() {mMetabolites.cleanup();}

C_INT32 CCompartment::load(CReadConfig & configbuffer)
{
  C_INT32 Fail = 0;
  std::string tmp;
  if ((Fail = configbuffer.getVariable("Compartment", "string",
                                       (void *) & tmp,
                                       CReadConfig::SEARCH)))
    return Fail;
  setObjectName(tmp);

  if ((Fail = configbuffer.getVariable("Volume", "C_FLOAT64",
                                       (void *) & mInitialVolume)))
    return Fail;

  setVolume(mInitialVolume);

  if (configbuffer.getVersion() < "4")
    return Fail;

  C_INT32 MetabolitesNo;

  if ((Fail = configbuffer.getVariable("MetabolitesNo", "C_INT32",
                                       (void *) & MetabolitesNo)))
    return Fail;

  CMetab::setParentCompartment(this);
  mMetabolites.load(configbuffer, MetabolitesNo);

  return Fail;
}

/*C_INT32 CCompartment::save(CWriteConfig & configbuffer)
{
  C_INT32 Fail = 0;
  std::string tmp = getObjectName();
  if ((Fail = configbuffer.setVariable("Compartment", "string",
                                       (void *) & tmp)))
    return Fail;
  if ((Fail = configbuffer.setVariable("Volume", "C_FLOAT64",
                                       (void *) & mVolume)))
    return Fail;
  C_INT32 size = mMetabolites.size();
  if ((Fail = configbuffer.setVariable("MetabolitesNo", "C_INT32",
                                       (void *) & size)))
    return Fail;
  mMetabolites.save(configbuffer);
  return Fail;
}
 
C_INT32 CCompartment::saveOld(CWriteConfig & configbuffer)
{
  C_INT32 Fail = 0;
 
  std::string tmp = getObjectName();
  if ((Fail = configbuffer.setVariable("Compartment", "string",
                                       (void *) & tmp)))
    return Fail;
  if ((Fail = configbuffer.setVariable("Volume", "C_FLOAT64",
                                       (void *) & mVolume)))
    return Fail;
  return Fail;
}*/

/*void CCompartment::saveSBML(std::ofstream &fout)
{
  std::string str;
  FixSName(getObjectName(), str);
  fout << "\t\t\t<compartment name=\"" << str << "\"";
  fout << " volume=\"" << mVolume << "\"/>" << std::endl;
}*/

const std::string & CCompartment::getKey() const {return mKey;}

//const std::string & CCompartment::getName() const {return getObjectName();}

const C_FLOAT64 & CCompartment::getInitialVolume() const
  {return mInitialVolume;}

const C_FLOAT64 & CCompartment::getVolume() const {return mVolume;}

const C_FLOAT64 & CCompartment::getVolumeInv() const {return mVolumeInv;}

CCopasiVectorNS < CMetab > & CCompartment::getMetabolites()
{return mMetabolites;}

const CCopasiVectorNS < CMetab > & CCompartment::getMetabolites() const
  {return mMetabolites;}

bool CCompartment::setName(const std::string & name)
{
  return setObjectName(name);
}

bool CCompartment::setInitialVolume(const C_FLOAT64 & volume)
{
  mInitialVolume = volume;

  /* This has to be moved to the state */
  setVolume(volume);

  C_INT32 i, imax = mMetabolites.size();
  for (i = 0; i < imax; ++i)
    {
      //update particle numbers
      mMetabolites[i]->setInitialConcentration(mMetabolites[i]->getInitialConcentration());
      mMetabolites[i]->setConcentration(mMetabolites[i]->getConcentration());
    }

  return true;
}

bool CCompartment::setVolume(const C_FLOAT64 & volume)
{
  mVolume = volume;

  if (volume != 0.0) mVolumeInv = 1.0 / volume;
  else mVolumeInv = DBL_MAX;

  return true;
}

/* Note: the metabolite stored in mMetabolites has definetly mpCompartment set.
   In the case the compartment is part of a model also mpModel is set. */
bool CCompartment::createMetabolite(const CMetab & metabolite)
{<commit_msg>Fixed setInitialVolume to update particle number instead of concentration, which we had agreed to be constant.<commit_after>/* Begin CVS Header
   $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/model/CCompartment.cpp,v $
   $Revision: 1.48 $
   $Name:  $
   $Author: shoops $ 
   $Date: 2005/04/11 17:53:23 $
   End CVS Header */

// CCompartment
//
// Derived from Gepasi's ccompart.cpp
// (C) <NAME> 1995-2000
//
// Converted for Copasi by <NAME>

#include <iostream>
#include <float.h>

#define  COPASI_TRACE_CONSTRUCTION

#include "copasi.h"
#include "utilities/CReadConfig.h"
#include "utilities/CCopasiVector.h"
#include "utilities/utility.h"
#include "report/CCopasiObjectReference.h"
#include "report/CKeyFactory.h"
#include "CCompartment.h"

CCompartment::CCompartment(const std::string & name,
                           const CCopasiContainer * pParent):
    CCopasiContainer(name, pParent, "Compartment"),
    mKey(GlobalKeys.add("Compartment", this)),
    mInitialVolume(1.0),
    mVolume(1.0),
    mVolumeInv(1.0),
    mMetabolites("Metabolites", this)
{
  initObjects();
  CONSTRUCTOR_TRACE;
}

CCompartment::CCompartment(const CCompartment & src,
                           const CCopasiContainer * pParent):
    CCopasiContainer(src, pParent),
    mKey(GlobalKeys.add("Compartment", this)),
    mInitialVolume(src.mInitialVolume),
    mVolume(src.mVolume),
    mVolumeInv(src.mVolumeInv),
    mMetabolites(src.mMetabolites, this)
{
  CONSTRUCTOR_TRACE;
  initObjects();
  //  for (unsigned C_INT32 i = 0; i < mMetabolites.size(); i++)
  //    mMetabolites[i]->setCompartment(this);
}

CCompartment::~CCompartment()
{
  GlobalKeys.remove(mKey);
  DESTRUCTOR_TRACE;
}

void CCompartment::cleanup() {mMetabolites.cleanup();}

C_INT32 CCompartment::load(CReadConfig & configbuffer)
{
  C_INT32 Fail = 0;
  std::string tmp;
  if ((Fail = configbuffer.getVariable("Compartment", "string",
                                       (void *) & tmp,
                                       CReadConfig::SEARCH)))
    return Fail;
  setObjectName(tmp);

  if ((Fail = configbuffer.getVariable("Volume", "C_FLOAT64",
                                       (void *) & mInitialVolume)))
    return Fail;

  setVolume(mInitialVolume);

  if (configbuffer.getVersion() < "4")
    return Fail;

  C_INT32 MetabolitesNo;

  if ((Fail = configbuffer.getVariable("MetabolitesNo", "C_INT32",
                                       (void *) & MetabolitesNo)))
    return Fail;

  CMetab::setParentCompartment(this);
  mMetabolites.load(configbuffer, MetabolitesNo);

  return Fail;
}

/*C_INT32 CCompartment::save(CWriteConfig & configbuffer)
{
  C_INT32 Fail = 0;
  std::string tmp = getObjectName();
  if ((Fail = configbuffer.setVariable("Compartment", "string",
                                       (void *) & tmp)))
    return Fail;
  if ((Fail = configbuffer.setVariable("Volume", "C_FLOAT64",
                                       (void *) & mVolume)))
    return Fail;
  C_INT32 size = mMetabolites.size();
  if ((Fail = configbuffer.setVariable("MetabolitesNo", "C_INT32",
                                       (void *) & size)))
    return Fail;
  mMetabolites.save(configbuffer);
  return Fail;
}
 
C_INT32 CCompartment::saveOld(CWriteConfig & configbuffer)
{
  C_INT32 Fail = 0;
 
  std::string tmp = getObjectName();
  if ((Fail = configbuffer.setVariable("Compartment", "string",
                                       (void *) & tmp)))
    return Fail;
  if ((Fail = configbuffer.setVariable("Volume", "C_FLOAT64",
                                       (void *) & mVolume)))
    return Fail;
  return Fail;
}*/

/*void CCompartment::saveSBML(std::ofstream &fout)
{
  std::string str;
  FixSName(getObjectName(), str);
  fout << "\t\t\t<compartment name=\"" << str << "\"";
  fout << " volume=\"" << mVolume << "\"/>" << std::endl;
}*/

const std::string & CCompartment::getKey() const {return mKey;}

//const std::string & CCompartment::getName() const {return getObjectName();}

const C_FLOAT64 & CCompartment::getInitialVolume() const
  {return mInitialVolume;}

const C_FLOAT64 & CCompartment::getVolume() const {return mVolume;}

const C_FLOAT64 & CCompartment::getVolumeInv() const {return mVolumeInv;}

CCopasiVectorNS < CMetab > & CCompartment::getMetabolites()
{return mMetabolites;}

const CCopasiVectorNS < CMetab > & CCompartment::getMetabolites() const
  {return mMetabolites;}

bool CCompartment::setName(const std::string & name)
{
  return setObjectName(name);
}

bool CCompartment::setInitialVolume(const C_FLOAT64 & volume)
{
  C_FLOAT64 Factor = volume / mInitialVolume;
  mInitialVolume = volume;

  /* This has to be moved to the state */
  setVolume(volume);

  C_INT32 i, imax = mMetabolites.size();
  for (i = 0; i < imax; ++i)
    {
      //update particle numbers
      mMetabolites[i]->setInitialNumber(mMetabolites[i]->getInitialNumber() * Factor);
      mMetabolites[i]->setNumber(mMetabolites[i]->getNumber() * Factor);
    }

  return true;
}

bool CCompartment::setVolume(const C_FLOAT64 & volume)
{
  mVolume = volume;

  if (volume != 0.0) mVolumeInv = 1.0 / volume;
  else mVolumeInv = DBL_MAX;

  return true;
}

/* Note: the metabolite stored in mMetabolites has definetly mpCompartment set.
   In the case the compartment is part of a model also mpModel is set. */
bool CCompartment::createMetabolite(const CMetab & metabolite)
{
{'fraction_non_alphanumeric': 0.1348050728041334, 'fraction_numerical': 0.015406294034758102, 'mean_word_length': 3.2112341772151898, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4013289', 'n_tokens_mistral': 3681, 'n_tokens_neox': 3544, 'n_words': 727}