Пример #1
0
Exec::RetType Exec_CrdAction::DoCrdAction(CpptrajState& State, ArgList& actionargs,
                                          DataSet_Coords* CRD, Action* act,
                                          TrajFrameCounter const& frameCount) const
{
  Timer total_time;
  total_time.Start();
# ifdef MPI
  ActionInit state(State.DSL(), State.DFL(), trajComm_);
# else
  ActionInit state(State.DSL(), State.DFL());
# endif
  if ( act->Init( actionargs, state, State.Debug() ) != Action::OK )
    return CpptrajState::ERR;
  actionargs.CheckForMoreArgs();
  // Set up frame and parm for COORDS.
  ActionSetup originalSetup( CRD->TopPtr(), CRD->CoordsInfo(), CRD->Size() );
  Frame originalFrame = CRD->AllocateFrame();
  ActionFrame frm( &originalFrame, 0 );
  // Set up for this topology 
  Action::RetType setup_ret = act->Setup( originalSetup );
  if ( setup_ret == Action::ERR || setup_ret == Action::SKIP )
    return CpptrajState::ERR;
  // Loop over all frames in COORDS.
  ProgressBar progress( frameCount.TotalReadFrames() );
  int set = 0;
  for (int frame = frameCount.Start(); frame < frameCount.Stop();
           frame += frameCount.Offset(), ++set)
  { 
    progress.Update( set );
    CRD->GetFrame( frame, originalFrame );
    frm.SetTrajoutNum( set );
    Action::RetType ret = act->DoAction( set, frm );
    if (ret == Action::ERR) {
      mprinterr("Error: crdaction: Frame %i, set %i\n", frame + 1, set + 1);
      break;
    }
    // Check if frame was modified. If so, update COORDS.
    if ( ret == Action::MODIFY_COORDS )
      CRD->SetCRD( frame, frm.Frm() );
  }
# ifdef MPI
  act->SyncAction();
# endif
  // Check if parm was modified. If so, update COORDS.
  if ( setup_ret == Action::MODIFY_TOPOLOGY ) {
    mprintf("Info: crdaction: Parm for %s was modified by action %s\n",
            CRD->legend(), actionargs.Command());
    CRD->CoordsSetup( originalSetup.Top(), originalSetup.CoordInfo() );
  } 
  act->Print();
  State.MasterDataFileWrite();
  total_time.Stop();
  mprintf("TIME: Total action execution time: %.4f seconds.\n", total_time.Total());
  return CpptrajState::OK;
}
Пример #2
0
Exec::RetType Exec_Precision::Execute(CpptrajState& State, ArgList& argIn) {
  // Next string is DataSet(s)/DataFile that command pertains to.
  std::string name1 = argIn.GetStringNext();
  if (name1.empty()) {
    mprinterr("Error: No filename/setname given.\n");
    return CpptrajState::ERR;
  }
  // This will break if dataset name starts with a digit...
  int width = argIn.getNextInteger(12);
  if (width < 1) {
    mprintf("Error: Cannot set width < 1 (%i).\n", width);
    return CpptrajState::ERR;
  }
  int precision = argIn.getNextInteger(4);
  if (precision < 0) precision = 0;
  DataFile* df = State.DFL().GetDataFile(name1);
  if (df != 0) {
    mprintf("\tSetting precision for all sets in %s to %i.%i\n", df->DataFilename().base(),
            width, precision);
    df->SetDataFilePrecision(width, precision);
  } else {
    State.DSL().SetPrecisionOfDataSets( name1, width, precision );
  }
  return CpptrajState::OK;
}
Пример #3
0
Exec::RetType Exec_DataFilter::Execute(CpptrajState& State, ArgList& argIn) {
  Action_FilterByData filterAction;
  ActionInit state(State.DSL(), State.DFL());
  if (filterAction.Init(argIn, state, State.Debug()) != Action::OK)
    return CpptrajState::ERR;
  size_t nframes = filterAction.DetermineFrames();
  if (nframes < 1) {
    mprinterr("Error: No data to filter. All sets must contain some data.\n");
    return CpptrajState::ERR;
  }
  ProgressBar progress( nframes );
  ActionFrame frm;
  for (size_t frame = 0; frame != nframes; frame++) {
    progress.Update( frame );
    filterAction.DoAction(frame, frm); // Filter does not need frame.
  }
  // Trigger master datafile write just in case
  State.MasterDataFileWrite();
  return CpptrajState::OK;
}
Пример #4
0
Exec::RetType Exec_ReadData::Execute(CpptrajState& State, ArgList& argIn) {
  DataFile dataIn;
  dataIn.SetDebug( State.DFL().Debug() );
  std::string filenameIn = argIn.GetStringNext();
  File::NameArray fnames = File::ExpandToFilenames( filenameIn );
  if (fnames.empty()) {
    mprinterr("Error: '%s' matches no files.\n", filenameIn.c_str());
    return CpptrajState::ERR;
  }
  int err = 0;
  int idx = -1;
  bool useIndex = argIn.hasKey("separate");
  for (File::NameArray::const_iterator fn = fnames.begin(); fn != fnames.end(); ++fn) {
    if (useIndex) idx++;
    if (dataIn.ReadDataIn( *fn, argIn, State.DSL(), idx, fnames.size() )!=0) {
      mprinterr("Error: Could not read data file '%s'.\n", fn->full());
      err++;
    }
  }
  if (err > 0) return CpptrajState::ERR;
  return CpptrajState::OK;
}
Пример #5
0
Exec::RetType Exec_CrdAction::DoCrdAction(CpptrajState& State, ArgList& actionargs,
                                          DataSet_Coords* CRD, Action* act,
                                          TrajFrameCounter const& frameCount) const
{
  Timer total_time;
  total_time.Start();
# ifdef MPI
  ActionInit state(State.DSL(), State.DFL(), trajComm_);
# else
  ActionInit state(State.DSL(), State.DFL());
# endif
  if ( act->Init( actionargs, state, State.Debug() ) != Action::OK )
    return CpptrajState::ERR;
  actionargs.CheckForMoreArgs();
  // Set up frame and parm for COORDS.
  ActionSetup originalSetup( CRD->TopPtr(), CRD->CoordsInfo(), CRD->Size() );
  Frame originalFrame = CRD->AllocateFrame();
  // Set up for this topology 
  Action::RetType setup_ret = act->Setup( originalSetup );
  if ( setup_ret == Action::ERR || setup_ret == Action::SKIP )
    return CpptrajState::ERR;
  // If the topology was modified, we will need a new COORDS set.
  DataSet_Coords* crdOut = 0;
  if ( setup_ret == Action::MODIFY_TOPOLOGY ) {
    // This will not work for a TRJ set.
    switch ( CRD->Type() ) {
      case DataSet::TRAJ      : mprinterr("Error: Cannot modify TRAJ data sets.\n"); break;
      case DataSet::COORDS    : crdOut = (DataSet_Coords*)new DataSet_Coords_CRD(); break;
      case DataSet::REF_FRAME : crdOut = (DataSet_Coords*)new DataSet_Coords_REF(); break;
      default: crdOut = 0; // SANITY
    }
    if (crdOut == 0) return CpptrajState::ERR;
    mprintf("Info: crdaction: COORDS set '%s' will be modified by action '%s'\n",
            CRD->legend(), actionargs.Command());
    if (frameCount.TotalReadFrames() != (int)CRD->Size())
      mprintf("Info: crdaction: Previous size= %zu, new size is %i\n",
              CRD->Size(), frameCount.TotalReadFrames());
    // Set up set, copy original metadata
    crdOut->SetMeta( CRD->Meta() );
    if (crdOut->CoordsSetup( originalSetup.Top(), originalSetup.CoordInfo() ))
      return CpptrajState::ERR;
    DataSet::SizeArray mfArray(1, frameCount.TotalReadFrames());
    if (crdOut->Allocate( mfArray )) return CpptrajState::ERR;
  }
    
  // Loop over all frames in COORDS.
  ProgressBar* progress = 0;
  if (State.ShowProgress())
    progress = new ProgressBar( frameCount.TotalReadFrames() );
  int set = 0;
  for (int frame = frameCount.Start(); frame < frameCount.Stop();
           frame += frameCount.Offset(), ++set)
  {
    // Since Frame can be modified by actions, save original and use currentFrame
    ActionFrame frm( &originalFrame, set );
    if (progress != 0) progress->Update( set );
    CRD->GetFrame( frame, originalFrame );
    Action::RetType ret = act->DoAction( set, frm );
    if (ret == Action::ERR) {
      mprinterr("Error: crdaction: Frame %i, set %i\n", frame + 1, set + 1);
      break;
    }
    // Check if frame was modified. If so, update COORDS.
    if ( ret == Action::MODIFY_COORDS ) {
      if (crdOut != 0)
        crdOut->AddFrame( frm.Frm() );
      else
        CRD->SetCRD( frame, frm.Frm() );
    }
  } 
  if (progress != 0) delete progress;
# ifdef MPI
  act->SyncAction();
# endif
  // If topology was modified, replace old set with new.
  if ( setup_ret == Action::MODIFY_TOPOLOGY ) {
    mprintf("Info: crdaction: Topology for '%s' was modified by action '%s'\n",
            CRD->legend(), actionargs.Command());
    State.DSL().RemoveSet( CRD );
    State.DSL().AddSet( crdOut );
  } 
  act->Print();
  State.MasterDataFileWrite();
  total_time.Stop();
  mprintf("TIME: Total action execution time: %.4f seconds.\n", total_time.Total());
  return CpptrajState::OK;
}
Пример #6
0
// Exec_PermuteDihedrals::Execute()
Exec::RetType Exec_PermuteDihedrals::Execute(CpptrajState& State, ArgList& argIn) {
  debug_ = State.Debug();
  mode_ = INTERVAL;
  // Get Keywords - first determine mode
  if (argIn.hasKey("random"))
    mode_ = RANDOM;
  else if (argIn.hasKey("interval"))
    mode_ = INTERVAL;
  // Get input COORDS set
  std::string setname = argIn.GetStringKey("crdset");
  if (setname.empty()) {
    mprinterr("Error: Specify COORDS dataset name with 'crdset'.\n");
    return CpptrajState::ERR;
  }
  DataSet_Coords* CRD = (DataSet_Coords*)State.DSL().FindCoordsSet( setname );
  if (CRD == 0) {
    mprinterr("Error: Could not find COORDS set '%s'\n", setname.c_str());
    return CpptrajState::ERR;
  }
  mprintf("    PERMUTEDIHEDRALS: Using COORDS '%s'\n", CRD->legend());

  // Get residue range
  Range resRange;
  resRange.SetRange(argIn.GetStringKey("resrange"));
  if (!resRange.Empty())
    resRange.ShiftBy(-1); // User res args start from 1
  mprintf("\tPermutating dihedrals in");
  if (resRange.Empty())
    mprintf(" all solute residues.\n");
  else
    mprintf(" residue range [%s]\n", resRange.RangeArg());

  // Determine which angles to search for
  DihedralSearch dihSearch;
  dihSearch.SearchForArgs(argIn);
  // If nothing is enabled, enable all 
  dihSearch.SearchForAll();
  mprintf("\tSearching for types:");
  dihSearch.PrintTypes();
  mprintf("\n");

  // Setup output trajectory
  outframe_ = 0; 
  std::string outfilename = argIn.GetStringKey("outtraj");
  if (!outfilename.empty()) {
    mprintf("\tCoordinates output to '%s'\n", outfilename.c_str());
    Topology* outtop = State.DSL().GetTopology( argIn );
    if (outtop == 0) {
      mprinterr("Error: No topology for output traj.\n");
      return CpptrajState::ERR;
    }
    // Setup output trajectory FIXME: Correct frames for # of rotations
    if (outtraj_.PrepareTrajWrite(outfilename, argIn, CRD->TopPtr(), CRD->CoordsInfo(),
                                  CRD->Size(), TrajectoryFile::UNKNOWN_TRAJ))
      return CpptrajState::ERR;
  }

  // Setup output coords
  outfilename = argIn.GetStringKey("crdout");
  if (!outfilename.empty()) {
    mprintf("\tCoordinates saved to set '%s'\n", outfilename.c_str());
    crdout_ = (DataSet_Coords_CRD*)State.DSL().AddSet(DataSet::COORDS, outfilename);
    if (crdout_ == 0) return CpptrajState::ERR;
    crdout_->CoordsSetup( CRD->Top(), CRD->CoordsInfo() );
  }

  // Get specific mode options.
  double interval_in_deg = 60.0;
  if ( mode_ == INTERVAL ) {
    interval_in_deg = argIn.getNextDouble(60.0);
    mprintf("\tDihedrals will be rotated at intervals of %.2f degrees.\n", interval_in_deg);
  } else if (mode_ == RANDOM) {
    check_for_clashes_ = argIn.hasKey("check");
    checkAllResidues_ = argIn.hasKey("checkallresidues");
    cutoff_ = argIn.getKeyDouble("cutoff",0.8);
    rescutoff_ = argIn.getKeyDouble("rescutoff",10.0);
    backtrack_ = argIn.getKeyInt("backtrack",4);
    increment_ = argIn.getKeyInt("increment",1);
    max_factor_ = argIn.getKeyInt("maxfactor",2);
    int iseed = argIn.getKeyInt("rseed",-1);
    // Output file for # of problems
    DataFile* problemFile = State.DFL().AddDataFile(argIn.GetStringKey("out"), argIn);
    // Dataset to store number of problems
    number_of_problems_ = State.DSL().AddSet(DataSet::INTEGER, argIn.GetStringNext(),"Nprob");
    if (number_of_problems_==0) return CpptrajState::ERR;
   // Add dataset to data file list
    if (problemFile != 0) problemFile->AddDataSet(number_of_problems_);
    // Check validity of args
    if (cutoff_ < Constants::SMALL) {
      mprinterr("Error: cutoff too small.\n");
      return CpptrajState::ERR;
    }
    if (rescutoff_ < Constants::SMALL) {
      mprinterr("Error: rescutoff too small.\n");
      return CpptrajState::ERR;
    }
    if (backtrack_ < 0) {
      mprinterr("Error: backtrack value must be >= 0\n");
      return CpptrajState::ERR;
    }
    if ( increment_<1 || (360 % increment_)!=0 ) {
      mprinterr("Error: increment must be a factor of 360.\n");
      return CpptrajState::ERR;
    }
    // Calculate max increment
    max_increment_ = 360 / increment_;
    // Seed random number gen
    RN_.rn_set( iseed );
    // Print info
    mprintf("\tDihedrals will be rotated to random values.\n");
    if (iseed==-1)
      mprintf("\tRandom number generator will be seeded using time.\n");
    else
      mprintf("\tRandom number generator will be seeded using %i\n",iseed);
    if (check_for_clashes_) {
      mprintf("\tWill attempt to recover from bad steric clashes.\n");
      if (checkAllResidues_)
        mprintf("\tAll residues will be checked.\n");
      else
        mprintf("\tResidues up to the currenly rotating dihedral will be checked.\n");
      mprintf("\tAtom cutoff %.2f, residue cutoff %.2f, backtrack = %i\n",
              cutoff_, rescutoff_, backtrack_);
      mprintf("\tWhen clashes occur dihedral will be incremented by %i\n",increment_);
      mprintf("\tMax # attempted rotations = %i times number dihedrals.\n",
              max_factor_);
    }
    // Square cutoffs to compare to dist^2 instead of dist
    cutoff_ *= cutoff_;
    rescutoff_ *= rescutoff_;
    // Increment backtrack by 1 since we need to skip over current res
    ++backtrack_;
    // Initialize CheckStructure
    if (checkStructure_.SetOptions( false, false, false, State.Debug(), "*", "", 0.8, 1.15, 4.0 )) {
      mprinterr("Error: Could not set up structure check.\n");
      return CpptrajState::ERR;
    }
    // Set up CheckStructure for this parm (false = nobondcheck)
    if (checkStructure_.Setup(CRD->Top(), CRD->CoordsInfo().TrajBox()))
      return CpptrajState::ERR;
  }

  // Determine from selected mask atoms which dihedrals will be rotated.
  PermuteDihedralsType dst;
  // If range is empty (i.e. no resrange arg given) look through all 
  // solute residues.
  Range actualRange;
  if (resRange.Empty())
    actualRange = CRD->Top().SoluteResidues();
  else 
    actualRange = resRange;
  // Search for dihedrals
  if (dihSearch.FindDihedrals(CRD->Top(), actualRange))
    return CpptrajState::ERR;
  // For each found dihedral, set up mask of atoms that will move upon 
  // rotation. Also set up mask of atoms in this residue that will not
  // move, including atom2.
  if (debug_>0)
    mprintf("DEBUG: Dihedrals:\n");
  for (DihedralSearch::mask_it dih = dihSearch.begin();
                               dih != dihSearch.end(); ++dih)
  {
    dst.checkAtoms.clear();
    // Set mask of atoms that will move during dihedral rotation.
    dst.Rmask = DihedralSearch::MovingAtoms(CRD->Top(), dih->A1(), dih->A2());
    // If randomly rotating angles, check for atoms that are in the same
    // residue as A1 but will not move. They need to be checked for clashes
    // since further rotations will not help them.
    if (mode_ == RANDOM && check_for_clashes_) {
      CharMask cMask( dst.Rmask.ConvertToCharMask(), dst.Rmask.Nselected() );
      int a1res = CRD->Top()[dih->A1()].ResNum();
      for (int maskatom = CRD->Top().Res(a1res).FirstAtom();
               maskatom < CRD->Top().Res(a1res).LastAtom(); ++maskatom)
        if (!cMask.AtomInCharMask(maskatom))
          dst.checkAtoms.push_back( maskatom );
      dst.checkAtoms.push_back(dih->A1()); // TODO: Does this need to be added first?
      // Since only the second atom and atoms it is bonded to move during 
      // rotation, base the check on the residue of the second atom.
      dst.resnum = a1res;
    }
    dst.atom0 = dih->A0(); // FIXME: This duplicates info
    dst.atom1 = dih->A1();
    dst.atom2 = dih->A2();
    dst.atom3 = dih->A3();
    BB_dihedrals_.push_back(dst);
    // DEBUG: List dihedral info.
    if (debug_ > 0) {
      mprintf("\t%s-%s-%s-%s\n", 
              CRD->Top().TruncResAtomName(dih->A0()).c_str(),
              CRD->Top().TruncResAtomName(dih->A1()).c_str(),
              CRD->Top().TruncResAtomName(dih->A2()).c_str(),
              CRD->Top().TruncResAtomName(dih->A3()).c_str() );
      if (debug_ > 1 && mode_ == RANDOM && check_for_clashes_) {
        mprintf("\t\tCheckAtoms=");
        for (std::vector<int>::const_iterator ca = dst.checkAtoms.begin();
                                              ca != dst.checkAtoms.end(); ++ca)
          mprintf(" %i", *ca + 1);
        mprintf("\n");
      }
      if (debug_ > 2) {
        mprintf("\t\t");
        dst.Rmask.PrintMaskAtoms("Rmask:");
      }
    }
  }

  // Set up simple structure check. First step is coarse; check distances 
  // between a certain atom in each residue (first, COM, CA, some other atom?)
  // to see if residues are in each others neighborhood. Second step is to 
  // check the atoms in each close residue.
  if (check_for_clashes_) {
    ResidueCheckType rct;
    int res = 0;
    for (Topology::res_iterator residue = CRD->Top().ResStart();
                                residue != CRD->Top().ResEnd(); ++residue)
    {
      rct.resnum = res++;
      rct.start = residue->FirstAtom();
      rct.stop = residue->LastAtom();
      rct.checkatom = rct.start;
      ResCheck_.push_back(rct);
    }
  }

  // Perform dihedral permute
  Frame currentFrame = CRD->AllocateFrame();
  for (unsigned int set = 0; set != CRD->Size(); set++)
  {
    CRD->GetFrame(set, currentFrame);
    int n_problems = 0;
    switch (mode_) {
      case RANDOM:
        RandomizeAngles(currentFrame, CRD->Top());
        // Check the resulting structure
        n_problems = checkStructure_.CheckOverlaps( currentFrame );
        //mprintf("%i\tResulting structure has %i problems.\n",frameNum,n_problems);
        number_of_problems_->Add(set, &n_problems);
        if (outtraj_.IsInitialized()) outtraj_.WriteSingle(outframe_++, currentFrame);
        if (crdout_ != 0) crdout_->AddFrame( currentFrame );
        break;
      case INTERVAL: IntervalAngles(currentFrame, CRD->Top(), interval_in_deg); break;
    }
  }
  if (outtraj_.IsInitialized()) outtraj_.EndTraj();
  return CpptrajState::OK;
}
Пример #7
0
// Exec_ParallelAnalysis::Execute()
Exec::RetType Exec_ParallelAnalysis::Execute(CpptrajState& State, ArgList& argIn)
{
  Timer t_total;
  t_total.Start();
  Timer t_sync;
  bool syncToMaster = argIn.hasKey("sync");
  std::vector<unsigned int> setSizesBefore;
  if (syncToMaster) {
    t_sync.Start();
    setSizesBefore.reserve( State.DSL().size() );
    for (DataSetList::const_iterator it = State.DSL().begin(); it != State.DSL().end(); ++it)
      setSizesBefore.push_back( (*it)->Size() );
    t_sync.Stop();
  }
  // DEBUG - Have each thread report what analyses it knows about and what
  // data sets it has.
/*
  for (int rank = 0; rank < Parallel::World().Size(); rank++) {
    if (rank == Parallel::World().Rank()) {
      printf("Rank %i, %u analyses, %zu data sets:\n", rank, State.Analyses().size(),
             State.DSL().size());
      for (DataSetList::const_iterator it = State.DSL().begin(); it != State.DSL().end(); ++it)
        printf("\t'%s' (%zu)\n", (*it)->Meta().PrintName().c_str(), (*it)->Size());
    }
    Parallel::World().Barrier();
  }
  Parallel::World().Barrier();
*/
  mprintf("    PARALLELANALYSIS: Will attempt to run current Analyses in parallel.\n\n"
          "*** THIS COMMAND IS STILL EXPERIMENTAL! ***\n\n");
  if (syncToMaster)
    mprintf("\tResulting data sets will be synced back to master.\n");
  // Naively divide up all analyses among threads.
  int my_start, my_stop;
  int nelts = Parallel::World().DivideAmongProcesses( my_start, my_stop, State.Analyses().size() );
  rprintf("Dividing %zu analyses among %i threads: %i to %i (%i)\n",
          State.Analyses().size(), Parallel::World().Size(), my_start, my_stop, nelts);

  // Each thread runs the analyses they are responsible for.
  int nerr = 0;
  for (int na = my_start; na != my_stop; na++) {
    // TODO check setup status
    if (State.Analyses().Ana(na).Analyze() != Analysis::OK) {
      rprinterr("Error: Analysis failed: '%s'\n", State.Analyses().Args(na).ArgLine());
      nerr++;
    }
  }
  // This error check serves as a barrier
  if (Parallel::World().CheckError( nerr )) return CpptrajState::ERR;
  State.DFL().AllThreads_WriteAllDF();
  State.Analyses().Clear();
  if (syncToMaster) {
    t_sync.Start();
    // Check which sizes have changed.
    if (setSizesBefore.size() != State.DSL().size()) {
      mprintf("Warning: Number of sets have changed. Not attempting to sync sets to master.\n");
    } else {
      for (unsigned int idx = 0; idx != State.DSL().size(); idx++) {
        int setHasChanged = 0;
        if (!Parallel::World().Master()) {
          if (setSizesBefore[idx] != State.DSL()[idx]->Size()) {
            if (State.Debug() > 0)
              rprintf("Set '%s' size has changed from %u to %zu\n",
                      State.DSL()[idx]->legend(), setSizesBefore[idx], State.DSL()[idx]->Size());
            setHasChanged = 1;
          }
        }
        int totalChanged;
        Parallel::World().AllReduce(&totalChanged, &setHasChanged, 1, MPI_INT, MPI_SUM);
        if (totalChanged > 0) {
          if (totalChanged == 1) {
            int sourceRank = 0;
            if (setHasChanged == 1)
              setHasChanged = Parallel::World().Rank();
            Parallel::World().ReduceMaster(&sourceRank, &setHasChanged, 1, MPI_INT, MPI_SUM);
            if (State.Debug() > 0)
              mprintf("DEBUG: Need to sync '%s' from %i\n", State.DSL()[idx]->legend(), sourceRank);
            if (Parallel::World().Master())
              State.DSL()[idx]->RecvSet( sourceRank, Parallel::World() );
            else if (setHasChanged == Parallel::World().Rank())
              State.DSL()[idx]->SendSet( 0,          Parallel::World() );
          } else
            mprintf("Warning: '%s' exists on multiple threads. Not syncing.\n",
                    State.DSL()[idx]->legend());
        }
      }
    }
    t_sync.Stop();
  }
  t_total.Stop();
  if (syncToMaster)
    t_sync.WriteTiming(2, "Sync:", t_total.Total());
  t_total.WriteTiming(1, "Total:");
  return CpptrajState::OK;
}
Пример #8
0
/** Syntax: dataset invert <set arg0> ... name <new name> */
Exec::RetType Exec_DataSetCmd::InvertSets(CpptrajState& State, ArgList& argIn) {
  DataSetList& DSL = State.DSL();
  // Get keywords
  DataSet* inputNames = 0;
  std::string dsname = argIn.GetStringKey("legendset");
  if (!dsname.empty()) {
    inputNames = DSL.GetDataSet( dsname );
    if (inputNames == 0) {
      mprinterr("Error: Name set '%s' not found.\n", dsname.c_str());
      return CpptrajState::ERR;
    }
    if (inputNames->Type() != DataSet::STRING) {
      mprinterr("Error: Set '%s' does not contain strings.\n", inputNames->legend());
      return CpptrajState::ERR;
    }
    mprintf("\tUsing names from set '%s' as legends for inverted sets.\n", inputNames->legend());
  }
  dsname = argIn.GetStringKey("name");
  if (dsname.empty()) {
    mprinterr("Error: 'invert' requires that 'name <new set name>' be specified.\n");
    return CpptrajState::ERR;
  }
  mprintf("\tNew sets will be named '%s'\n", dsname.c_str());
  DataFile* outfile = State.DFL().AddDataFile( argIn.GetStringKey("out"), argIn );
  if (outfile != 0)
    mprintf("\tNew sets will be output to '%s'\n", outfile->DataFilename().full());
  // TODO determine type some other way
  DataSet::DataType outtype = DataSet::DOUBLE;
  // Get input DataSets
  std::vector<DataSet_1D*> input_sets; 
  std::string dsarg = argIn.GetStringNext();
  while (!dsarg.empty()) {
    DataSetList sets = DSL.GetMultipleSets( dsarg );
    for (DataSetList::const_iterator ds = sets.begin(); ds != sets.end(); ++ds)
    {
      if ( (*ds)->Group() != DataSet::SCALAR_1D ) {
        mprintf("Warning: '%s': Inversion only supported for 1D scalar data sets.\n",
                (*ds)->legend());
      } else {
        if (!input_sets.empty()) {
          if ( (*ds)->Size() != input_sets.back()->Size() ) {
            mprinterr("Error: Set '%s' has different size (%zu) than previous set (%zu)\n",
                      (*ds)->legend(), (*ds)->Size(), input_sets.back()->Size());
            return CpptrajState::ERR;
          }
        }
        input_sets.push_back( (DataSet_1D*)*ds );
      }
    }
    dsarg = argIn.GetStringNext();
  }
  if (input_sets.empty()) {
    mprinterr("Error: No sets selected.\n");
    return CpptrajState::ERR;
  }
  if (inputNames != 0 && inputNames->Size() != input_sets.front()->Size()) {
    mprinterr("Error: Name set '%s' size (%zu) differs from # data points (%zu).\n",
              inputNames->legend(), inputNames->Size(), input_sets.front()->Size());
    return CpptrajState::ERR;
  }
  mprintf("\t%zu input sets; creating %zu output sets.\n",
          input_sets.size(), input_sets.front()->Size());
  // Need an output data set for each point in input sets
  std::vector<DataSet*> output_sets;
  int column = 1;
  for (int idx = 0; idx != (int)input_sets[0]->Size(); idx++, column++) {
    DataSet* ds = 0;
    ds = DSL.AddSet(outtype, MetaData(dsname, column));
    if (ds == 0) return CpptrajState::ERR;
    if (inputNames != 0)
      ds->SetLegend( (*((DataSet_string*)inputNames))[idx] );
    output_sets.push_back( ds );
    if (outfile != 0) outfile->AddDataSet( ds );
  }
  // Create a data set containing names of each input data set
  DataSet* nameset = DSL.AddSet(DataSet::STRING, MetaData(dsname, column));
  if (nameset == 0) return CpptrajState::ERR;
  if (inputNames != 0)
    nameset->SetLegend("Names");
  if (outfile != 0) outfile->AddDataSet( nameset );
  // Populate output data sets
  for (int jdx = 0; jdx != (int)input_sets.size(); jdx++)
  {
    DataSet_1D const& INP = static_cast<DataSet_1D const&>( *(input_sets[jdx]) );
    nameset->Add( jdx, INP.legend() );
    for (unsigned int idx = 0; idx != INP.Size(); idx++)
    {
      double dval = INP.Dval( idx );
      output_sets[idx]->Add( jdx, &dval );
    }
  }

  return CpptrajState::OK;
}