avtTraceHistoryFilter.C 13.8 KB
Newer Older
1 2
/*****************************************************************************
*
whitlocb's avatar
whitlocb committed
3
* Copyright (c) 2000 - 2014, Lawrence Livermore National Security, LLC
4
* Produced at the Lawrence Livermore National Laboratory
5
* LLNL-CODE-442911
6 7
* All rights reserved.
*
8
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
9 10 11 12 13 14 15 16 17 18
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
19 20 21
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
22 23 24 25 26
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
27 28 29
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

// ************************************************************************* //
//  File: avtTraceHistoryFilter.C
// ************************************************************************* //

#include <avtTraceHistoryFilter.h>

#include <vtkCellType.h>
#include <vtkUnstructuredGrid.h>

#include <vtkVisItUtility.h>

#include <snprintf.h>
#include <DBOptionsAttributes.h>
#include <DebugStream.h>
#include <DatabasePluginManager.h>
#include <DatabasePluginInfo.h>
#include <Expression.h>
#include <ParsingExprList.h>

#include <avtSourceFromAVTDataset.h>
#include <avtExpressionEvaluatorFilter.h>
#include <avtDisplaceFilter.h>
#include <avtDatabaseWriter.h>
#include <avtDatabaseMetaData.h>


// ****************************************************************************
//  Method: avtTraceHistoryFilter constructor
//
//  Programmer: childs -- generated by xml2avt
//  Creation:   Sun Apr 8 17:04:24 PST 2007
//
// ****************************************************************************

avtTraceHistoryFilter::avtTraceHistoryFilter()
{
}


// ****************************************************************************
//  Method: avtTraceHistoryFilter destructor
//
//  Programmer: childs -- generated by xml2avt
//  Creation:   Sun Apr 8 17:04:24 PST 2007
//
//  Modifications:
//
// ****************************************************************************

avtTraceHistoryFilter::~avtTraceHistoryFilter()
{
}


// ****************************************************************************
//  Method:  avtTraceHistoryFilter::Create
//
//  Programmer: childs -- generated by xml2avt
//  Creation:   Sun Apr 8 17:04:24 PST 2007
//
// ****************************************************************************

avtFilter *
avtTraceHistoryFilter::Create()
{
    return new avtTraceHistoryFilter();
}


// ****************************************************************************
//  Method:      avtTraceHistoryFilter::SetAtts
//
//  Purpose:
//      Sets the state of the filter based on the attribute object.
//
//  Arguments:
//      a        The attributes to use.
//
//  Programmer: childs -- generated by xml2avt
//  Creation:   Sun Apr 8 17:04:24 PST 2007
//
// ****************************************************************************

void
avtTraceHistoryFilter::SetAtts(const AttributeGroup *a)
{
    atts = *(const TraceHistoryAttributes*)a;
}


// ****************************************************************************
//  Method: avtTraceHistoryFilter::Equivalent
//
//  Purpose:
//      Returns true if creating a new avtTraceHistoryFilter with the given
//      parameters would result in an equivalent avtTraceHistoryFilter.
//
//  Programmer: childs -- generated by xml2avt
//  Creation:   Sun Apr 8 17:04:24 PST 2007
//
// ****************************************************************************

bool
avtTraceHistoryFilter::Equivalent(const AttributeGroup *a)
{
    return (atts == *(TraceHistoryAttributes*)a);
}


// ****************************************************************************
//  Method: avtTraceHistoryFilter::Execute
//
//  Purpose:
//      Sends the specified input and output through the TraceHistory filter.
//
//  Arguments:
//      in_ds      The input dataset.
//      <unused>   The domain number.
//      <unused>   The label.
//
//  Returns:       The output dataset.
//
//  Programmer: childs -- generated by xml2avt
//  Creation:   Sun Apr 8 17:04:24 PST 2007
//
// ****************************************************************************

void
avtTraceHistoryFilter::Execute(void)
{
    //
    // Write out the connectivity.  "-1" is the hint to do that.
    //
    OutputTime(GetTypedInput(), -1);

    //
    // Pull out just the points.
    //
    avtDataset_p ds = InitializeDataset();

    //
    // Put the fields from the current time step on the points and then
    // output those fields.
    //
    PerformIteration(0, ds, false);
    OutputTime(ds, 0);

    //
    // Now iterate through the time slices, displacing and evaluating.
    //
    for (int i = 0 ; i < atts.GetNumiter() ; i++)
    {
        ds->SetSource(GetInput()->GetSource());
        PerformIteration(i, ds, true);
        OutputTime(ds, i+1);
    }

    // The operator just passes the data through.
    GetDataTree() = GetInputDataTree();
}


// ****************************************************************************
//  Method: avtTraceHistoryFilter::AdditionalPipelineFilters
//
//  Purpose:
//      Does a best guess at the number of substages, in an attempt to keep
//      the progress as accurate.
//
//  Programmer: Hank Childs
//  Creation:   April 8, 2007
//
// ****************************************************************************

int
avtTraceHistoryFilter::AdditionalPipelineFilters(void)
{
    return atts.GetNumiter()*(5+atts.GetVars().size());
}


// ****************************************************************************
//  Method: avtTraceHistoryFilter::InitializeDataset
//
//  Purpose:
//      Takes the input data set and extracts its points.  Also dumps out a 
//      "root"-file and the connectivity information.
//
//  Programmer: Hank Childs
//  Creation:   April 8, 2007
//
// ****************************************************************************

avtDataset_p
avtTraceHistoryFilter::InitializeDataset(void)
{
    int  i, j;

    avtDataTree_p inTree = GetInputDataTree();
    int nLeaves;
    vtkDataSet **ds = inTree->GetAllLeaves(nLeaves);
    vtkDataSet **ugrids = new vtkDataSet*[nLeaves];
    for (i = 0 ; i < nLeaves ; i++)
    {
        vtkPoints *pts = vtkVisItUtility::GetPoints(ds[i]);

        vtkUnstructuredGrid *ugrid = vtkUnstructuredGrid::New();
        ugrid->SetPoints(pts);
        int npts = pts->GetNumberOfPoints();
        ugrid->Allocate(2*npts);
        vtkIdType onevertex[1];
        for (j = 0 ; j < npts ; j++)
        {
             onevertex[0] = j;
             ugrid->InsertNextCell(VTK_VERTEX, 1, onevertex);
        }
        ugrids[i] = ugrid;

        pts->Delete();
    }

    avtDataTree_p newTree = new avtDataTree(nLeaves, ugrids, -1);
    for (i = 0 ; i < nLeaves ; i++)
        ugrids[i]->Delete();
    delete [] ugrids;

    avtDataset_p rv = new avtDataset(GetTypedInput(), newTree);
    avtDataAttributes &atts = rv->GetInfo().GetAttributes();
    int nVars = atts.GetNumberOfVariables();
    while (nVars > 0)
    {
        atts.RemoveVariable(atts.GetVariableName(0));
        nVars = atts.GetNumberOfVariables();
    }

274 275 276
    // Free the memory from the GetAllLeaves function call.
    delete [] ds;

277 278 279 280 281 282 283 284 285 286 287 288 289
    return rv;
}


// ****************************************************************************
//  Method: avtTraceHistoryFilter::OutputTime
//
//  Purpose:
//      Outputs a time slice.
//
//  Programmer: Hank Childs
//  Creation:   May 22, 2007
//
whitlocb's avatar
whitlocb committed
290 291 292 293 294 295
//  Modifications:
//    Brad Whitlock, Mon Aug 18 09:32:03 PDT 2008
//    Made it directly include the ExtrudedVol writer instead of accessing
//    it via the plugin managers since those are now off limits. The AVT pipeline
//    probably should not be aware of plugins anyway.
//
296 297
// ****************************************************************************

whitlocb's avatar
whitlocb committed
298 299
#include <avtExtrudedVolWriter.h>

300 301 302
void
avtTraceHistoryFilter::OutputTime(avtDataset_p ds, int time)
{
whitlocb's avatar
whitlocb committed
303
    DBOptionsAttributes *opts = new DBOptionsAttributes();
304 305 306
    opts->SetInt("Time", time);
    opts->SetInt("nTimes", atts.GetNumiter());

whitlocb's avatar
whitlocb committed
307
    avtExtrudedVolWriter *wrtr = new avtExtrudedVolWriter(opts);
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

    avtDataObject_p dob;
    CopyTo(dob, ds);
    wrtr->SetInput(dob);
    char filename[1024];
    strcpy(filename, atts.GetOutput().c_str());
    avtDatabaseMetaData md;
    avtMeshMetaData mmd;
    mmd.name = "mesh";
    md.AddMeshes(mmd);
    std::vector<std::string> vars;
    avtVectorMetaData vmd;
    vmd.name = atts.GetDisplacement();
    vmd.meshName = mmd.name;
    md.AddVectors(vmd);

    //vars.push_back(vmd.name);
    wrtr->Write(filename, &md, vars, false);
    delete wrtr;
whitlocb's avatar
whitlocb committed
327
    delete opts;
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
}


// ****************************************************************************
//  Method: avtTraceHistoryFilter::AddExpression
//
//  Purpose:
//      Adds an expression to the list of expressions used by the expression
//      evaluator filter.
//
//  Programmer: Hank Childs
//  Creation:   May 22, 2007
//
// ****************************************************************************

void
AddExpression(ExpressionList &new_list, std::string &var, int time)
{
    char exp_name[1024];
    SNPRINTF(exp_name, 1024, "AVT_TRACE_HIST_%s", var.c_str());
    char exp_defn[1024];
    const char *meshname = var.c_str(); // The expression language will let
                                        // you put in a variable for the
                                        // meshname since it can use that to 
                                        // find the meshname.
    meshname = "quadmesh";
    SNPRINTF(exp_defn, 1024, "pos_cmfe(<[%d]i:%s>, %s, 0.)", time, 
                                                var.c_str(), meshname);

    Expression exp;
    exp.SetName(exp_name);
    exp.SetDefinition(exp_defn);
    exp.SetType(Expression::Unknown);
    new_list.AddExpressions(exp);
}


// ****************************************************************************
//  Method: avtTraceHistoryFilter::PerformIteration
//
//  Purpose:
//      Move the points forward by the displacement vector, evaluate the fields
//      at their position, and then output them.
//
//  Programmer: Hank Childs
//  Creation:   May 22, 2007
//
// ****************************************************************************

void
avtTraceHistoryFilter::PerformIteration(int time, avtDataset_p &ds, bool doDisp)
{
    int   i;

    // Store off the original expression list.
    ParsingExprList *pel = ParsingExprList::Instance();
    ExpressionList orig_list = *(pel->GetList());

hrchilds's avatar
hrchilds committed
386 387 388
    avtContract_p spec = GetGeneralContract();
    avtDataRequest_p dataRequest = spec->GetDataRequest();
    int timestep = dataRequest->GetTimestep();
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

    int newTimeIndex = timestep-time;
    if (newTimeIndex < 0)
    {
        EXCEPTION1(VisItException, "You asked to trace back before the first"
                      " dump.");
    }

    ExpressionList new_list = *(pel->GetList());
    std::vector<std::string> vars = atts.GetVars();
    for (i = 0 ; i < vars.size () ; i++)
    {
        AddExpression(new_list, vars[i], newTimeIndex);
    }
    if (doDisp)
        AddExpression(new_list, atts.GetDisplacement(), newTimeIndex);
    *(pel->GetList()) = new_list;

    avtSourceFromAVTDataset termsrc(ds);
    avtDataObject_p data = termsrc.GetOutput();

    avtExpressionEvaluatorFilter eef;
    eef.SetInput(data);
    
hrchilds's avatar
hrchilds committed
413
    avtDataRequest_p dataRequest2 = new avtDataRequest(dataRequest);
414 415 416 417
    for (i = 0 ; i < vars.size() ; i++)
    {
        char exp_name[1024];
        SNPRINTF(exp_name, 1024, "AVT_TRACE_HIST_%s", vars[i].c_str());
hrchilds's avatar
hrchilds committed
418
        dataRequest2->AddSecondaryVariable(exp_name);
419 420 421 422 423 424 425 426 427 428 429 430 431
    }

    if (doDisp)
    {
        char disp_name[1024];
        SNPRINTF(disp_name, 1024, "AVT_TRACE_HIST_%s", 
                                               atts.GetDisplacement().c_str());

        avtDisplaceFilter disp;
        disp.SetInput(eef.GetOutput());

        disp.SetVariable(disp_name);
        disp.SetFactor(1.);
hrchilds's avatar
hrchilds committed
432
        dataRequest2->AddSecondaryVariable(disp_name);
433

hrchilds's avatar
hrchilds committed
434 435
        avtContract_p spec2 = new avtContract(spec, 
                                                                       dataRequest2);
436 437 438 439 440
        disp.Update(spec2);
        ds = disp.GetTypedOutput();
    }
    else
    {
hrchilds's avatar
hrchilds committed
441 442
        avtContract_p spec2 = new avtContract(spec, 
                                                                       dataRequest2);
443 444 445 446 447 448 449 450 451 452
        eef.Update(spec2);
        ds = eef.GetTypedOutput();
    }

    // Restore the original expression list ... i.e. undo the temporary 
    // expressions we put in.
    *(pel->GetList()) = orig_list;
}