AMCAX Kernel 1.0.0.0
Loading...
Searching...
No Matches
MCNP Parser Example​

Overview

This tutorial provides the basic usage of the MCNP parser: parsing MCNP input cards, automatically constructing topological entities, and finally exporting the results in the form of Labels (with material attributes attached to the Labels). It is suitable for scenarios where MCNP geometry needs to be converted into CAD representations like BREP.

Input File and Output BREP File Preview

The left image shows the input MCNP file (a section is captured), and the right image shows the parsed and exported BREP file.

Namespaces

For clarity in the example code, namespaces are used.

using namespace AMCAX;
using namespace AMCAX::MCNP;
Namespace of all interface in the AMCAX kernel.
Definition misc.docu:8

Complete Example

The basic usage flow is: construct an MCNPReader parser, then call the Read interface to get the parsing results. The following example code demonstrates how to construct the parser for parsing, export the geometry in BREP format, and how to extract material attributes from the Labels.

Parse by Filename

MCNPReader reader("LCT001");
std::vector<Label> labels = reader.Read();
Class for parsing MCNP files into TopoShape objects and labels.
Definition MCNPReader.hpp:16

Parse by File Stream

std::ifstream fin("LCT001");
if(fin)
{
MCNPReader reader(fin);
std::vector<Label> labels = reader.Read();
}

Get Material Attributes from Parsing Results

for(auto& iter:labels)
{
auto attrs = iter.FindAllAttributes();
for(auto& attr:attrs)
{
int mat = attr->GetValue<AMCAX::Int16Attribute>();
std::cout<<"m: "<< mat <<std::endl;
}
}

Export Parsing Results as a BREP File

builder.MakeCompound(cmp);
for(auto& iter:labels)
{
AMCAX::TopoShape shape = iter.GetShape();
builder.Add(cmp,shape);
}
std::string file("LCT001.brep");
std::ofstream mcnp(file);
static AMCAX_API bool Write(const TopoShape &s, std::ostream &os, int format=3)
Write a shape to a stream.
Class of a tool for building B-Rep structure.
Definition TopoBuilder.hpp:39
AMCAX_API void Add(TopoShape &s, const TopoShape &c) const
Add a sub-shape to a shape.
AMCAX_API void MakeCompound(TopoCompound &c) const
Make an empty compound.
Class of compound.
Definition TopoCompound.hpp:12
Base class of shape, containing an underlying shape with a location and an orientation.
Definition TopoShape.hpp:15

Appendix

#include <iostream>
#include <fstream>
#include <filesystem>
using namespace AMCAX;
using namespace AMCAX::MCNP;
int main()
{
MCNPReader reader("./data/LCT001");
std::vector<Label> labels = reader.Read();
builder.MakeCompound(cmp);
for (auto& iter : labels)
{
AMCAX::TopoShape shape = iter.GetShape();
builder.Add(cmp, shape);
auto attrs = iter.FindAllAttributes();
for (auto& attr : attrs)
{
int mat = attr->GetValue<AMCAX::Int16Attribute>();
std::cout << "m: " << mat << "\n";
}
}
std::string file("LCT001.brep");
std::ofstream mcnp(file);
}
The class of AttributeTool.
Reader class for parsing MCNP files into TopoShape objects and Labels.
Class of read and write shapes from OCCT BRep files.
Class of a tool for building B-Rep structure.
Class of compound.

Click here to download the complete source code for the above examples. Please download it as needed for your learning purposes.