示例#1
0
void Foam::PrintTable<KeyType, DataType>::print
(
    Ostream& os,
    const bool printSum,
    const bool printAverage
) const
{
    HashTable<HashTable<DataType, label>, KeyType> combinedTable;

    List<HashTableData> procData(Pstream::nProcs(), HashTableData());

    procData[Pstream::myProcNo()] = table_;

    Pstream::gatherList(procData);

    if (Pstream::master())
    {
        label largestKeyLength = 6;
        label largestDataLength = 0;

        List<label> largestProcSize(Pstream::nProcs(), 0);

        forAll(procData, procI)
        {
            const HashTableData& procIData = procData[procI];

            for
            (
                typename HashTableData::const_iterator iter = procIData.begin();
                iter != procIData.end();
                ++iter
            )
            {
                if (!combinedTable.found(iter.key()))
                {
                    combinedTable.insert
                    (
                        iter.key(),
                        HashTable<DataType, label>()
                    );
                }

                HashTable<DataType, label>& key = combinedTable[iter.key()];

                key.insert(procI, iter());

                for
                (
                    typename HashTable<DataType, label>
                        ::const_iterator dataIter = key.begin();
                    dataIter != key.end();
                    ++dataIter
                )
                {
                    std::ostringstream buf;
                    buf << dataIter();

                    largestDataLength = max
                    (
                        largestDataLength,
                        label(buf.str().length())
                    );
                }

                std::ostringstream buf;
                buf << iter.key();

                largestKeyLength = max
                (
                    largestKeyLength,
                    label(buf.str().length())
                );
            }
        }

        os.width(largestKeyLength);
        os  << nl << indent << tab << "# " << title_.c_str() << endl;

        os.width(largestKeyLength);
        os  << indent << "# Proc";

        forAll(procData, procI)
        {
            os  << tab;
            os.width(largestDataLength);
            os  << procI;
        }

        if (printSum)
        {
            os  << tab;
            os.width(largestDataLength);
            os  << "Sum";
        }

        if (printAverage)
        {
            os  << tab;
            os.width(largestDataLength);
            os  << "Average";
        }

        os  << endl;

        const List<KeyType>& sortedTable = combinedTable.sortedToc();

        forAll(sortedTable, keyI)
        {
            const HashTable<DataType, label>& procDataList
                = combinedTable[sortedTable[keyI]];

            os.width(largestKeyLength);
            os  << indent << sortedTable[keyI];

            forAll(procDataList, elemI)
            {
                os  << tab;
                os.width(largestDataLength);
                os  << procDataList[elemI];
            }

            if (printSum)
            {
                DataType sum = 0;
                forAll(procDataList, elemI)
                {
                    sum += procDataList[elemI];
                }

                os  << tab;
                os.width(largestDataLength);
                os  << sum;

                if (printAverage)
                {
                    os  << tab;
                    os.width(largestDataLength);
                    os  << sum/Pstream::nProcs();
                }
            }

            os  << endl;
        }
void Foam::UList<double>::writeNonUniform(Ostream& os) const
{
//Pout<< "width:" << os.width() << endl;
//Pout<< "precision:" << os.precision() << endl;
//Pout<< "fmtflags:" << os.flags() << endl;
//Pout<< "boolalpha:" << (os.flags() & ios_base::boolalpha) << endl;
//Pout<< "showbase:" << (os.flags() & ios_base::showbase) << endl;
//Pout<< "showpoint:" << (os.flags() & ios_base::showpoint) << endl;
//Pout<< "showpos:" << (os.flags() & ios_base::showpos) << endl;
//Pout<< "skipws:" << (os.flags() & ios_base::skipws) << endl;
//Pout<< "unitbuf:" << (os.flags() & ios_base::unitbuf) << endl;
//Pout<< "uppercase:" << (os.flags() & ios_base::uppercase) << endl;
//
//Pout<< "dec:" << (os.flags() & ios_base::showbase) << endl;
//Pout<< "hex:" << (os.flags() & ios_base::hex) << endl;
//Pout<< "oct:" << (os.flags() & ios_base::oct) << endl;
//
//Pout<< "fixed:" << (os.flags() & ios_base::fixed) << endl;
//Pout<< "scientific:" << (os.flags() & ios_base::scientific) << endl;
//
//Pout<< "internal:" << (os.flags() & ios_base::internal) << endl;
//Pout<< "left:" << (os.flags() & ios_base::left) << endl;
//Pout<< "right:" << (os.flags() & ios_base::right) << endl;

    const Foam::UList<double>& L = *this;

    // Write size and start delimiter
    os << nl << L.size() << nl << token::BEGIN_LIST;

    if
    (
        (os.width() == 0)
     && !(os.flags() & ios_base::showbase)
     && !(os.flags() & ios_base::showpoint)
     && !(os.flags() & ios_base::showpos)
     && !(os.flags() & ios_base::unitbuf)
     && !(os.flags() & ios_base::uppercase)
     && !(os.flags() & ios_base::hex)
     && !(os.flags() & ios_base::oct)
     && !(os.flags() & ios_base::fixed)
     && !(os.flags() & ios_base::scientific)
     && !(os.flags() & ios_base::internal)
     && !(os.flags() & ios_base::right)
     && isA<OFstream>(os)
    )
    {
        OFstream& ofs = dynamic_cast<OFstream&>(os);

        // Can do optimised
        Pout<< "Optimised" << endl;
        const string singleFmt("\n%-." + Foam::name(ofs.precision()) + "lg");
        string fmt;
        for (int i = 0; i < 8; i++)
        {
            fmt += singleFmt;
        }
        //Pout<< "Format string:" << fmt << endl;
        const char* fmt_ptr = fmt.c_str();

        const int bufLen = 4096;
        char buf[bufLen];
        int free = 0;
        label i = 0;
        for (label outer = 0; outer < L.size()/8; outer++)
        {
            const int nFree = bufLen-free;
            int n = snprintf
            (
                &buf[free],
                nFree,
                fmt_ptr,
                L[i],
                L[i+1],
                L[i+2],
                L[i+3],
                L[i+4],
                L[i+5],
                L[i+6],
                L[i+7]
            );
            if (n >= nFree)
            {
                // Failed. Flush buffer so far
                ofs.stdStream().write(buf, free);
                free = 0;

                // Re-print current entry (assume it succeeds)
                n = snprintf
                (
                    &buf[free],
                    bufLen-free,
                    fmt_ptr,
                    L[i],
                    L[i+1],
                    L[i+2],
                    L[i+3],
                    L[i+4],
                    L[i+5],
                    L[i+6],
                    L[i+7]
                );
            }
            i += 8;
            free += n;
        }

        // Do remainder element by element
        const char* sptr = singleFmt.c_str();
        while (i < L.size())
        {
            const int nFree = bufLen-free;
            int n = snprintf(&buf[free], nFree, sptr, L[i]);
            if (n >= nFree)
            {
                ofs.stdStream().write(buf, free);
                free = 0;

                // Re-print current entry
                n = snprintf(&buf[free], bufLen-free, sptr, L[i]);
            }
            free += n;
            i++;
        }

        if (free > 0)
        {
            ofs.stdStream().write(buf, free);
        }
    }
    else
    {
        // Write contents
        forAll(L, i)
        {
            os << nl << L[i];
        }
    }

    // Write end delimiter
    os << nl << token::END_LIST << nl;
}
示例#3
0
void Foam::functionObjects::writeFile::initStream(Ostream& os) const
{
    os.setf(ios_base::scientific, ios_base::floatfield);
    os.width(charWidth());
}
void Foam::UList<Foam::Vector<double>>::writeNonUniform(Ostream& os) const
{
//Pout<< "width:" << os.width() << endl;
//Pout<< "precision:" << os.precision() << endl;
//Pout<< "fmtflags:" << os.flags() << endl;
//Pout<< "boolalpha:" << (os.flags() & ios_base::boolalpha) << endl;
//Pout<< "showbase:" << (os.flags() & ios_base::showbase) << endl;
//Pout<< "showpoint:" << (os.flags() & ios_base::showpoint) << endl;
//Pout<< "showpos:" << (os.flags() & ios_base::showpos) << endl;
//Pout<< "skipws:" << (os.flags() & ios_base::skipws) << endl;
//Pout<< "unitbuf:" << (os.flags() & ios_base::unitbuf) << endl;
//Pout<< "uppercase:" << (os.flags() & ios_base::uppercase) << endl;
//
//Pout<< "dec:" << (os.flags() & ios_base::showbase) << endl;
//Pout<< "hex:" << (os.flags() & ios_base::hex) << endl;
//Pout<< "oct:" << (os.flags() & ios_base::oct) << endl;
//
//Pout<< "fixed:" << (os.flags() & ios_base::fixed) << endl;
//Pout<< "scientific:" << (os.flags() & ios_base::scientific) << endl;
//
//Pout<< "internal:" << (os.flags() & ios_base::internal) << endl;
//Pout<< "left:" << (os.flags() & ios_base::left) << endl;
//Pout<< "right:" << (os.flags() & ios_base::right) << endl;

    const Foam::UList<Vector<double>>& L = *this;

    // Write size and start delimiter
    os << nl << L.size() << nl << token::BEGIN_LIST;

    if
    (
        (os.width() == 0)
     && !(os.flags() & ios_base::showbase)
     && !(os.flags() & ios_base::showpoint)
     && !(os.flags() & ios_base::showpos)
     && !(os.flags() & ios_base::unitbuf)
     && !(os.flags() & ios_base::uppercase)
     && !(os.flags() & ios_base::hex)
     && !(os.flags() & ios_base::oct)
     && !(os.flags() & ios_base::fixed)
     && !(os.flags() & ios_base::scientific)
     && !(os.flags() & ios_base::internal)
     && !(os.flags() & ios_base::right)
     && isA<OFstream>(os)
    )
    {
        OFstream& ofs = dynamic_cast<OFstream&>(os);

        // Can do optimised
        Pout<< "Optimised" << endl;
        const string sFmt("%-." + Foam::name(ofs.precision()) + "lg");
        string fmt("\n("+sFmt+' '+sFmt+' '+sFmt+')');
        Pout<< "Format string:" << fmt << endl;
        const char* fmt_ptr = fmt.c_str();

        const int bufLen = 4096;
        char buf[bufLen];
        int free = 0;
        forAll(L, i)
        {
            const Vector<double>& p = L[i];

            const int nFree = bufLen-free;
            int n = snprintf(&buf[free], nFree, fmt_ptr, p.x(), p.y(), p.z());
            if (n >= nFree)
            {
                ofs.stdStream().write(buf, free);
                free = 0;

                // Re-print current entry
                n = snprintf(&buf[free], bufLen, fmt_ptr, p.x(), p.y(), p.z());
            }
            free += n;
        }

        if (free > 0)
        {
            ofs.stdStream().write(buf, free);
        }
    }
void Foam::functionObjectFile::initStream(Ostream& os) const
{
    os.setf(ios_base::scientific, ios_base::floatfield);
//    os.precision(IOstream::defaultPrecision());
    os.width(charWidth());
}